in reply to Re: Is it possible to create a sub exclusive to a sub?
in thread Is it possible to create a sub exclusive to a sub?

Yes, stacks of nested subs are super-cool. But, uh, why bother writing all recursive code this way? I don't see any gain in efficiency (if anything, creating all those subs might slow things down a tad), and it introduces an extra level of "WTF?" to people reading the code.

On the other hand, if you're writing tail-recursive code, this is a great way of not exposing an accumulator variable to the rest of the world. For example (untested):

sub fact { my $fact_tr; # needs to exist before it gets assigned to $fact_tr = sub { my ($v, $r) = @_; return 1 if $v < 2; # oversimplified $fact_tr->($v-1, $v * $r); }; # thanks ihb for pointing out the missing semi $fact_tr->(@_, 1); }
Also, have a look at Just Another Godel, Escher, Bach hacker.

Edit: Separated declaration and use of $fact_tr. Thanks ihb!

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry