Alex Rivera | Logout

Why does this map statement in Perl not compile?

Asked 2008-11-07T00:38:29.833
10

This fails:

my @a = ("a", "b", "c", "d", "e");
my %h = map { "prefix-$_" => 1 } @a;

with this error:

Not enough arguments for map at foo.pl line 4, near "} @a"

but this works:

my @a = ("a", "b", "c", "d", "e");
my %h = map { "prefix-" . $_ => 1 } @a;

why?

Edit
Report

1 Answer

20

Because Perl is guessing an EXPR (a hash reference, for example) instead of a BLOCK. This should work (note the '+' symbol):

my @a = ("a", "b", "c", "d", "e");
my %h = map { +"prefix-$_" => 1 } @a;

See http://perldoc.perl.org/functions/map.html.

answered 2008-11-07T00:45:51.690

Your Answer