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