Search code examples
perlperl-hash

What's the difference between these two definitions?


Why it's a syntax error:

my @hash{1..4}=(1..4);

but not this one:

my %hash;
@hash{1..4}=(1..4);

Solution

  • the 1st example is of a lexically scoped 'my' + a hash slice which pre-supposes that one can declare a hash in the manner of a slice which is not valid syntax. your 2nd example is appropriate, declaring the hash first, assuming that you're use'ing strict + warnings;