Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

<<q=~q>>;
  This is a multiline comment.
q

Running it through -M0=Deparse gives:

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

Can someone tell me what is going on?

like image 547
Joel Berger Avatar asked Mar 26 '11 14:03

Joel Berger


People also ask

How do I comment out multiple lines in Perl?

In Perl 6, multi-line comments are supported. The syntax for a multi-line comment is the following. It starts with the #` sequence (the # character, same as in one-line comments, followed by a backtick symbol). Then, the part with the body of the comment goes in.

How do I comment out a block of code in Perl?

Block comments in Perl are enclosed within the = and =cut symbols. Everything written after the = symbol is considered part of the comment until =cut is encountered. There should be no whitespace following = in the multi-line comment.

How will you describe a multi-line comment?

/* */ (multiline comment) Multiline comments are used for large text descriptions of code or to comment out chunks of code while debugging applications. Comments are ignored by the compiler.

Which is the correct way to put multi-line comment?

Using triple-quoted string literals Another way to add multiline comments is to use triple-quoted, multi-line strings. These strings are to be used carefully and should not be confused with Docstrings (triple-quoted string literals appearing right after a function/class/module to generate documentation).


2 Answers

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.
EOF

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:

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

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).

like image 77
Gilles 'SO- stop being evil' Avatar answered Sep 19 '22 23:09

Gilles 'SO- stop being evil'


Hah. There is no such thing as a "<< >>" operator. What's going on there is basically equivalent to this:

"" =~ ''

=~ is, of course, the normal pattern-binding operator you normally use with m// or s///. The '' is using the q{} syntax for literal strings, with > as a delimiter, and is interpreted as a pattern at run-time. The "" is a here-doc with q as the terminating string.

I certainly wouldn't call this a comment though. Consider the output of this program fragment:

"copacetic" =~ q/(c[a-z]+)/;
print "$1\n";

<<q=~q>>;
    This is crap, not a comment!
q

print "$1\n";
like image 23
Anomie Avatar answered Sep 20 '22 23:09

Anomie