It's a recursive, i.e. leaking, reference.
Hmm... probably safer to keep the reference on the call stack, thus releasing it on exit.
For example, adopting the convention that the reference is always passed as the first parameter:
my @factorials = map {
$_[0] ||= sub {
my $factor = pop;
return $factor > 1
? &{$_[0]}($_[0],$factor-1) * $factor
: 1;
};
&{$_[0]}($_[0],$_)} (3,5,7,9);
..or..
sub make_call {
goto $_[0];
}
my @factorials = map {
make_call (sub {
my $factor = $_[1];
return $factor > 1
? make_call($_[0], $factor-1) * $factor
: 1;
}, $_);
} (3,5,7,9);
|