Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re^3: HTML::LinkExtor weirdness (scoping closures nested named subs)by Anonymous Monk |
on Nov 07, 2013 at 00:27 UTC ( [id://1061507]=note: print w/replies, xml ) | Need Help?? |
This is why you don't nest named subs, the closure becomes "static" perl -E " sub f{ my %g; sub g{say \%g; } g(); say \%g; \%g } say f(),$/ for 1 .. 4;" perl -E ' sub f{ my %g; sub g{say \%g; } g(); say \%g; \%g } say f(),$/ for 1 .. 4; '
By making the nested subroutine a variable (also known as an anonymous sub), the closure is dynamic $ perl -lE " sub f{ my %g; my $g = sub {say \%g; $g{1}++}; $g->(); say \%g; \%g } say f(),$/ for 1 .. 4;"
In Section
Seekers of Perl Wisdom
|
|