Memoize.pm

filter: Statementstmt 94.9% 149 / 157 Branchbran 87.5% 70 / 80 Conditioncond 81.4% 22 / 27 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 95.4% 21 / 22 Podpod 66.6% 2 / 3 totaltotal 90.1% 275 / 305 CC 53 SCAR 25.9
CC
53
Cov
91%
memoize
27.1
__ANON__
10.0
flush_cache
9.8
CRAP 13.3
↑ Summary Memoize/Expire.pm »
1 # -*- mode: perl; perl-indent-level: 2; -*-
2 # vim: ts=8 sw=2 sts=2 noexpandtab
3  
4 # Memoize.pm
5 #
6 # Copyright 1998, 1999, 2000, 2001, 2012 M. J. Dominus.
7 # You may copy and distribute this program under the
8 # same terms as Perl itself.
9  
10 16 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Memoize;
13 16 our $VERSION = '1.17';
14  
15 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use Scalar::Util 1.11 (); # for set_prototype
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 16 ▶BEGIN { require Exporter; *import = \&Exporter::import }
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 our @EXPORT = qw(memoize);
20 16 our @EXPORT_OK = qw(unmemoize flush_cache);
21  
22 16 my %memotable;
23  
24 0 ▶sub CLONE {
Subroutine CLONE: not called CC=1 SCAR=6.9
25 0 my @info = values %memotable;
26 0 %memotable = map +($_->{WRAPPER} => $_), @info;
27 }
28  
29 73 ▶sub memoize {
Subroutine memoize: called CC=27 SCAR=33.0
Pod undocumented
30 73 my $fn = shift;
31 73 my %options = @_;
32  
33 73 ▶ unless (defined($fn) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fn and ref $fn eq "CODE" || ref $fn eq "") 3 70 73
Condition83%5/6
defined $fn and ref $fn eq "CODE" || ref $fn eq ""
!ll&&!rl&&r
0370
ref $fn eq "CODE" or ref $fn eq ""
l!l&&r!l&&!r
25453
MC/DC66%2/3
defined $fn and ref $fn eq "CODE" || ref $fn eq ""
defined $fn ref $fn eq "CODE" ref $fn eq ""
Truth table75%3/4
A and B or C
A= defined $fnB= ref $fn eq "CODE"C= ref $fn eq ""
ABCresult
0XX0
1000
11X1
1011
34 (ref $fn eq 'CODE' || ref $fn eq '')) {
35 3 croak "Usage: memoize 'functionname'|coderef {OPTIONS}";
36 }
37  
38 70 my $uppack = caller; # TCL me Elmo!
39 70 ▶ my $name = (ref $fn ? undef : $fn);
Branch: 1 branch
BranchTrueFalseTotal
ref $fn ? : 25 45 70
40 70 my $cref = _make_cref($fn, $uppack);
41  
42 69 my $normalizer = $options{NORMALIZER};
43 69 ▶ if (defined $normalizer && ! ref $normalizer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $normalizer and not ref $normalizer) 6 63 69
Condition100%3/3
defined $normalizer and not ref $normalizer
!ll&&!rl&&r
6036
MC/DC100%2/2
defined $normalizer and not ref $normalizer
defined $normalizer not ref $normalizer
Truth table100%3/3
A and not(B)
A= defined $normalizerB= not ref $normalizer
ABresult
0X0
100
111
44 6 $normalizer = _make_cref($normalizer, $uppack);
45 }
46  
47 my $install_name = exists $options{INSTALL}
48 ? $options{INSTALL} # use given name (or, if undef: do not install)
49 69 ▶ : $name; # no INSTALL option provided: default to original name if possible
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'INSTALL'} ? : 14 55 69
50  
51 69 ▶ if (defined $install_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $install_name) 42 27 69
52 42 ▶ $install_name = $uppack . '::' . $install_name
Branch: 1 branch
BranchTrueFalseTotal
unless $install_name =~ /::/ 41 1 42
53 unless $install_name =~ /::/;
54 }
55  
56 # convert LIST_CACHE => MERGE to SCALAR_CACHE => MERGE
57 # to ensure TIE/HASH will always be checked by _check_suitable
58 69 ▶ if (($options{LIST_CACHE} || '') eq 'MERGE') {
Branch: 1 branch
BranchTrueFalseTotal
if (($options{"LIST_CACHE"} || "") eq "MERGE") 6 63 69
Condition100%2/2
$options{'LIST_CACHE'} || ''
l!l
3336
MC/DC100%1/1
$options{'LIST_CACHE'} || ''
$options{'LIST_CACHE'}
Truth table100%2/2
A
A= $options{'LIST_CACHE'}
Aresult
11
00
59 6 $options{LIST_CACHE} = $options{SCALAR_CACHE};
60 6 $options{SCALAR_CACHE} = 'MERGE';
61 }
62  
63 # These will be the caches
64 69 my %caches;
65 69 for my $context (qw(LIST SCALAR)) { # SCALAR_CACHE must be last, to process MERGE
66 130 ▶ my $fullopt = $options{"${context}_CACHE"} ||= 'MEMORY';
Condition100%2/2
$options{"${context}_CACHE"} ||= "MEMORY"
l!l
5674
MC/DC100%1/1
$options{"${context}_CACHE"} ||= "MEMORY"
$options{"${context}_CACHE"}
Truth table100%2/2
A
A= $options{"${context}_CACHE"}
Aresult
11
00
67 130 ▶ my ($cache_opt, @cache_opt_args) = ref $fullopt ? @$fullopt : $fullopt;
Branch: 1 branch
BranchTrueFalseTotal
ref $fullopt ? : 29 101 130
68 130 ▶ if ($cache_opt eq 'FAULT') { # no cache
Branch: 5 branches
BranchTrueFalseTotal
if ($cache_opt eq 'FAULT') { } 18 112 130
elsif ($cache_opt eq 'HASH') { } 18 94 112
elsif ($cache_opt eq 'TIE') { } 8 86 94
elsif ($cache_opt eq 'MEMORY') { } 74 12 86
elsif ($cache_opt eq 'MERGE' and not ref $fullopt) { } 8 4 12
69 18 $caches{$context} = undef;
70 } elsif ($cache_opt eq 'HASH') { # user-supplied hash
71 18 my $cache = $cache_opt_args[0];
72 18 _check_suitable($context, ref tied %$cache);
73 16 $caches{$context} = $cache;
74 } elsif ($cache_opt eq 'TIE') {
75 8 ▶ carp("TIE option to memoize() is deprecated; use HASH instead")
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("all") 1 7 8
76 if warnings::enabled('all');
77 8 ▶ my $module = shift(@cache_opt_args) || '';
Condition50%1/2
shift @cache_opt_args || ''
l!l
80
MC/DC0%0/1
shift @cache_opt_args || ''
shift @cache_opt_args
Truth table50%1/2
A
A= shift @cache_opt_args
Aresult
11
00
78 8 _check_suitable($context, $module);
79 5 my $hash = $caches{$context} = {};
80 5 (my $modulefile = $module . '.pm') =~ s{::}{/}g;
81 5 require $modulefile;
82 4 ▶ tie(%$hash, $module, @cache_opt_args)
Branch: 1 branch
BranchTrueFalseTotal
tie %$hash, $module, @cache_opt_args or croak("Couldn't tie memoize hash to `${module}': $!") 4 0 4
83 or croak "Couldn't tie memoize hash to `$module': $!";
84 } elsif ($cache_opt eq 'MEMORY') {
85 74 $caches{$context} = {};
86 ▶ } elsif ($cache_opt eq 'MERGE' and not ref $fullopt) { # ['MERGE'] was never supported
Condition100%3/3
$cache_opt eq "MERGE" and not ref $fullopt
!ll&&!rl&&r
228
MC/DC100%2/2
$cache_opt eq "MERGE" and not ref $fullopt
$cache_opt eq "MERGE" not ref $fullopt
Truth table100%3/3
A and not(B)
A= $cache_opt eq "MERGE"B= not ref $fullopt
ABresult
0X0
100
111
87 8 ▶ die "cannot MERGE $context\_CACHE" if $context ne 'SCALAR'; # should never happen
Branch: 1 branch
BranchTrueFalseTotal
if $context ne "SCALAR" 0 8 8
88 8 ▶ die 'bad cache setup order' if not exists $caches{LIST}; # should never happen
Branch: 1 branch
BranchTrueFalseTotal
unless exists $caches{"LIST"} 0 8 8
89 8 $options{MERGED} = 1;
90 8 $caches{SCALAR} = $caches{LIST};
91 } else {
92 4 croak "Unrecognized option to `${context}_CACHE': `$cache_opt' should be one of (MERGE TIE MEMORY FAULT HASH)";
93 }
94 }
95  
96 59 my $wrapper = _wrap($install_name, $cref, $normalizer, $options{MERGED}, \%caches);
97  
98 59 ▶ if (defined $install_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $install_name) 42 17 59
99 16 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 16 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
101 42 *{$install_name} = $wrapper;
102 }
103  
104 $memotable{$wrapper} = {
105 L => $caches{LIST},
106 S => $caches{SCALAR},
107 59 U => $cref,
108 NAME => $install_name,
109 WRAPPER => $wrapper,
110 };
111  
112 59 $wrapper # Return just memoized version
113 }
114  
115 2 ▶sub flush_cache {
Subroutine flush_cache: called CC=6 SCAR=22.9
Pod documented
116 2 my $func = _make_cref($_[0], scalar caller);
117 2 my $info = $memotable{$func};
118 2 ▶ die "$func not memoized" unless defined $info;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info 0 2 2
119 2 for my $context (qw(S L)) {
120 4 my $cache = $info->{$context};
121 4 ▶ if (tied %$cache && ! (tied %$cache)->can('CLEAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied %$cache and not tied(%$cache)->can('CLEAR')) { } 0 4 4
Condition33%1/3
tied %$cache and not tied(%$cache)->can("CLEAR")
!ll&&!rl&&r
400
MC/DC0%0/2
tied %$cache and not tied(%$cache)->can("CLEAR")
tied %$cache not tied(%$cache)->can("CLEAR")
Truth table33%1/3
A and not(B)
A= tied %$cacheB= not tied(%$cache)->can("CLEAR")
ABresult
0X0
100
111
122 0 ▶ my $funcname = defined($info->{NAME}) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $info->{'NAME'} ? : 0 0 0
123 "function $info->{NAME}" : "anonymous function $func";
124 0 my $context = {S => 'scalar', L => 'list'}->{$context};
125 0 croak "Tied cache hash for $context-context $funcname does not support flushing";
126 } else {
127 4 %$cache = ();
128 }
129 }
130 }
131  
132 59 ▶sub _wrap {
Subroutine _wrap: called CC=1 SCAR=0.0
133 59 my ($name, $orig, $normalizer, $merged, $caches) = @_;
134 59 my ($cache_L, $cache_S) = @$caches{qw(LIST SCALAR)};
135 59 undef $caches; # keep the pad from keeping the hash alive forever
136 326 ▶ Scalar::Util::set_prototype(sub {
Subroutine __ANON__: called CC=10 SCAR=23.0
137 326 my $argstr = do {
138 16 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 326 ▶ defined $normalizer
Branch: 2 branches
BranchTrueFalseTotal
defined $normalizer ? : 26 300 326
wantarray ? : 21 5 26
140 ? ( wantarray ? ( $normalizer->( @_ ) )[0] : $normalizer->( @_ ) )
141 . '' # coerce undef to string while the warning is off
142 : join chr(28), @_;
143 };
144  
145 326 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 48 278 326
146 48 ▶ _crap_out($name, 'list') unless $cache_L;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_L 4 44 48
147 exists $cache_L->{$argstr} ? (
148 19 @{$cache_L->{$argstr}}
149 44 ▶ ) : do {
Branch: 1 branch
BranchTrueFalseTotal
exists $cache_L->{$argstr} ? : 19 25 44
150 16 ▶ my @q = do { no warnings 'recursion'; &$orig };
Subroutine BEGIN: called CC=1 SCAR=0.0
151 25 $cache_L->{$argstr} = \@q;
152 25 @q;
153 };
154 } else {
155 278 ▶ _crap_out($name, 'scalar') unless $cache_S;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache_S 4 274 278
156 exists $cache_S->{$argstr} ? (
157 $merged ? $cache_S->{$argstr}[0] : $cache_S->{$argstr}
158 274 ▶ ) : do {
Branch: 2 branches
BranchTrueFalseTotal
exists $cache_S->{$argstr} ? : 109 165 274
$merged ? : 3 106 109
159 16 ▶ my $val = do { no warnings 'recursion'; &$orig };
Subroutine BEGIN: called CC=1 SCAR=0.0
160 165 ▶ $cache_S->{$argstr} = $merged ? [$val] : $val;
Branch: 1 branch
BranchTrueFalseTotal
$merged ? : 4 161 165
161 165 $val;
162 };
163 }
164 59 }, prototype $orig);
165 }
166  
167 23 ▶sub unmemoize {
Subroutine unmemoize: called CC=4 SCAR=13.9
Pod documented
168 23 my $f = shift;
169 23 my $uppack = caller;
170 23 my $cref = _make_cref($f, $uppack);
171  
172 23 ▶ unless (exists $memotable{$cref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $memotable{$cref}) 2 21 23
173 2 croak "Could not unmemoize function `$f', because it was not memoized to begin with";
174 }
175  
176 21 my $tabent = $memotable{$cref};
177 21 ▶ unless (defined $tabent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tabent) 0 21 21
178 0 croak "Could not figure out how to unmemoize function `$f'";
179 }
180 21 my $name = $tabent->{NAME};
181 21 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 15 6 21
182 16 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 16 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
184 15 *{$name} = $tabent->{U}; # Replace with original function
185 }
186 21 delete $memotable{$cref};
187  
188 21 $tabent->{U};
189 }
190  
191 101 ▶sub _make_cref {
Subroutine _make_cref: called CC=7 SCAR=19.7
192 101 my $fn = shift;
193 101 my $uppack = shift;
194 101 my $cref;
195 my $name;
196  
197 101 ▶ if (ref $fn eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $fn eq 'CODE') { } 32 69 101
elsif (not ref $fn) { } 69 0 69
198 32 $cref = $fn;
199 } elsif (! ref $fn) {
200 69 ▶ if ($fn =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fn =~ /::/) { } 1 68 69
201 1 $name = $fn;
202 } else {
203 68 $name = $uppack . '::' . $fn;
204 }
205 16 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
206 69 ▶ if (defined $name and !defined(&$name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and not defined &$name) 1 68 69
Condition66%2/3
defined $name and not defined &$name
!ll&&!rl&&r
0681
MC/DC50%1/2
defined $name and not defined &$name
defined $name not defined &$name
Truth table66%2/3
A and not(B)
A= defined $nameB= not defined &$name
ABresult
0X0
100
111
207 1 croak "Cannot operate on nonexistent function `$fn'";
208 }
209 # $cref = \&$name;
210 68 $cref = *{$name}{CODE};
211 } else {
212 0 my $parent = (caller(1))[3]; # Function that called _make_cref
213 0 croak "Usage: argument 1 to `$parent' must be a function name or reference.\n";
214 }
215 100 ▶ our $DEBUG and warn "${name}($fn) => $cref in _make_cref\n";
Branch: 1 branch
BranchTrueFalseTotal
our $DEBUG and warn "$name($fn) => $cref in _make_cref\n" 0 100 100
216 100 $cref;
217 }
218  
219 8 ▶sub _crap_out {
Subroutine _crap_out: called CC=2 SCAR=6.9
220 8 my ($funcname, $context) = @_;
221 8 ▶ if (defined $funcname) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $funcname) { } 2 6 8
222 2 croak "Function `$funcname' called in forbidden $context context; faulting";
223 } else {
224 6 croak "Anonymous function called in forbidden $context context; faulting";
225 }
226 }
227  
228 # Raise an error if the user tries to specify one of these packages as a
229 # tie for LIST_CACHE
230 16 my %scalar_only = map {($_ => 1)} qw(DB_File GDBM_File SDBM_File ODBM_File), map +($_, "Memoize::$_"), qw(AnyDBM_File NDBM_File);
231 26 ▶sub _check_suitable {
Subroutine _check_suitable: called CC=3 SCAR=11.0
232 26 my ($context, $package) = @_;
233 croak "You can't use $package for LIST_CACHE because it can only store scalars"
234 26 ▶ if $context eq 'LIST' and $scalar_only{$package};
Branch: 1 branch
BranchTrueFalseTotal
if $context eq "LIST" and $scalar_only{$package} 5 21 26
Condition100%3/3
$context eq "LIST" and $scalar_only{$package}
!ll&&!rl&&r
1475
MC/DC100%2/2
$context eq "LIST" and $scalar_only{$package}
$context eq "LIST" $scalar_only{$package}
Truth table100%3/3
A and B
A= $context eq "LIST"B= $scalar_only{$package}
ABresult
0X0
100
111
235 }
236  
237 16 1;
238  
239 __END__
↑ Summary Memoize/Expire.pm »