will(would in scalar context, you can have the normalizer function selectits behavior based on the results of C<wantarray>. Even if called ina list context, a normalizer should still return a single string.=head2 C<SCALAR_CACHE>, C<LIST_CACHE>Normally, C<Memoize> caches your function's return values into anordinary Perl hash variable. However, you might like to have thevalues cached on the disk, so that they persist from one run of yourprogram to the next, or you might like to associate some otherinteresting semantics with the cached values.There's a slight complication under the hood of C<Memoize>: There areactually I<two> caches, one for scalar values and one for list values.When your function is called in scalar context, its return value iscached in one hash, and when your function is called in list context,its value is cached in the other hash. You can control the cachingbehavior of both contexts independently with these options.The argument to C<LIST_CACHE> or C<SCALAR_CACHE> must either be one ofthe following four strings:MEMORYFAULTMERGEHASHor else it must be a reference to a list whose first element is one ofthese four strings, such as C<[HASH, arguments...]>.=over 4=item C<MEMORY>C<MEMORY> means that return values from the function will be cached inan ordinary Perl hash variable. The hash variable will not persistafter the program exits. This is the default.=item C<HASH>C<HASH> allows you to specify that a particular hash that you supplywill be used as the cache. You can tie this hash beforehand to giveit any behavior you want.A tied hash can have any semantics at all. It is typically tied to anon-disk database, so that cached values are stored in the database andretrieved from it again when needed, and the disk file typicallypersists after your program has exited. See C<perltie> for morecomplete details about C<tie>.A typical example is:use DB_File;tie my %cache => 'DB_File', $filename, O_RDWR|O_CREAT, 0666;memoize 'function', SCALAR_CACHE => [HASH => \%cache];This has the effect of storing the cache in a C<DB_File> databasewhose name is in C<$filename>. The cache will persist after theprogram has exited. Next time the program runs, it will find thecache already populated from the previous run of the program. Or youcan forcibly populate the cache by constructing a batch program thatruns in the background and populates the cache file. Then when youcome to run your real program the memoized function will be fastbecause all its results have been precomputed.=item C<TIE>This option is no longer supported. It is still documented only toaid in the debugging of old programs that use it. Old programs shouldbe converted to use the C<HASH> option instead.memoize ... [TIE, PACKAGE, ARGS...]is merely a shortcut forrequire PACKAGE;{ my %cache;tie %cache, PACKAGE, ARGS...;}memoize ... [HASH => \%cache];=item C<FAULT>C<FAULT> means that you never expect to call the function in scalaror list)
X-Ref
|