1 package Memoize::Saves;
7 my ($package, %args) = @_;
10 # Convert the CACHE to a referenced hash for quick lookup
15 $args{CACHE} = [ $args{CACHE} ] unless ref $args{CACHE} eq "ARRAY";
16 foreach my $value ( @{$args{CACHE}} )
20 $args{CACHE} = \%hash;
23 # Convert the DUMP list to a referenced hash for quick lookup
28 $args{DUMP} = [ $args{DUMP} ] unless ref $args{DUMP} eq "ARRAY";
29 foreach my $value ( @{$args{DUMP}} )
38 my ($module, @opts) = @{$args{TIE}};
39 my $modulefile = $module . '.pm';
40 $modulefile =~ s{::}{/}g;
41 eval { require $modulefile };
43 die "Memoize::Saves: Couldn't load hash tie module `$module': $@; aborting";
45 my $rc = (tie %cache => $module, @opts);
47 die "Memoize::Saves: Couldn't tie hash to `$module': $@; aborting";
52 bless \%args => $package;
60 if( exists $self->{C}->{$key} )
74 return $self->{C}->{$key};
83 # If CACHE defined and this is not in our list don't save it
85 if(( defined $self->{CACHE} )&&
86 ( ! defined $self->{CACHE}->{$value} ))
88 print "$value not in CACHE list.\n" if $DEBUG;
92 # If DUMP is defined and this is in our list don't save it
94 if(( defined $self->{DUMP} )&&
95 ( defined $self->{DUMP}->{$value} ))
97 print "$value in DUMP list.\n" if $DEBUG;
101 # If REGEX is defined we will store it only if its true
103 if(( defined $self->{REGEX} )&&
104 ( $value !~ /$self->{REGEX}/ ))
106 print "$value did not match regex.\n" if $DEBUG;
110 # If we get this far we should save the value
112 print "Saving $key:$value\n" if $DEBUG;
113 $self->{C}->{$key} = $value;
123 Memoize::Saves - Plug-in module to specify which return values should be memoized
130 SCALAR_CACHE => [TIE, Memoize::Saves,
131 CACHE => [ "word1", "word2" ],
132 DUMP => [ "word3", "word4" ],
133 REGEX => "Regular Expression",
134 TIE => [Module, args...],
139 Memoize::Saves is a plug-in module for Memoize. It allows the
140 user to specify which values should be cached or which should be
141 dumped. Please read the manual for Memoize for background
144 Use the CACHE option to specify a list of return values which should
145 be memoized. All other values will need to be recomputed each time.
147 Use the DUMP option to specify a list of return values which should
148 not be memoized. Only these values will need to be recomputed each
151 Use the REGEX option to specify a Regular Expression which must match
152 for the return value to be saved. You can supply either a plain text
153 string or a compiled regular expression using qr//. Obviously the
154 second method is prefered.
156 Specifying multiple options will result in the least common denominator
159 You can use the TIE option to string multiple Memoize Plug-ins together:
164 [TIE, Memoize::Saves,
166 TIE => [Memoize::Expire,
168 TIE => [ GDBM_File, $filename,
169 O_RDWR | O_CREAT, 0666]
177 This module is experimental, and may contain bugs. Please report bugs
178 to the address below.
180 If you are going to use Memoize::Saves with Memoize::Expire it is
181 import to use it in that order. Memoize::Expire changes the return
182 value to include expire information and it may no longer match
183 your CACHE, DUMP, or REGEX.
188 Joshua Gerth <gerth@teleport.com>
194 The Memoize man page.