Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the <<'m'=~m>> syntax mean in perl?

Tags:

perl

So I understand that perl has much unusual syntax, but I came across a code snippet at work that other day that has left me confused. Could someone please explain to me what it means:

<<'m'=~m>> print $a unless $b; return; m ; 

It looks like HEREDOC syntax, but not in any form I've ever seen.

like image 618
AliceHemp Avatar asked Sep 19 '15 07:09

AliceHemp


People also ask

What is M in Perl?

m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.

What does ~= mean in Perl?

=~ is the Perl binding operator and can be used to determine if a regular expression match occurred (true or false) $sentence = "The river flows slowly."; if ($sentence =~ /river/) { print "Matched river.\n"; } else { print "Did not match river.\n"; } Follow this answer to receive notifications.

What is the meaning of $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

What does \c mean in Perl?

to print 0x1F or control-underscore or 'unit separator', etc. You can use lower-case or upper-case letters, but only the non-alphabetic characters shown ( "\c@" is the null byte, '\0' in C). Note that the backslash needs special treatment. The escape sequence cannot appear at the end of a string.


1 Answers

This secret syntax is known as the ornate double-bladed sword, and is typically denoted as:

<<m=~m>>  Comments here  m ; 

It is a hack for multi-line comments, making use of the heredoc syntax and match operator (=~) with > as delimiter.

Note, as slashes, /, aren't being used as the delimiter for matching, the 'm' operator is required. For example $my_var =~ /test/ is equivalent to $my_var =~ m>test>, whereas $my_var =~ >test> would be invalid.

Pay attention to the fact that the first m character in your snippet is enclosed by single quotes, meaning the $a and $b variables won't be interpolated. Had these quotes been omitted (as per my provided code example) perl would automatically add double quotes to the end marker and any subsequent variables would be interpolated. This would cause problems if $a and $b are no longer defined and you're using use warnings;.

To make the ornate double-blade example above easier to understand (but still not recommended), we could write:

<<"END" =~ //  Comments here  END ; 

Note that the =~ // is redundant, so this is equivalent to:

<<"END";  Comments here  END 

Which is just standard heredoc syntax in void context.

I would not recommend using this in production code as having a string in void context can cause problems. Plus this code is a head scratcher for even the most experienced perl programmers and is pointlessly difficult to read and maintain! If you want to create proper multi-line comments then I would suggest sticking to pod as explained here How do I enter a multi-line comment in Perl?

Hope this clears it up.

like image 185
Hayden Avatar answered Oct 12 '22 20:10

Hayden