Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: map +

by tlm (Prior)
on Jun 21, 2005 at 07:07 UTC ( #468629=note: print w/replies, xml ) Need Help??


in reply to map +

The unary operator + is often used to disambiguate syntax that would otherwise confuse perl. In the case of map this can happen in two different situations. One is when the first non-whitespace character after the map keyword is a (. The other one is when this character is a { ...

In the case of a "(" perl can't tell whether this parenthesis signals the opening of the argument list for map or it is being used merely to group the expressions in the first argument of map. Consider this:

my %hash = map ( $_ => 1 ), @array;
perl will interpret this as
(my %hash = map( $_, 1 )), @array;
which is not the desired intent. A simple way to clue perl in is to put a + before the "("1:
my %hash = map +( $_ => 1 ), @array;
BTW, the line above is a common way to "hashify" an array.

Another common situation when you will see the same trick is with print:

print +( $x ? $y : $z ), "\n";
Without the +, the above would be interpreted as
(print( $x ? $y : $z )), "\n";

The second potentially ambiguous situation with map is when the first character after the map keyword is a {, because it could either refer to a block or to an anonymous hash. I saw this (admittedly farfetched) example recently:

my @foo = map { 1, 2 } 3, 4;
which triggers a syntax error, even though it is syntactically correct. Instead of "hmm, no comma after the }; it must be a block then", perl opts for "hmm, no comma after the }; SYNTAX ERROR!" Not very DWIM-ish, IMHO, but I'm sure that there are good reasons for this confusion, having to do with subtleties of parsing that are beyond me.

Be that as it may, perl needs help with such expressions. In this case the solution is not to prepend a +, because that would disambiguate the expression in the direction opposite to what we want. We want perl to regard the {...} as a block. The standard trick is to put a bare ; as the first token after the opening "{":

my @foo = map { ; 1, 2 } 3, 4; print "@foo\n"; __END__ 1 2 1 2
But if perl had been right originally (meaning that we did want map's first argument to be interpreted as an anonymous hash, and we did have a syntax error (by forgetting the comma after the closing }), then we could dispel all possible doubt by adding a leading + (and the forgotten comma, of course):
my @foo = map +{ 1, 2 }, 3, 4;
As it turns out, in this case adding the missing comma is enough to set perl straight:
my @foo = map { 1, 2 }, 3, 4; print "@foo\n"; __END__ HASH(0x814cbb8) HASH(0x814cd5c)
but according to perlref, this may change in the future, so I typically put that + in there anyway.

1 I've used "" around the ( to disambiguate a potential smiley. The footnote digit helps too. (:

the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://468629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2020-05-30 05:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?