sub foo { my $arg = shift @_; state %cache; return $cache{$arg} if exists $cache{$arg}; return $cache{$arg} = something_costly( $arg ); }