Perl qq/STRING/ Function

doubly quote a string.

You can use double quotes if you need to interpolate data before processing. For instance escape characters can be used to insert newlines, tabs and other special characters into a string. In the same time the variables that are enclosed in double quotes stings are replaced with their values.

See the following example:

my $str = 'London is the most exciting city.';
print "\$str:\t$str\n";
# it prints: $str:   London is the most exciting city.
 
my @array = ('one','two','three','four');
print "\@array = @array\n";
# it prints: @array = one two three four
The first example is for strings. The first escape character is used to print the $ special character as a literal character, \t inserts a tab and $str will be interpolated and replaced with its value. Finally, the \n inserts a newline.

The second example shows you how you can interpolate an array variable. The first escape character is used to print @ as a literal character. @array will be interpolated and replaced with the array elements separated by space.

A special case is when you print html tags which requires a lot of double quotes for each tag or when you need to insert double quotes inside a string. The qq/STRING/ operator is used to interpolate variables but to ignore double quotes.

See the following example:

my $str = "It's nice";
 
print "\"$str\", he said.\n";
# it prints: "It's nice", he said.
 
print qq/"$str\", he said.\n/;
# it prints: "It's nice", he said.
 
print q/"$str\", he said.\n/;
# it prints: "$str\", he said.\n
The first print let you display the double quote characters by escaping them.

The second print uses the qq operator to interpolate the $str variable.

The third print uses q (simple quote operator) instead of qq and there are not any interpolation here, the $str variable will be literally printed.

The qq operator is used with the / (slash) character as a delimiter, but you can use other delimiters as well. The following lines of code are all equivalent:

my $str = "It's nice";
 
print qq/"$str\", he said.\n/;
print qq("$str\", he said.\n);
print qq:"$str\", he said.\n:;
print qq%"$str\", he said.\n%;