1 package Memoize::Saves;
9 my ($package, %args) = @_;
10 my $cache = $args{HASH} || {};
12 # Convert the CACHE to a referenced hash for quick lookup
17 $args{CACHE} = [ $args{CACHE} ] unless ref $args{CACHE} eq "ARRAY";
18 foreach my $value ( @{$args{CACHE}} )
22 $args{CACHE} = \%hash;
25 # Convert the DUMP list to a referenced hash for quick lookup
30 $args{DUMP} = [ $args{DUMP} ] unless ref $args{DUMP} eq "ARRAY";
31 foreach my $value ( @{$args{DUMP}} )
40 my ($module, @opts) = @{$args{TIE}};
41 my $modulefile = $module . '.pm';
42 $modulefile =~ s{::}{/}g;
43 eval { require $modulefile };
45 die "Memoize::Saves: Couldn't load hash tie module `$module': $@; aborting";
47 my $rc = (tie %$cache => $module, @opts);
49 die "Memoize::Saves: Couldn't tie hash to `$module': $@; aborting";
54 bless \%args => $package;
62 if( exists $self->{C}->{$key} )
76 return $self->{C}->{$key};
85 # If CACHE defined and this is not in our list don't save it
87 if(( defined $self->{CACHE} )&&
88 ( ! defined $self->{CACHE}->{$value} ))
90 print "$value not in CACHE list.\n" if $DEBUG;
94 # If DUMP is defined and this is in our list don't save it
96 if(( defined $self->{DUMP} )&&
97 ( defined $self->{DUMP}->{$value} ))
99 print "$value in DUMP list.\n" if $DEBUG;
103 # If REGEX is defined we will store it only if its true
105 if(( defined $self->{REGEX} )&&
106 ( $value !~ /$self->{REGEX}/ ))
108 print "$value did not match regex.\n" if $DEBUG;
112 # If we get this far we should save the value
114 print "Saving $key:$value\n" if $DEBUG;
115 $self->{C}->{$key} = $value;
125 Memoize::Saves - Plug-in module to specify which return values should be memoized
132 SCALAR_CACHE => [TIE, Memoize::Saves,
133 CACHE => [ "word1", "word2" ],
134 DUMP => [ "word3", "word4" ],
135 REGEX => "Regular Expression",
136 HASH => $cache_hashref,
141 Memoize::Saves is a plug-in module for Memoize. It allows the
142 user to specify which values should be cached or which should be
143 dumped. Please read the manual for Memoize for background
146 Use the CACHE option to specify a list of return values which should
147 be memoized. All other values will need to be recomputed each time.
149 Use the DUMP option to specify a list of return values which should
150 not be memoized. Only these values will need to be recomputed each
153 Use the REGEX option to specify a Regular Expression which must match
154 for the return value to be saved. You can supply either a plain text
155 string or a compiled regular expression using qr//. Obviously the
156 second method is prefered.
158 Specifying multiple options will result in the least common denominator
161 You can use the HASH option to string multiple Memoize Plug-ins together:
163 tie my %disk_hash => 'GDBM_File', $filename, O_RDWR|O_CREAT, 0666;
164 tie my %expiring_cache => 'Memoize::Expire',
165 LIFETIME => 5, HASH => \%disk_cache;
166 tie my %cache => 'Memoize::Saves',
167 REGEX => qr/my/, HASH => \%expiring_cache;
169 memoize ('printme', SCALAR_CACHE => [HASH => \%cache]);
173 This module is experimental, and may contain bugs. Please report bugs
174 to C<mjd-perl-memoize+@plover.com>.
176 If you are going to use Memoize::Saves with Memoize::Expire it is
177 important to use it in that order. Memoize::Expire changes the return
178 value to include expire information and it may no longer match
179 your CACHE, DUMP, or REGEX.
184 Joshua Gerth <gerth@teleport.com>