One of the first things I try to learn in an unfamiliar programming language is how it handles closures. Their semantics are often intertwined with how the language handles scop
$_ is a global variable and should not be used in closure. Before using it assign this to a lexically scoped variable as shown bewlow. This will produce expected o/p.
#!/usr/bin/perl -w
use strict;
my @closures;
foreach (1..3) {
my $var = $_;
push @closures, sub { print "I will remember $var"; };
}
foreach (@closures) {
$_->();
print "\n";
}