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

Re: map +

by tlm (Prior)
on Jun 21, 2005 at 11: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?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-02-07 02:22 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (38 votes). Check out past polls.