Another question though; here's why I often use the "anonymous lambda" style (dynamic sub): if I do something like this (embedded sub):
The var $counter is captured in the closure and does not get reset even though I'd like it to be reset. It's either this way and moving and resetting $counter by hand outside the block (which I won't do because it's logically inside the function and also easy to forget) or using the dynamic sub and risk a memory leak - how would I get the best of both worlds (uncaptured var + no risk of memory leak). Using a private embedded package or putting everything in it's own package seems wordy.sub traverse { my $start_node = HTML::TreeBuilder->new; $start_node->parse_file(shift); { my $counter = 'x0000'; sub give_id { my $x = $_[0]; $x->attr('id', $counter++) unless defined $x->attr('id'); foreach my $c ($x->content_list) { give_id($c) if ref $c; # ignore text nodes } }; give_id($start_node); } }
Thanks for the enlightenment.
In reply to Re: Re: Q on HTML::Element recursive lambda comment
by lucylane
in thread Q on HTML::Element recursive lambda comment
by lucylane
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |