BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

Why does this work:

#! perl -slw use strict; use Data::Dumper; my $d = [ [[1,2], [3,4]], [[5,6], [7,8]] ]; my %hash = map{ my $key = "@{ $_->[ 0 ] }"; $key => $_ } @{ $d }; print Dumper \%hash; __END__ C:\test>junk.pl $VAR1 = { '5 6' => [ [ 5, 6 ], [ 7, 8 ] ], '1 2' => [ [ 1, 2 ], [ 3, 4 ] ] };

And this not?

#! perl -slw use strict; use Data::Dumper; my $d = [ [[1,2], [3,4]], [[5,6], [7,8]] ]; my %hash = map{ "@{ $_->[ 0 ] }" => $_ } @{ $d }; print Dumper \%hash; __END__ [ 4:49:09.65] C:\test>junk.pl syntax error at C:\test\junk.pl line 7, near "} @" Execution of C:\test\junk.pl aborted due to compilation errors.

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: Syntax error that I don't understand?
by ikegami (Patriarch) on Jul 03, 2008 at 04:01 UTC

    Curlies ambiguity. In places where blocks and expressions are both valid, curlies are normally interpreted as blocks. However, when the code looks like hash construction, the curlies are interpreted as a hash constructor instead.

    >perl -ce"map{ qq{@{ $_->[ 0 ] }} => $_ } @a" syntax error at -e line 1, next char @ -e had compilation errors. >perl -ce"map{; qq{@{ $_->[ 0 ] }} => $_ } @a" -e syntax OK
        If it could happen to him, what chance do the rest of us have?