What is your solution? Using defined? Sorry, but that doesn't actually change anything. Really. IMO, you're sounding an alarm over nothing, and then offering a solution that not only doesn't solve any part of your original "problem" but offers distinctly different semantics.
$ perl -MData::Dumper -e 'my $h; $h->{x}++ if exists $h->{x}; print Du
+mper($h)'
$VAR1 = {};
$ perl -MData::Dumper -e 'my $h; $h->{x}++ if defined $h->{x}; print D
+umper($h)'
$VAR1 = {};
In both cases, %$h was autovivified. And $h->{x} was not. (I think in older perl5's, $h->{x} might have been autovivified and set to undef in the defined case, but never in the exists case ... unless I'm misremembering something.) The semantics, however, are different. If $h->{x} actually is undef, the exists will return true, while the defined will return false. Which is exactly what I don't want if all I want to do is test that the key exists. That's why we have different keywords - for different tests.
A value that doesn't exist is not defined. But a value that exists may still not be defined. Thus, two tests - one for exists, one for defined.
The proper solution to this quandary is to test each reference before dereferencing it:
if ($h and exists $h->{x})
or
if ($h and exists $h->{a} and exists $h->{a}{b} and exists $h->{a}{b}{
+foo})
This is the only way to keep $h->{a}{b} from being autovivified. |