Package MyObject; sub new { my $self={ hash_ref=>{key1=>'value1',key2=>'value2'} }; bless $self; return $self; } #global variable to access $self my $sort_self; sub byvalue { $sort_self->{hash_ref}->{$a}<=>$sort_self->{hash_ref}->{$b}; } sub sort_hash_ref_keys_by_value { my $self=shift; #set global variable by hand $sort_self=$self; #now the cmp function knows the object my @keys_sorted_by_value=sort byvalue keys(%{$self->{hash_ref}}); } Packahe Main; my $myobject=new MyObject; $myobject->sort_hash_ref_keys_by_value;