I'm planning to have a Package variable that is a hash, say %BigHash, that I will create only if it is needed. Perhaps I'll read it in from another file.

A hash can't be undef like a scalar can. What's an efficient and elegant way to test to see if I've loaded it already? If I used a ref instead of a hash variable, I could use undef as opposed to a hash ref. But I plan to make the thing available as a public variable too, and there's really no need for it to be a reference other than this feature.

If it's not defined at all, I should be able to check for exists in the Stash, right? Is that efficient, like checking the keys of a regular hash? I'm just curious at this point, since I suppose that even so, it's only as efficient as a hash exists, while normal named variables are even more efficient.

So my current plan is to use a separate lexical variable to keep track of whether it's been loaded, since that is fastest to check. But it bugs me to use a separate variable, and I still wonder if there is a better (or at least "cooler") way.


In reply to lazy creation of a hash by John M. Dlugosz

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.