1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#!/usr/bin/perl use strict; use warnings; package my_package; sub new { my $class = shift; my $obj = { HASH => { "key1" => 35, "key2" => 28, "key3" => 74, }, }; return bless $obj, $class; } sub outer_subroutine { my $obj = shift; sub sort_by_value { $obj->{"HASH"}{$b} <=> $obj->{"HASH"}{$a}; } foreach my $key ( sort sort_by_value(keys $obj->{"HASH"} ) ) { print $key, "\n"; } } package main; my $object = my_package->new; $object->outer_subroutine;
1 2 3 4 5
my $mysort = sub { return $a <=> $b; }; print sort $mysort (1, 2, 3, 2, 1);
1 2 3 4
my $hash = $obj->{HASH} || {}; foreach my $key ( sort {$hash->{$b} <=> $hash->{$a} } keys %{$hash} ) { print $key, "\n"; }