Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: map syntax error -- weird

by BrowserUk (Pope)
on Mar 22, 2013 at 10:32 UTC ( #1024911=note: print w/ replies, xml ) Need Help??


in reply to map syntax error -- weird

It is a well-known limitation of the perl parser.

It decides that this: map { ... => ... } ... is a map returning a hashref; and then the absence of a comma means if doesn't have enough arguments.

Fix it by giving it a hint that the curlies are a code block like this: map {; "x" => $y } qw(1 2 3);


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: map syntax error -- weird
Select or Download Code
Re^2: map syntax error -- weird
by hdb (Prior) on Mar 22, 2013 at 10:38 UTC

    Why does this work? (same result as your proposals)

    my %opt; my @vals = qw(title artist album); my @opts = map { "$_"."=s" => \$opt{$_} } @vals; print @opts;

    UPDATE: Guess: Because there is some statement in there that needs to be executed rather than a static string only?

      Guess: Because there is some statement in there that needs to be executed rather than a static string only?

      Indeed.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      > Why does this work? (same result as your proposals)

      There is no clear rule to distinguish between code-block and hash-ref!!!

      The parser uses DWIM heuristics to guess if it looks more like a hash-ref or not. (Furthermore "w/o look-ahead")

      So in complicated cases you need to give hints for what you mean.

      This ambiguity should be one reason why Perl6 switched from {} to <> to indicate hashes.

      > UPDATE: Guess: Because there is some statement in there that needs to be executed rather than a static string only?

      well it's a perfect hash-ref!

      DB<112> $hr={ "$_"."=s" => \$opt{$_} } => { "=s" => \undef }

      But I think the used heuristic must be quite simple, something like

        (bareword || variable) (comma || fatcomma) (expr)

      So "$_"."=s" is already to complex to fit this rule of thumb.

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re^2: map syntax error -- weird
by tobyink (Abbot) on Mar 22, 2013 at 22:38 UTC

    And just for completeness, to disambiguate the other way, use a leading +:

    map +{ "x" => $y }, qw(1 2 3);
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      map +{... map EXPR, LIST map {;... map BLOCK LIST

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2014-09-19 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls