Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

map +

by palette (Scribe)
on Jun 21, 2005 at 09:17 UTC ( #468605=perlquestion: print w/replies, xml ) Need Help??
palette has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I recently came across something like map + . I learnt the difference between map and map + is like, map returns a hash while map + return array of anonymous hash.

Can anyone of you can tell me in which context map and map + is used with examples ie; why we require a hash in one place and why an array of anonymous hash in another.


Replies are listed 'Best First'.
Re: map +
by gellyfish (Monsignor) on Jun 21, 2005 at 09:23 UTC

    Er map + is not a separate thing to map it is simply that on occasion the plus sign (the otherwise no-op unary addition ) might be necessary to disambiguate the bare block and prevent it from being parsed as an anonymous hash.


Re: map +
by tlm (Prior) on Jun 21, 2005 at 11:07 UTC

    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 { ...

    the lowliest monk

Re: map +
by borisz (Canon) on Jun 21, 2005 at 10:09 UTC
    The + is just to point the perl parser into the right direction see map.
    %hash = map { "\L$_", 1 } @array # perl guesses EXPR. wrong %hash = map { +"\L$_", 1 } @array # perl guesses BLOCK. right %hash = map { ("\L$_", 1) } @array # this also works %hash = map { lc($_), 1 } @array # as does this. %hash = map +( lc($_), 1 ), @array # this is EXPR and works! %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array) or to force an anon hash constructor use "+{" @hashes = map +{ lc($_), 1 }, @array # EXPR, so needs , at end

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://468605]
Approved by Mutant
[Your Mother]: “I’m afraid of Americans…”
[Your Mother]: “No one needs anyone / They don’t even just pretend…”
[erix]: they've stuck nuclear missiles in Kaliningrad - they can just reach Berlin :)
[LanX]: naaah! Putin wouldn't nuke all the Russians in Berlin
[Your Mother]: Putin is a bad guy but he’s not that kind of bad guy. (Who would launch.)
[erix]: fortunately, Putin seems to be ill -- or at least, they're building a hospital for him and his cronies in Moscow
[erix]: nobody knows what kind it takes :)
[erix]: I say his seems a face that launches a thousand missiles :)
[Your Mother]: I say I do. :P
[Your Mother]: Missiles would be an idiot’s play. Putin is many things but he is not an idiot.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2017-01-20 00:56 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (173 votes). Check out past polls.