Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: map syntax bug-a-boo?

by chipmunk (Parson)
on Nov 30, 2001 at 09:53 UTC ( #128560=note: print w/replies, xml ) Need Help??

in reply to map syntax bug-a-boo?

Curly braces can denote a block or an anonymous hash, and map can take a block or an expression, so map followed by a curly brace is ambiguous. Unfortunately, perl doesn't look forward to the closing brace to see whether it's a block or an anonymous hash, so perl has to guess.

If the opening curly brace is followed by a string and a comma (, or =>), perl guesses it's an anonymous hash. Anything else, and perl guesses it's a block. This heuristic works most of the time, but in some cases perl guesses wrong:

# a block returning a list of two elements, # but perl thinks it's an anonymous hash %hash = map { "$_", 2 } @l; # an anonymous hash, but perl thinks it's a block @hash_refs = map { $_, 2 }, @l;
However, there's not much urgency to fix this problem, because it's so easy to avoid:
# force parsing as a block %hash = map { +"$_", 2 } @l; %hash = map { ; "$_", 2 } @l; %hash = map { ( "$_", 2) } @l; # force parsing as an anonymous hash @hash_refs = map +{ $_, 2 }, @l;

Replies are listed 'Best First'.
Re: Re: map syntax bug-a-boo?
by shotgunefx (Parson) on Nov 30, 2001 at 17:38 UTC
    Can I ask a silly question? What does the + mean to Perl in these examples. I don't ever recall seeing it ever used in such a way.


    "To be civilized is to deny one's nature."

      It's like making explicit the implicit '+' that precedes any non-negative number (think scalar ::= number or string).

      Do you remember, when learning how to format numbers for output (using '%f', '%g', etc.) with printf in C, you had to account for the sign position? It would display as either a hyphen or blank. A blank, however, was analagous to a plus-sign.

      Likewise, on input, non-negative numbers could optionally be expressed with a leading plus-sign. Since scalars (or scalar expressions) may be either numbers or a strings in perl, perl allows this with arbitrary scalar expressions.


      You can give a man a fish and feed him for a day ...
      Or, you can teach him to fish and feed him for a lifetime
      Unary minus negates its operand, either adding a minus sign, or switching a minus sign to a plus sign or vice versa. Unary plus, on the other hand, returns its operand completely unchanged. Why is this useful? It can come in handy when dealing with ambiguous syntax.

      More examples of unary plus:

      print (3-1)*2; # oops! print (...) interpreted as function ... print +(3-1)*2; sub foo { $foo } $h{foo}; # equivalent to $h{'foo'} $h{+foo}; # but maybe you meant $h{foo()}
      See perlop for more on this curious operator.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2020-01-20 23:55 GMT
Find Nodes?
    Voting Booth?