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 programming 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 programming isn’t always clear.
See Also
The sections on “Symbol Tables” in Chapter 5 of
Programming Perl and in perlmod
(1); the section on “Closures” in Chapter 4 of
Programming Perl and the discussion of
closures in perlref
(1); Section 10.13; Section 11.4 ...
Get Perl Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.