Recursive closure


Uri: It would only leak if you let it fall out of scope without destroying it

Brian: "You only need weak references if you don't want use some other way to take care of tracking and breaking the circular dependancy on every possible exit path" is true of anything that uses weak references.

Uri: Damian refutes the need for the weak ref or an explicit breaking of the circular refs. You can verify that by blessing the closure and seeing it die:

package Bang;
sub DESTROY { print "Bang!\n" }

my $fib;
$fib = bless sub {
         my ($n) = @_;
         return 1 if $n < 2;
         return $fib->($n-1) + $fib->($n-2);
       }, 'Bang';

print $fib->(7), "\n";

$fib = 7;   # Should "Bang!" here

$fib inside the code is the same $fib outside of it, so the code ref only has one ref which is in $fib. When $fib goes out of scope, the whole pad is freed and the ref count goes to 0 like it should and it gets destroyed.

Can you spot the fallicy?