in reply to Re: A perl buffer
in thread A perl buffer

It's certainly cool that there's a module for this purpose, however in some cases you can simply write:

my %cache; my $result = $cache{$input} ||= function($input); print $result;

Replies are listed 'Best First'.
Re^3: A perl buffer
by Corion (Patriarch) on Aug 10, 2008 at 16:12 UTC

    ... which fails when function($input) returns the empty string, 0 or undef. These values are often used when the most expensive search came up fruitless :).

      I knew that someone would find this loophole ;)

      Well, it would not exactly "fail", it would just ignore the cache. Perl 5.10 has help for us:

      my $result = $cache{$input} //= function($input);

      Of course, there might still be the case where the function returns undef or a list or depends on the context. Before conjuring up a perfect solution that honours all special cases, I'd better let you use Memoize as recommended above :)

        You can use exists. Of course, you still have to know the context you're being called in...