After running this myself, the only outcome that I didn't expect, was that exists $hash{d} returns false after print $hash{d}. I had expected the print to autovivify the d key to contain undef.
The thing to remember is that it is not hash values that autovivify -- it is anonymous hash and array references that autovivify. For example, given an undef scalar, you can autovivify the anonymous hash just by coding as if the scalar contained a hash reference:
use strict;
use warnings;
local $\="\n";
my $hr;
print '$hr ', defined $hr ? 'defined' : 'undefined';
print '$hr->{a} ', exists $hr->{a} ? 'exists' : 'doesn\'t exist';
print '$hr ', defined $hr ? 'defined' : 'undefined';
print '$hr is ', $hr;
Prints:
$hr undefined
$hr->{a} doesn't exist
$hr defined
$hr is HASH(0x3d51c0)
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|