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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|