Using qq
-
"qq" is another Perl trick which
helps a programmer solve the double-quote problem by allowing her to change
the double-quote delimiter in a print statement.
-
Normally, as we said, double-quotes (") are used to delimit the characters
in a print statement. However, by replacing the first quote with two q's
followed by another character, that final character becomes the new print
statement delimiter. Thus, by using "qq!", we tell Perl to use the bang
(!) character to delimit the string instead of the double quotes.
-
For example, without using qq, a print statement that outputs, 'She said,
"hi"'. would be written as
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print "She said, \"hi\".";
But with the qq making bang (!) the new delimiter, the same statement can
be written as
#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print qq!She said, "hi"!;
Why would we do this? Readability. If the print statement was surrounded
with the normal double-quotes, then every double-quote would have to be
escaped with a backslash whenever it was used within a string. The backslashes
clutter the readability of the string. Thus, we choose a different character
to delimit the string in the print statement so that we
do not have to
escape the double-quotes with backslashes.
Printing
with Here Documents
Table of Contents
Using the printf and
sprintf functions
|