ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
10.16. Nesting SubroutinesProblemYou want to have nested subroutines, such that one subroutine is only visible and callable from another. When you try the obvious approach of nesting SolutionInstead 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. DiscussionIf 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 This essentially creates a function local to another function, something not directly supported in Perl; however, the programming isn't always clear. See AlsoThe 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 |