my (%withlex, %withglobal); for (1 .. 10) { my $ref = sub { return $_ }; $withglobal{$ref} = $ref; print $ref->(), "\n"; } print keys %withglobal; print "\n"; $_ = 'foo'; foreach my $key ( keys %withglobal ) { print "k: $key\tv: ", &{ $withglobal{$key} }(), "\n"; } for my $i (1 .. 10) { my $ref = sub { return $i }; $withlex{$ref} = $ref; print $ref->(), "\n"; } print sort { $withlex{$a} <=> $withlex{$b} } keys %withlex; print "\n"; my $i = 'bar'; foreach my $key ( sort { $withlex{$a} <=> $withlex{$b} } keys %withlex ) { print "k: $key\tv: ", &{ $withlex{$key} }(), "\n"; } print "Using lex num of subs is " . (keys %withlex) . "\n"; print "Using global num of subs is " . (keys %withglobal) . "\n";