First of all what you heard on p5p is correct. The code example that I gave at Re (tilly) 9: Why are closures cool? makes it clear that there is no good resolution of the named/lexical issue.

As for the second, it is possible already:

sub recurse_postorder { my $self=shift; my $debug=shift; my @list; my $recurser; $recurser = sub { my $node=shift; my $depth=shift; print ("\t"x$depth)."$node->{value}\n" if $debug; $recurser->($node->{left},$depth+1); $recurser->($node->{right},$depth+1); push @list,$node->{value}; }; $recurser->($self->{root},0); return \@list; }
OK, so the cost is that you have to use anonymous functions and a slightly unusual syntax. But that is fixable as well - just use the syntax all of the time and it is no longer unusual for you! (This has been suggested seriously before because it allows strict.pm to catch typos in function names.)

The bigger cost is that the function is never garbage collected. You can partially fix this by doing an undef on the function. I say partially because there is an outstanding bug where it still doesn't get garbage collection. But ignoring the garbage issue, it is here and works. :-)


In reply to Re (tilly) 7 (exists): Why are closures cool? by tilly
in thread Why are closures cool? by mr.dunstan

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.