in reply to Re: Reference to an anon sub from within.
in thread Reference to an anon sub from within.

The use is that it allows a recursive anonymous subroutine.

The cost is that the subroutine will now hold a reference to itself and will therefore never get garbage collected.

  • Comment on Re^2: Reference to an anon sub from within.

Replies are listed 'Best First'.
Re^3: Reference to an anon sub from within.
by Sandy (Curate) on Mar 08, 2005 at 23:07 UTC
    Thanks Tilly,

    Here is my recursive 'sort-of' anonymous routine that calculates factors

    # --- Factoring with a nony mouse sub { my $sub; my $fact; $fact = 1; $sub = sub {my $num=shift; $fact=$num*$fact; if ($num==1){print "$fact\n"} else{$num--;$sub->($num)}; return $sub}; $sub->(5); }
    although, by using $sub it's not really anonymous, is it?

    UPDATE

    Improved version of above

    # --- Factoring with a nony mouse sub my $sub; { my $fact; $fact = 1; $sub = sub {my $num=shift; $fact=$num*$fact; $num--; $sub->($num) unless $num==1; return $fact}; } print $sub->(5),"\n";
      That's the idea, though there is no reason to make the anonymous factorial subroutine (it doesn't factor, it computes factorial) only work properly if you call it just once. See the following:
      my $sub; $sub = sub { my $num = shift; if (0 == $num) { return 1; } else { return $num * $sub->($num - 1); } }; print $sub->(5), "\n"; print $sub->(6), "\n";
      As for the anonymity, it is anonymous in that it doesn't have a global name in any package. OK, so someone somewhere has named a reference to it. But that's what we always mean with an anonymous subroutine.

      Incidentally in languages that lexically bind calling variables (ie not Perl), and support closures, it is possible to write a recursive function like this using functions of 1 variable without using any declarations or assignments. The trick is kind of complex, here it is (untested, from memory) in JavaScript:

      function (builder) { return function (n) { return builder(builder)(n); } }( function (recurse) { return function (n) { if (0 == n) return 1; else return n * recurse(recurse)(n); }; } )( 5 // THIS is the factorial to compute );
      There's no real point to code like this other than so that functional programmers can prove that they're really clever. (OK, I lie, it allows functional programmers to completely do away with side-effects. But I remain convince that proving cleverness is the main goal.)