use warnings; use 5.010; for my $num (1 .. 5) { my $a = $num; {say "in block: $a"}; sub test { say "in function: $a"; } test() } --output:-- in block: 1 in function: 1 in block: 2 in function: 1 in block: 3 in function: 1 in block: 4 in function: 1 in block: 5 in function: 1 #### use strict; use warnings; use 5.010; my %h1 = ( 'a' => 5, 'b' => 8, 'c' => 1 ); my %h2 = ( 'a' => 200, 'b' => 150, 'c' => 100 ); my @AoH = (\%h1, \%h2); for my $href (@AoH) { my %hash = %$href; my @sorted_keys = sort by_val keys %hash; sub by_val { $hash{$a} <=> $hash{$b} }; for my $key (@sorted_keys) { say "$key = $hash{$key}"; } say "=" x 20; } --output:-- c = 1 a = 5 b = 8 #ok, the first hash is sorted perfectly. ==================== c = 100 a = 200 b = 150 #but what happened here? ==================== #### use strict; use warnings; use 5.010; for my $num (1 .. 5) { say "start of for loop: $num"; {say "in block: $num"}; sub test { say "in function: $num"; } test() } --output:-- start of for loop: 1 in block: 1 Use of uninitialized value $num in concatenation (.) or string at 3perl.pl line 11. in function: start of for loop: 2 in block: 2 Use of uninitialized value $num in concatenation (.) or string at 3perl.pl line 11. in function: .. ..