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.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

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%;

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

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
        qq (more)

return from Perl qq/STRING/ function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!