in reply to How to test caching?
Just another idea...
use constant TEST_NOCACHE => $ENV{TEST_NOCACHE}; sub foo ($) { my $arg = shift; state %cache; return $cache{$arg} if !TEST_NOCACHE && exists $cache{$arg}; my $result = ... something costly ...; $cache{$arg} = $result; }
Thanks to constant folding, the above compiles identically as the original code under normal circumstances, and all effects of the cache are removed when the flag is true.
The effect of the cache can be observed by finding the difference between runs of
TEST_NOCACHE=0 cache.t
and
TEST_NOCACHE=1 cache.t
|
|---|