my %maxIntesityCache; sub getIntensity { my $m = shift; # Do we know this already? return $maxIntesityCache{$m} if defined $maxIntesityCache{$m}; ...as for your function, except you can probably change the core to remove the exists test and/or one of the other suggestions in this thread... # Remember this for later $maxIntensityCache{$m} = $intensity; return $intensity; }