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.
In reply to Re: closure clarity, please
by BrowserUk
in thread closure clarity, please
by 7stud
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |