in reply to reusable hash value sort

You could use a closure:
use Carp; sub make_by_value_sorter { my ($hash, $op) = @_; return sub ($$) { $hash->{$_[0]} <=> $hash->{$_[1]} } if $op eq '<=>'; return sub ($$) { $hash->{$_[0]} cmp $hash->{$_[1]} } if $op eq 'cmp'; croak("Unknown sort op '$op'"); } my $hash = { bar => 1, foo => 2, baz => 3, fah => 4, }; my $by_value = make_by_value_sorter($hash, '<=>'); my @a = sort $by_value keys %$hash; # Simple case my @b = sort { substr($a, 0, 1) cmp substr($b, 0, 1) or $by_value->($a, $b) # As part of another sort sub } keys %$hash;
Cheers,
-Anomo