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
In reply to Re: Inner subroutines?
by 7stud
in thread Inner subroutines?
by wsppan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |