Almost :)
Perl will auto-vivify the hashes/hashkeys *necessary* to evaluate an expression.
This will clarify the differences:
In order to test for $h1->{a}->{b}->{c} there must exist a hash for 'c' to be a key in, so Perl auto-vivifies the data structure up to that point. But notice that 'b' points to an empty hash; 'c' is not a key in it! Perl sees that 'c' is not in {'b'}, and that is all that it needs to evaluate the unless.perl -MData::Dumper -we 'print Dumper $h1 unless $h1->{a}->{b}->{c}' $VAR1 = { 'a' => { 'b' => {} } };
Here, we asked Perl to actually *obtain* the value stored in 'c', so Perl went one step further to create the hash key 'c', which holds 'undef', which it returns to the for statement.perl -MData::Dumper -we 'print Dumper $h1 for $h1->{a}->{b}->{c}' $VAR1 = { 'a' => { 'b' => { 'c' => undef } } };
In reply to The Two Auto-Vivifies
by Util
in thread re-using a hash reference (was: Mar)
by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |