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