lucylane has asked for the wisdom of the Perl Monks concerning the following question:
Now, if you've been reading Structure and Interpretation of Computer Programs too much, maybe you even want a recursive lambda. Go ahead:I've tried test programs to give me an error or other bad output but everything looks the same (w/ and w/o the undef). This is a construct that I like to use in my code and this comment bothers me. (Do others agree it's a little bit nutty btw?)It's a bit nutty, and it's still more concise than a call to the traverse method! It is left as an exercise to the reader to figure out how to do the same thing without using a $give_id symbol at all. It is also left as an exercise to the reader to figure out why I undefine $give_id, above; and why I could achieved the same effect with any of:{ my $counter = 'x0000'; my $give_id; $give_id = sub { 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); undef $give_id; }But not:$give_id = 'I like pie!'; # or... $give_id = []; # or even; $give_id = sub { print "Mmmm pie!\n" };$give_id = sub { print "I'm $give_id and I like pie!\n" }; # nor... $give_id = \$give_id; # nor... $give_id = { 'pie' => \$give_id, 'mode' => 'a la' };
Thank you for any wisdom.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Q on HTML::Element recursive lambda comment
by fizbin (Chaplain) on Mar 08, 2004 at 19:15 UTC | |
by lucylane (Acolyte) on Mar 08, 2004 at 20:08 UTC | |
by fizbin (Chaplain) on Mar 08, 2004 at 20:24 UTC | |
by fizbin (Chaplain) on Mar 08, 2004 at 21:07 UTC | |
Re: Q on HTML::Element recursive lambda comment
by kvale (Monsignor) on Mar 08, 2004 at 19:19 UTC | |
by flyingmoose (Priest) on Mar 08, 2004 at 19:55 UTC |