A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (‘) or double quote (“).
In Perl, strings can be put in between double-quotes (” “) or in between single-quotes (‘ ‘). However, strings defined in single-quotes and those defined in double-quotes are treated differently.
Double-quoted strings are interpolated i.e. variable names (scalars, arrays, and hashes) are replaced by their original values and escape sequences (like /t, /n, etc.) do their work.
qq operator can also be used in place of double-quoted strings.
Single-quoted strings are not interpolated. They are interpreted as is, with no modifications whatsoever. q operator in Perl provides the same use as the single-quoted string.
Using Single quotes: @list Using Double-quotes: 1 2 3 4 5 6 7 8 9 10
Interpolation of Strings with the use of double quotes can sometimes become tricky because some strings contain symbols which might be of no use when interpolated. For example: ‘@’ symbol used in writing email addresses. When an email address is to be stored in a double-quoted string, then the ‘at’ (@) sign is automatically interpolated and is taken to be the beginning of the name of an array and is substituted by it. If an array with that name is found then it will replace the array name with the array values, or it will be left blank if an array with that name doesn’t exist.
In the above example, the string ($email) is interpolated and @gmail is substituted by an array named ‘@gmail’ but since no array with such name is found, @gmail is removed but is not substituted and hence “GeeksforGeeks0402.com” is printed.
In the below example, @gmail has been pre-defined and hence is substituted in place of @gmail.
GeeksforGeeks0402a b c d e f g.com
This can be corrected by using single quotes in place of double quotes. Assigning the string to the variable with the use of single quotes will remove the interpolation and hence the ‘@’ will not be considered as array declaration.
Above solution to the interpolation problem contains a drawback. What if there’s a need to substitute a variable’s value in the string along with the use of ‘@’ symbol. Then this method will be of no use as single quotes won’t allow the substitution of variable’s value. To ovecome this situation, the escape character i.e. the backslash(\) is used. The backslash is inserted just before the ‘@’ as shown below:
The backslash is the escape character and is used to make use of escape sequences. When there is a need to insert the escape character in an interpolated string, the same backslash is used, to escape the substitution of escape character with ” (blank). This allows the use of escape character in the interpolated string.
Using the escape(\) character
Use of double quotes in a string signifies the end of the string and hence, cannot be inserted directly. To insert double quotes in an interpolated string, backslash is used just before the double quotes to escape its interpolation.
This page is "Geeks For Geeks".
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.