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