Tie::Memoize(3pm)
πΈ NAME πΈ
Tie::Memoize(3pm) β straight from the Perl Programmers Reference Guide β is literally here to add data to hash when needed. no cap, it's giving lazy-loading royalty. π
β¨ SYNOPSIS β¨
okay bestie, here's the copy-paste moment:
require Tie::Memoize;
tie %hash, 'Tie::Memoize',
\&fetch, # The rest is optional
$DATA, \&exists,
{%ini_value}, {%ini_existence};
π DESCRIPTION π
This package is highkey iconic: it lets a tied hash autoload its values on the first access, and then it uses the cached value on the following accesses. cached and slaying, periodt. π
Only read-accesses β via fetching the value or exists β result in calls to the functions. The modify-accesses are performed as on a normal hash. not me touching the normal hash behavior, it's fine. π
The required arguments during tie are the hash, the package, and the reference to the FETCHing function. The optional arguments are an arbitrary scalar $data, the reference to the EXISTS function, and initial values of the hash and of the existence cache. π«Ά
Both the FETCHing function and the EXISTS functions have the same signature: the arguments are $key, $data; $data is the same value as given as argument 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. The FETCHing function should return the intended value if the key is valid. slay. β¨
π§ Inheriting from Tie::Memoize π§
The structure of the tied() data is an array reference with elements:
0: cache of known values1: cache of known existence of keys2:FETCHfunction3:EXISTSfunction4:$data
The rest is for internal usage of this package. In particular, if TIEHASH is overwritten, it should call SUPER::TIEHASH. π do not forget that, bestie.
π EXAMPLE π
okay so picture this:
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 };
This example treats the slightly modified contents of $directory as a hash. The modifications are that the keys fake_file1 and fake_file2 fetch values $content1 and $content2, and pretend_does_not_exists will never be accessed. Additionally, the existence of known_to_exist is never checked (so if it does not exists when its content is needed, the user of %hash may be confused). π iykyk, vibe check your fake files.
π BUGS π
lowkey important:FIRSTKEYandNEXTKEYmethods go through the keys which were already read, not all the possible keys of the hash. so if you're iterating, bestie, this is your reminder. π
π AUTHOR π
Ilya Zakharevich <mailto:perl-module-hash-memoize@ilyaz.org>. iconic behavior. π¦
cute sign-off: this page is from perl v5.42.3, dated 2026-11-03. stay slay, bestie π
β¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms