Randal L. Schwartz wrote:
> I prefer a pattern that looks more like this:
> 
> sub expensive_to_calculate {
> 
>   my $self = shift;
>   my $input = shift; # cache on this scalar
> 
>   our %cache_for_expensive_to_calculate;
>   return $cache_for_expensive_to_calculate{$input} ||= do {
>     expensive calculation here;
>     more things based on $input;
>     last expression is value;
>   };
> 
> }
> 
> Yes, in 5.10, this can be simplified, but I find 5.8 to still be
> the dominant Perl.
> 
> This also presumes that a "false" value is never needed to be cached.

Out of curiosity, if it's gone this far, is it fair for me to recommend
reading Mr. Dominus' Memoizing chapter in Higher-Order Perl
(http://hop.perl.plover.com/)?

Fantastic reading regarding caching. Fantastic book overall. Note also
that the author (Mark Jason Dominus) now has the entire book on the
website as a free PDF download.

It is a mind-twisting read, from front to end.

I will never, ever forget a particular footnote statement (it makes me
smile every time I think about it):

"I sometimes enjoy the mind-bending exercise of imagining the result of
memoizing the Unix fork() function." (Chapter 3, page 80).

Steve

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to