accustomed to using 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 type of thing incurs mysterious warnings about
-``will not stay shared''. For example, this won't work:
+"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();
- }
+ }
A work-around is the following:
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 (anonymous subroutine). But when