First of all, Sean Burke writes eclectic prose and doesn't write code so much as play with code. I love his style :)

In this bit of the doc, he is rewriting the simple code traversal routine

{ 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); }
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.

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


In reply to Re: Q on HTML::Element recursive lambda comment by kvale
in thread Q on HTML::Element recursive lambda comment by lucylane

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.