in reply to Inner subroutines?
Named subroutine definitions are global--no matter where you put them in your code. On the other hand, anonymous subroutines (i.e. closures) can see the variables in the scope in which they are defined:
my @coderefs; for my $num (10, 20, 30) { my $ref = sub {print "$num\n"}; push @coderefs, $ref; } #Note: $num has ceased to exist here... #...yet the following subs can still see the value of #$num at the time they were created: $coderefs[0]->(); $coderefs[2]->(); --output:-- 10 30
|
|---|