Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: perl question



> I've got this Perl code
> 
>    print qq~
> <$dtd>
> <html>
> 
> <head>
> blah blah
> </html>
> ~;
> 
> 
> Now I understand what this does, it just spits out (in this case) html tags
> to stdout. What I don't understand is why.

"qq" is an operator that says "the next character, whatever it may be, is to be 
treated as a double-quote". So everything between ~ immediately after the
qq and the next occurance of ~ is treated as a double-quoted string. 
Read perlop(1) and skip to the "Quote and Quote-like Operators" section
for detail. It's useful when you have to embed double-quotes and other meta-
characters into your string. For example:

print qq/And God said, "Let there be light!"/;

is completely equivalent to:

print "And God said, \"Let there be light!\"";

but the former is more readable once you understand what the qq operator does.
In case you're wondering, there is no reason why the writer of that code
couldn't have just used double-quotes instead of qq~~, since the text
being quoted doesn't have any double-quotes. It's grammatically correct, but
needlessly obscure.

> The Camel book gives example of here documents using this format
> print <<EOF
> blah blah
> EOF;

They both work. The qq operator has the benefit of not screwing up your
indentation, while the << syntax frees you from having to look for that
one special character that isn't used in the text you're quoting. Remember,
the Perl mantra is: There Is More Than One Way To Do It. (I can almost hear
the python lovers in the audience snickering over that remark...)

> Is this qq thing something new???

Well, it's been there since at least 1996....



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links