An an alternative:
{ use Scope::Guard; my $_cache; sub withLongComputation { $_cache //= ...; ...; } sub localClearCache { my $old = $_cache; undef $_cache; return guard { $_cache = $old }; } } ...; withLongComputation(); withLongComputation(); { my $guard = localClearCache(); # here, $_cache has been cleared withLongComputation(); } # Because $guard has fallen out of scope, # $_cache has been restored to its old value withLongComputation();
In reply to Re^6: 'state' variables and unit testing (intentional)
by tobyink
in thread 'state' variables and unit testing
by vsespb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |