| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Tie::Memoize 3perl" |
| 58 |
.TH Tie::Memoize 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Tie::Memoize \- add data to hash when needed |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 5 |
| 68 |
\& require Tie::Memoize; |
| 69 |
\& tie %hash, \*(AqTie::Memoize\*(Aq, |
| 70 |
\& \e&fetch, # The rest is optional |
| 71 |
\& $DATA, \e&exists, |
| 72 |
\& {%ini_value}, {%ini_existence}; |
| 73 |
.Ve |
| 74 |
.SH DESCRIPTION |
| 75 |
.IX Header "DESCRIPTION" |
| 76 |
This package allows a tied hash to autoload its values on the first access, |
| 77 |
and to use the cached value on the following accesses. |
| 78 |
.PP |
| 79 |
Only read-accesses (via fetching the value or \f(CW\*(C`exists\*(C'\fR) result in calls to |
| 80 |
the functions; the modify-accesses are performed as on a normal hash. |
| 81 |
.PP |
| 82 |
The required arguments during \f(CW\*(C`tie\*(C'\fR are the hash, the package, and |
| 83 |
the reference to the \f(CW\*(C`FETCH\*(C'\fRing function. The optional arguments are |
| 84 |
an arbitrary scalar \f(CW$data\fR, the reference to the \f(CW\*(C`EXISTS\*(C'\fR function, |
| 85 |
and initial values of the hash and of the existence cache. |
| 86 |
.PP |
| 87 |
Both the \f(CW\*(C`FETCH\*(C'\fRing function and the \f(CW\*(C`EXISTS\*(C'\fR functions have the |
| 88 |
same signature: the arguments are \f(CW\*(C`$key, $data\*(C'\fR; \f(CW$data\fR is the same |
| 89 |
value as given as argument during \fBtie()\fRing. Both functions should |
| 90 |
return an empty list if the value does not exist. If \f(CW\*(C`EXISTS\*(C'\fR |
| 91 |
function is different from the \f(CW\*(C`FETCH\*(C'\fRing function, it should return |
| 92 |
a TRUE value on success. The \f(CW\*(C`FETCH\*(C'\fRing function should return the |
| 93 |
intended value if the key is valid. |
| 94 |
.SH "Inheriting from \fBTie::Memoize\fP" |
| 95 |
.IX Header "Inheriting from Tie::Memoize" |
| 96 |
The structure of the \fBtied()\fR data is an array reference with elements |
| 97 |
.PP |
| 98 |
.Vb 5 |
| 99 |
\& 0: cache of known values |
| 100 |
\& 1: cache of known existence of keys |
| 101 |
\& 2: FETCH function |
| 102 |
\& 3: EXISTS function |
| 103 |
\& 4: $data |
| 104 |
.Ve |
| 105 |
.PP |
| 106 |
The rest is for internal usage of this package. In particular, if |
| 107 |
TIEHASH is overwritten, it should call SUPER::TIEHASH. |
| 108 |
.SH EXAMPLE |
| 109 |
.IX Header "EXAMPLE" |
| 110 |
.Vb 6 |
| 111 |
\& sub slurp { |
| 112 |
\& my ($key, $dir) = shift; |
| 113 |
\& open my $h, \*(Aq<\*(Aq, "$dir/$key" or return; |
| 114 |
\& local $/; <$h> # slurp it all |
| 115 |
\& } |
| 116 |
\& sub exists { my ($key, $dir) = shift; return \-f "$dir/$key" } |
| 117 |
\& |
| 118 |
\& tie %hash, \*(AqTie::Memoize\*(Aq, \e&slurp, $directory, \e&exists, |
| 119 |
\& { fake_file1 => $content1, fake_file2 => $content2 }, |
| 120 |
\& { pretend_does_not_exists => 0, known_to_exist => 1 }; |
| 121 |
.Ve |
| 122 |
.PP |
| 123 |
This example treats the slightly modified contents of \f(CW$directory\fR as a |
| 124 |
hash. The modifications are that the keys \fIfake_file1\fR and |
| 125 |
\&\fIfake_file2\fR fetch values \f(CW$content1\fR and \f(CW$content2\fR, and |
| 126 |
\&\fIpretend_does_not_exists\fR will never be accessed. Additionally, the |
| 127 |
existence of \fIknown_to_exist\fR is never checked (so if it does not |
| 128 |
exists when its content is needed, the user of \f(CW%hash\fR may be confused). |
| 129 |
.SH BUGS |
| 130 |
.IX Header "BUGS" |
| 131 |
FIRSTKEY and NEXTKEY methods go through the keys which were already read, |
| 132 |
not all the possible keys of the hash. |
| 133 |
.SH AUTHOR |
| 134 |
.IX Header "AUTHOR" |
| 135 |
Ilya Zakharevich <mailto:perl\-module\-hash\[email protected]>. |