Joel Berger Joel Berger - 1 year ago 102
Perl Question

Understanding strange Perl multiline comment mechanism

EDIT: Note to new Perl programmers: This mechanism should NOT be used for multiline comments! It has a flaw decreases readability.

In this PerlMonks post on mechanisms to produce multiline comments in Perl, Abigail provided this one, that perplexes me:

The problem with just using a here document is that it will issue a warning under '-w'. It's much better to use the little known << >> operator.

This is a multiline comment.

Running it through

" This is a multiline comment.\n" =~ //;
-e syntax OK

Can someone tell me what is going on?

Answer Source

Abigail's answer is partly humorous. There is in fact no << >> operator (not in versions of Perl before 5.22), but there is a (not that well-known, I guess) << operator. Not the binary shift operator, but the unary here-document (heredoc for short). A simple form of it is:

$long_string = <<EOF;
This is a long, multiline string.
It ends when EOF appears alone on a line.
It will be assigned to the \$long_string variable.

This is, in fact, the “multiline comment” feature underlying Abigail's answer — a multiline string literal. The rest is a bit of somewhat obfuscated Perl.

The bareword or quoted string after << ends the string literal. You can use q as a bareword:

This is a multiline comment, or rather a string literal whose value is ignored.

To understand the rest of Abigail's snippet, it helps to rewrite the here-document into a simple string literal:

"This is a multiline comment.\n" =~ q>>;

Ok, now q>> is the q quote-like operator with > as the delimiter character. q>> is equivalent to '' (a non-interpolated literal, which happens to be empty). So the string literal is matched against an empty pattern. The result of that matching is ignored anyway, but this clobbers the match result variables ($1, $&, etc).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download