sub foo ($) { my $arg = shift; state %cache; if exists $cache{$arg} { print "Got that from the cache!\n"; return $cache{$arg} } print "Didn't find that in cache...\n"; my $result = ... something costly ...; $cache{$arg} = $result; return $result; }