line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: Memo.pm 1.11 Wed, 10 Dec 1997 17:58:09 -0500 jesse $ |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Devel::Memo; |
4
|
|
|
|
|
|
|
require 5.004; |
5
|
1
|
|
|
1
|
|
2932
|
use FreezeThaw qw(safeFreeze); |
|
1
|
|
|
|
|
9885
|
|
|
1
|
|
|
|
|
463
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new($$@) { |
8
|
0
|
|
|
0
|
0
|
|
my ($class, $subr, @styles)=@_; |
9
|
0
|
|
|
|
|
|
my %cache; |
10
|
0
|
|
|
|
|
|
my $proto=prototype $subr; |
11
|
0
|
0
|
|
|
|
|
$proto="($proto)" if defined $proto; |
12
|
0
|
|
|
|
|
|
bless eval qq{ |
13
|
|
|
|
|
|
|
sub $proto {$class->_exec(\$subr, \\\@styles, \\%cache, [\@_])} |
14
|
|
|
|
|
|
|
}, $class; |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _exec($$$$;) { |
18
|
0
|
|
|
0
|
|
|
my ($class, $subr, $styles, $cache, $args)=@_; |
19
|
0
|
|
|
|
|
|
my @styles=@$styles; |
20
|
0
|
|
|
|
|
|
my @virtargs=@$args; |
21
|
0
|
0
|
|
|
|
|
if ($styles[-1] eq '-rest') { |
22
|
0
|
|
|
|
|
|
$styles[-1]='-equal'; |
23
|
0
|
|
|
|
|
|
my $rest=[splice @virtargs, $#styles]; |
24
|
0
|
|
|
|
|
|
push @virtargs, $rest; |
25
|
|
|
|
|
|
|
} |
26
|
0
|
0
|
|
|
|
|
die "Bad matchup of arguments: @{[scalar @virtargs]} vs. @{[scalar @styles]}" |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
unless @styles==@virtargs; |
28
|
0
|
|
|
|
|
|
my $i; for ($i=0; $i<@virtargs; $i++) { |
|
0
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
$virtargs[$i]=safeFreeze($virtargs[$i]) if $styles[$i] eq '-equal'; |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
|
my $key=join '', map {length($_) . ":$_"} @virtargs; |
|
0
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $val=$cache->{$key}; |
33
|
0
|
0
|
|
|
|
|
$val=$cache->{$key}=[&$subr(@$args)] unless defined $val; |
34
|
0
|
0
|
|
|
|
|
wantarray ? @$val : $val->[-1]; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
1; |
38
|
|
|
|
|
|
|
__END__ |