start page | rating of books | rating of authors | reviews | copyrights

Perl Cookbook

Perl CookbookSearch this book
Previous: 10.15. Trapping Undefined Function Calls with AUTOLOAD Chapter 10
Subroutines
Next: 10.17. Program: Sorting Your Mail
 

10.16. 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); Recipe 10.13 ; Recipe 11.4


Previous: 10.15. Trapping Undefined Function Calls with AUTOLOAD Perl Cookbook Next: 10.17. Program: Sorting Your Mail
10.15. Trapping Undefined Function Calls with AUTOLOAD Book Index 10.17. Program: Sorting Your Mail