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.

No (yes, you are misremembering). Despite having seen many warnings for people to use defined and/or exists, these have never explicitly prevented autovivification. They can indirectly prevent autovivification by preventing a sneaky "lvalue context" situation, but they aren't special in this regard as just about any expression will do that as well, for example, " '' .... ". And, of course, nearly all of these cases of "wrapping in an expression" change the results. A possible exception is scalar, which prevents lvalue context while not changing the value returned (if dealing with a scalar expression, of course).

So, sometimes you can avoid one level of autovivification by using a less direct expression, but:

  1. that only matters if you have an "lvalue context"
  2. what you use to gain this indirection mostly doesn't matter (exists or defined or scalar or lots of other things all work as well)
  3. you can't avoid the other levels of autovification this way

I think at some point someone was pointed to insert a use of exists in order to gain a level of indirection such that $hash{key} would no longer be in an "lvalue context" and this advice got misinterpretted as exists having some magic power (or just an intentional feature) to prevent autovivification. That has never been the case. Though, there is talk of making exists special such that it can prevent more levels of autovivification (but I'm not holding my breath on that, personally).

- tye        


In reply to Re^10: searching a list (myth) by tye
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.