Depends on how and where you're building it; as was mentioned give us a sample and someone can comment on specifics. That being said though here's a way to (for example) initialize your hash from a file with one item per list lazily and only one time (unless you explicitly clear it):

if( exists _get_cache()->{ $candidate } ) { say qq{IT DOES}; } else { say qq{No such luck . . .}; } { ## Block to scope our cache to just these subs my $lookup_cache = undef; sub _reset_cache { $lookup_cache = undef; } sub _get_cache { $lookup_cache //= _load_cache(); } sub _load_cache { ## presuming you've declared file var somewhere above . . . open( my $fh, q{<}, $CACHE_FILE_NAME ) or die qq{Can't open '$CACHE_FILE_NAME': $!\n}; $lookup_cache = {}; while( <$fh> ) { chomp; $lookup_cache->{ $_ } = 1; } close( $fh ); return $lookup_cache; } } ## End of limited scope block.

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re^3: Hash versus chain of elsifs by Fletch
in thread Hash versus chain of elsifs by mldvx4

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.