Perl qq/STRING/ Function
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 = 'Paris is the most exciting city.';
print "\$str:\t$str\n";
# it prints:
# $str: Paris 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 anyinterpolation 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%;
Table of Contents:
A Perl Script Install Perl Running Perl Perl Data Types Perl Variables Perl Operators Perl Lists Perl Arrays Array Size Array Length Perl Hashes Perl Statements Perl if Perl unless Perl switch Perl while Perl do-while Perl until Perl do-until Perl for Perl foreach Built-in Perl Functions Functions by Category String Functions Regular Expressions and Pattern Matching List Functions Array Functions Hash Functions Miscellaneous Functions Functions in alphabetical order chomp chop chr crypt defined delete each exists grep hex index join keys lc lcfirst length map oct ord pack pop push q qq (more) qw reverse rindex scalar shift sort splice split sprintf substr tr uc ucfirst undef unpack unshift values
return from Perl qq/STRING/ function to Perl Basics
Would you like to create your own website like this one? Hit the Alarm Clock!
|