{ 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();