I’m attempting to call a nested recursive perl function, but I can’t tack the correct syntax.
Question: What is the correct syntax to perform a recursive call for a nested function (if nested functions should be recursively called at all)?
Answer: Refer to suggested pseudocode in the accepted answer.
Here is a pseudocode snippet:
use Scalar::Util;
sub outerfunction {
my $innerfunction = sub {
# Do something
innerfunction();
# Do other things
};
Scalar::Util::weaken($innerfunction);
&$innerfunction(@_);
};
I’ve tried to invoke innerfunction as the following (with the consequential error messages):
innerfunction
Undefined subroutine &main::innerfunction
&innerfunction
Undefined subroutine &main::innerfunction
&$innerfunction
Global symbol “$innerfunction” requires explicit package name
I’ve also tried to declare the innerfunction as local, but receive the following:
Global symbol “$innerfunction” requires explicit package name
I don’t have much experience with interpreted languages, so any incidental commentary related to memory/stack leakage/ corruption or other dangers with the above pseudocode (other than system limits on recursion) would be greatly appreciated as well.
Thanks!
perl v5.10.1 running on Linux 2.6.34.7-61.fc13.x86_64
The
innerfunction()syntax is only available for subroutines that have been installed into the symbol table (such as thesub NAME {...}syntax does). You need to call your inner function as$innerfunction->()or&$innerfunction(), but where you are having trouble is with the scoping of the$innerfunctionlexical.When you declare a variable with
my, the variable is in scope after that statement ends. So you need to split your declaration:To break the circular reference with
weakenthe usual pattern is:So now, as soon as
$strong_refgoes out of scope, the subroutine will be garbage collected.