Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: map + by tlm
in thread map + by palette

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2023-02-06 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (33 votes). Check out past polls.

    Notices?