I've seen this "use exists() to avoid autovivification" misconception around a lot. I suspect it comes from this statement in exists:

The element is not autovivified if it doesn’t exist.

Other similar functions like defined don't have any such statement, leading people to believe that those somehow do autovivify elements, which in turn leads to unnecessarily complicated code like

  my $foo_in_hash = exists $hash{foo} && defined $hash{foo};

I'm not sure why that disclaimer is there in the first place. Surely it's never meaningful to talk about autovivifying elements, since autovivification is about the silent creation of actual hashes and arrays, not their contents? And this also leads some people to think exists doesn't autovivify things at all, which it certainly does (as indeed the documentation goes on to explain... but some people never read past the first paragraph!)

This is one of those little nitpicks I'm often tempted to submit a bug report about, but I'm never quite convinced it's really serious enough to warrant it. (And I'm not 100% confident it's wrong! I'm painfully conscious there's still a heck of a lot left for me to learn about Perl.)


In reply to Re^7: Common hash keys by Porculus
in thread Common hash keys by Anonymous Monk

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.