use Storable qw[ freeze ]; my %cache; sub memoised { my $href = shift; my $cacheKey = pack 'NN/a*', scalar keys %$href, freeze $href; return $cache{ $cacheKey } if exists $cache{ $cacheKey }; ## calculate value for href return $cache{ $cacheKey } = $value; }