in reply to Re: Re (tilly) 5: Why are closures cool?
in thread Why are closures cool?

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. :-)