But I don't understand why it works
It's a hash slice.
@foo{@keys} = @values; looks like it's affecting @foo. But it isn't. It's affecting %foo
So $foo[0] affects $foo? No. The sigil indicates the value type, not the variable type. $foo[3] refers to the scalar value (thus the $) at element 3 of the array @foo.
@foo{qw( 1 2 3 )} = qw( a b c ); works the same as %foo = qw( 1 a 2 b 3 c );
Only if the hash is initially empty.
my %foo = qw( a 1 b 2 ); my %bar = qw( a 1 b 2 ); %foo = qw( c 3 d 4 ); @bar{qw( c d )} = qw( 3 4 ); print(join(' ', %foo), "\n"); # c 3 d 4 print(join(' ', %bar), "\n"); # a 1 b 2 c 3 d 4
I have an intuition that this has to do with (de)referencing of typeglobs
No, no typeglobs involved, so it works equally well on lexical (my) variables as package (our) variables.
In reply to Re: Hash initialization works, but why?
by ikegami
in thread Hash initialization works, but why?
by throop
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |