Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2020-06-03 12:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you really want to know if there is extraterrestrial life?



      Results (23 votes). Check out past polls.

      Notices?