This throws an error in Perl v5.20:
use strict;
use warnings;
my @a = (2,3,9);
my %b = map { "number $_" => 2*$_ } @a;
Error:
syntax error at a.pl line 4, near "} @a"
Execution of a.pl aborted due to compilation errors.
This doesn't:
use strict;
use warnings;
my @a = (2,3,9);
my %b = map { "number ".$_ => 2*$_ } @a;
Why is interpolation of $_ disallowed within the map BLOCK?
maphas two syntax:Perl must determine which syntax you are using. The problem is that both
BLOCKandEXPRcan start with{because{ ... }can be the hash constructor (e.g.my $h = { a => 1, b => 2 };).That means that Perl's grammar is ambiguous. When an ambiguity is encountered,
perlguesses what you mean after looking ahead a little. In your situation, it guessed wrong. It guessed{was the start of a hash constructor instead of the start of a block. You will need to disambiguate explicitly.The following are convenient ways to disambiguate blocks and hash constructors:
So in your case, you could use
Related: Difference between returning +{} or {} in perl from a function, and return ref or value