Tie::Memoize(3perl)
π Tie::Memoize β it's a Perl manual page, bestie π
okay so this is Tie::Memoize from the Perl Programmers Reference Guide, and it's genuinely one of the cutest little modules ever. it lets a tied hash lazily load its values β like, it doesn't do the work until you actually ask for something. main character energy tbh. π¦
β¨ NAME β¨
Tie::Memoize β add data to hash when needed π
πΈ SYNOPSIS πΈ
require Tie::Memoize;
tie %hash, 'Tie::Memoize',
\&fetch, # The rest is optional
$DATA, \&exists,
{%ini_value}, {%ini_existence};
see that "the rest is optional" comment? that's not me being cute, that's fr in the source. the only thing you gotta pass is that \&fetch coderef. π«Ά
π DESCRIPTION π
so this package lets a tied hash autoload its values on the first access, and then it uses the cached value on all the following accesses. one and done, no cap. β¨
here's the tea: only read-accesses β like fetching the value or checking exists β actually trigger calls to your functions. the modify-accesses just happen like they would on a normal, boring, non-tied hash. so writes don't bother your callbacks at all. π
the required arguments when you call tie are:
- the hash itself,
- the package (
'Tie::Memoize'), - and the reference to the
FETCHing function.
the optional arguments are:
- an arbitrary scalar
$data, - the reference to the
EXISTSfunction, - initial values of the hash,
- and initial values of the existence cache.
both the FETCHing function and the EXISTS function have the same signature: the arguments they get are $key, $data, and that $data is literally the same value you passed in during tie()ing. π
both functions should return an empty list if the value does not exist!!
if the EXISTS function is different from the FETCHing function, it should return a TRUE value on success. and the FETCHing function should return the intended value if the key is valid. that's it, that's the contract. π§
π¦ Inheriting from Tie::Memoize π¦
if you're subclassing this (go off, icon), the structure of the tied() data is an array reference with these elements:
0: cache of known values
1: cache of known existence of keys
2: FETCH function
3: EXISTS function
4: $data
the rest of the array is for internal usage of this package, so like, don't touch it. π
in particular, ifTIEHASHis overwritten, it should callSUPER::TIEHASH. do not skip this. this is the vibe check.
π₯ EXAMPLE π₯
sub slurp {
my ($key, $dir) = shift;
open my $h, '<', "$dir/$key" or return;
local $/; <$h> # slurp it all
}
sub exists { my ($key, $dir) = shift; return -f "$dir/$key" }
tie %hash, 'Tie::Memoize', \&slurp, $directory, \&exists,
{ fake_file1 => $content1, fake_file2 => $content2 },
{ pretend_does_not_exists => 0, known_to_exist => 1 };
okay so what's happening here β this example treats the slightly modified contents of $directory as a hash. the modifications are:
- the keys
fake_file1andfake_file2fetch values$content1and$content2respectively, - and
pretend_does_not_existswill never be accessed.
additionally, the existence of known_to_exist is never checked β so if it does not exist when its content is needed, the user of %hash may be confused. π that's literally what the manpage says and i'm not editorializing, it's just true.
π BUGS π
FIRSTKEY and NEXTKEY methods go through the keys which were already read, not all the possible keys of the hash. so if you're iterating, you're only seeing the ones that have been touched. that's the bug note, fr fr. π
π AUTHOR π
Ilya Zakharevich <mailto:perl-module-hash-memoize@ilyaz.org>. π
and that's the whole thing, bestie!! this page came straight out of perl v5.44.0, dated 2026-10-17, and it lives at Tie::Memoize(3perl) in the Perl Programmers Reference Guide. slay, go memoize something. π
β¨π
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms