sub foo { my $arg = shift @_; state %cache; return $cache{$arg} if exists $cache{$arg}; return $cache{$arg} = something_costly( $arg ); }
Test code:
use Immutable::UntestableCode qw( foo ); my $real_thing = \&Immutable::UntestableCode::something_costly; my $calls = 0; *Immutable::UntestableCode::something_costly = sub { $calls++; return $real_thing->( @_ ); }; foo( 1 ); is( 1, $calls ); foo( 1 ); is( 1, $calls );
- tye
In reply to Re: How to test caching? (simple)
by tye
in thread How to test caching?
by JavaFan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |