Perl | Quoted, Interpolated and Escaped Strings

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 (“).
 

Quoted Strings

 
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:
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:
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.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# An array of integers from 1 to 10
@list = (1..10); 
  
# Non-interpolated string
$strng1 = 'Using Single quotes: @list'
  
# Interpolated string
$strng2 = "Using Double-quotes: @list";
print("$strng1\n$strng2");

chevron_right


Output:

Using Single quotes: @list 
Using Double-quotes: 1 2 3 4 5 6 7 8 9 10

 

Interpolation of Strings

 
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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Assining a variable with an email address
# using double-quotes
$email = "GeeksforGeeks0402@gmail.com"
  
# Printing the interpolated string
print($email);

chevron_right


Output:

GeeksforGeeks0402.com

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.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Pre-defining the array
@gmail = (a..g);
  
# Assining a variable with an email 
# address using double-quotes
$email = "GeeksforGeeks0402@gmail.com"
  
# Printing the interpolated string
print($email);

chevron_right


Output:

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.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Assining a variable with an email address
# using single-quotes
$email = 'GeeksforGeeks0402@gmail.com'
  
# Printing the non-interpolated string
print($email);

chevron_right


Output:

GeeksforGeeks0402@gmail.com

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Assining a variable with an email 
# address using double-quotes
# Note: Using '\' to escape the 
# interpolation of '@'
$email = "GeeksforGeeks0402\@gmail.com"
  
# Printing the interpolated string
print($email);
  
# variable to be substituted
$name = "GeeksforGeeks";
  
# variable to store the string
$email2 = "\n$name\@gmail.com";
  
# Printing the interpolated string
print($email2);

chevron_right


Output:


GeeksforGeeks0402@gmail.com
GeeksforGeeks@gmail.com

 

Escaping the escape character

 
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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Using Two escape characters to avoid
# the substitution of escape(\) with blank
$string1 = "Using the escape(\\) character"
  
# Printing the Interpolated string
print($string1);

chevron_right


Output:

Using the escape(\) character

 

Escaping double quotes

 
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.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/usr/bin/perl
  
# Escaping double-quotes with '\'
$string = "This page is \"Geeks For Geeks\"."
  
# Printing the interpolated string
print($string);

chevron_right


Output:

This page is "Geeks For Geeks".


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.