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".
- Quoted, Interpolated and Escaped Strings in Julia
- What is the difference between single-quoted and double-quoted strings in PHP?
- Which Characters Should Be Escaped Inside A "pre" tag?
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | Multi-line Strings | Here Document
- Sorting mixed Strings in Perl
- Perl Installation and Environment Setup in Windows, Linux, and MacOS
- Perl | Removing leading and trailing white spaces (trim)
- Perl | undef and the defined function
- Perl | Variables and its Types
- Perl | Constructors and Destructors
- Perl | Finding Files and Directories
- Perl | Opening and Reading a File
- Perl | STDIN in Scalar and List Context
- Perl List and its Types
- Perl | Number and its Types
- Perl | Mutable and Immutable parameters
- Perl | Hash in Scalar and List Context
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.