in reply to Trying to understand closures
#!/usr/bin/perl
foo();
bar();
for (1..3) {
my $x;
sub foo { print ++$x, "f\n" }
sub bar { print ++$x, "b\n" }
print ++$x, "m\n";
}
foo();
bar();
resulting in
1f
2b
3m
1m
1m
4f
5b
foo and bar hold the first incarnation of x in the loop. The second and third incarnation of my $x is initialised with 0 so you get the 1m 1m lines in the midd.
|
|---|