in reply to Re^8: searching a list
in thread searching a list
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.
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.$ 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 = {};
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:
orif ($h and exists $h->{x})
This is the only way to keep $h->{a}{b} from being autovivified.if ($h and exists $h->{a} and exists $h->{a}{b} and exists $h->{a}{b}{ +foo})
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: searching a list (myth)
by tye (Sage) on Jun 27, 2006 at 18:48 UTC | |
|
Re^10: searching a list
by Corion (Patriarch) on Jun 27, 2006 at 14:54 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |