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.


In reply to Re^9: searching a list by Tanktalus
in thread searching a list by keiusui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.