in reply to strange syntax error with map

From map:

{ starts both hash references and blocks, so map { ... could be either the start of map BLOCK LIST or map EXPR, LIST. Because Perl doesn't look ahead for the closing } it has to take a guess at which it's dealing with based on what it finds just after the {. Usually it gets it right, but if it doesn't it won't realize something is wrong until it gets to the } and encounters the missing (or unexpected) comma. The syntax error will be reported close to the }, but you'll need to change something near the { such as using a unary + or semicolon to give Perl some help:
$ perl -wMstrict -le 'print map { (2, $_) } 1 .. 2' 2122 $ perl -wMstrict -le 'print map {; 2, $_ } 1 .. 2' 2122 $ perl -wMstrict -le 'print map { +2, $_ } 1 .. 2' 2122

Replies are listed 'Best First'.
Re^2: strange syntax error with map (unary plus)
by LanX (Saint) on Apr 09, 2017 at 13:52 UTC
    >   perl -wMstrict -le 'print map { +2, $_ } 1 .. 2'

    I'm not a big fan of the unary plus solution, since it's not easily understandable why the unary plus sometimes works or not.

    Fun with Perl's parsing:

    DB<100> map {+2,$_} 1..3 # +2 is not a hash key => (2, 1, 2, 2, 2, 3) DB<101> map {2,$_} 1..3 # 2 is a hash key ;; Not enough arguments for map at (eval 21)[multi_perl5db.pl:644] line 2, near "} 1" syntax error at (eval 21)[multi_perl5db.pl:644] line 2, near "} 1" DB<102> map {2,$_}, 1..3 # 2 is a hash key => ({ 2 => 1 }, { 2 => 2 }, { 2 => 3 }) DB<103> map {+2,$_}, 1..3 # +2 is not a hash key ;; syntax error at (eval 26)[multi_perl5db.pl:644] line 2, near "}," DB<104> map {+2 => $_}, 1..3 # +2 is not a hash key even with fat co +mma ;; syntax error at (eval 29)[multi_perl5db.pl:644] line 2, near "},"

    so far so consistent, BUT

    DB<105> map +{+2 => $_}, 1..3 # +2 IS A HASH KEY => ({ 2 => 1 }, { 2 => 2 }, { 2 => 3 }) DB<106> map +{+2 , $_}, 1..3 # +2 IS A HASH KEY even without => => ({ 2 => 1 }, { 2 => 2 }, { 2 => 3 })

    see my point? :)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      I'm not a big fan of the unary plus solution

      I agree, I was just keeping the examples in line with what the map docs were saying. Personally in this case I'd prefer map { (...) } ..., since it makes it look obvious that returning a list is intentional. OTOH I also use map {$_=>1} ... very often, but that one doesn't cause any parse trouble.

      The map +{...}, ... stuff is also mentioned in the map docs, although I haven't yet seen it used in the wild - personally I'd probably write map { {...} } .... As you can tell I usually prefer map BLOCK LIST over map EXPR, LIST.

      ... to force an anon hash constructor use +{:
      my @hashes = map +{ lc($_) => 1 }, @array # EXPR, so needs # comma at end
      to get a list of anonymous hashes each with only one entry apiece.
Re^2: strange syntax error with map
by BillKSmith (Monsignor) on Apr 09, 2017 at 13:30 UTC
    I recently had a similar problem which I solved by changing to the EXPR,LIST form of map. Thanks for the explanation.
    C:\Users\Bill>perl -wMstrict -le "print map( do{2,$_}, 1..2)" 2122
    Bill
      Far simpler way to disambiguate:
      {; ... } # BLOCK +{ ... } # Hash constructor
        Your solution requires an understanding of the problem. That is exactly what I was thanking haukex for. Note that I bypassed the problem rather than fix it.
        Bill
      Nice idea to use 'do{ }' sentence, I like to use it. So I'll use 'do{ }' in such situations.
Re^2: strange syntax error with map
by rsFalse (Chaplain) on Apr 09, 2017 at 11:47 UTC
    Thanks ;)