Nesting Subroutines
Problem
You want to have nested subroutines, such that one subroutine is only visible and callable from another. When you try the obvious approach of nesting sub
FOO
{
sub
BAR
{
}
...
}
Perl gives you warnings about variables that will not stay shared.
Solution
Instead of having the inner functions be normal subroutines, make them closures and temporarily assign them to the typeglob of the right name to create a localized function.
Discussion
If you use nested subroutines in other developing languages with their own private variables, you'll have to work at it a bit in Perl. The intuitive coding of this kind of thing gives the warning "will not stay shared". For example, this won't work:
sub outer { my $x = $_[0] + 35; sub inner { return $x * 19 } # WRONG return $x + inner(); }
The following is a workaround:
sub outer { my $x = $_[0] + 35; local *inner = sub { return $x * 19 }; return $x + inner(); }
Now inner()
can only be called from within outer()
because of the temporary assignments of the closure. But when it does, it has normal access to the lexical variable $x
from the scope of outer()
.
This essentially creates a function local to another function, something not directly supported in Perl; however, the developing isn't always clear.
See Also
The sections on "Symbol Tables" in of Perl Developing and in perlmod (1); the section on "Closures" in of Perl Developing and the discussion of closures in perlref (1); ;