my $current_href = {}; sub byValue { $current_href->{$a} cmp $current_href->{$a}; } # ... somewhere in your program $current_href = \%myHash;