Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between these two definitions?

Tags:

perl

perl-hash

Why it's a syntax error:

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

but not this one:

my %hash;
@hash{1..4}=(1..4);
like image 221
bolbol Avatar asked Jun 10 '26 06:06

bolbol


2 Answers

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;

like image 89
shinronin Avatar answered Jun 11 '26 23:06

shinronin


my requires a variable or a list of variable in parens as argument.

@hash{1..4}

is neither of those, so

my @hash{1..4}

is a syntax error.

like image 36
ikegami Avatar answered Jun 12 '26 01:06

ikegami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!