in reply to Q on HTML::Element recursive lambda comment
In this bit of the doc, he is rewriting the simple code traversal routine
to be more lispish. In particular, he is assigning an anonymous sub to a lexical, rather than just creating a give_id sub. Doing the former promotes the sub to a first class object, i.e., it can be assigned to a variable and passed around the same as any other object.{ 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); }
Now, you would expect, $giv_id to go out of scope at the end of the block, but it is used in the anonymous sub, so this 'closure' lives on unless you destroy it explicitly. This may or may not be what you want to do, depending on the rest of your code.
I think the code is perfectly fine to use. It is not very nutty and should pose no problems. But the non-lispy code is a little simpler.
-Mark
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Q on HTML::Element recursive lambda comment
by flyingmoose (Priest) on Mar 08, 2004 at 19:55 UTC |