in reply to closure clarity, please
sub definitions are parsed once only. The closure retains the value it had at that point in time. Try this:
use warnings; use 5.010; for my $num (1 .. 5) { my $a = $num; {say "in block: $a"}; *test = sub { say "in function: $a"; }; test() } __END__ c:\test>junk11 in block: 1 in function: 1 in block: 2 Subroutine main::test redefined at C:\test\junk11.pl line 11. in function: 2 in block: 3 Subroutine main::test redefined at C:\test\junk11.pl line 11. in function: 3 in block: 4 Subroutine main::test redefined at C:\test\junk11.pl line 11. in function: 4 in block: 5 Subroutine main::test redefined at C:\test\junk11.pl line 11. in function: 5
You can disable the redefinition warning.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: closure clarity, please
by ikegami (Patriarch) on Nov 24, 2009 at 07:10 UTC | |
by BrowserUk (Patriarch) on Nov 24, 2009 at 07:27 UTC | |
by ikegami (Patriarch) on Nov 24, 2009 at 07:32 UTC | |
by BrowserUk (Patriarch) on Nov 24, 2009 at 17:08 UTC |