There is no big point for using nested subs if they are not completely encapsulated and might interfere with external names, which you might not be able to control.
IMHO safer alternatives are:
# No Namespace Confusion! $\="\n"; sub inner { print "global_inner" } inner(); sub whatever { inner() } sub outer { { package outer; local *inner=sub {print "inner_of_outer"}; inner(); } whatever(); } outer(); __END__ global_inner inner_of_outer global_inner
or
I prefere the latter! Unless you plan to call outer() recursively *... ; )# No Memory Leak! $\="\n"; my $inner=sub { print "global_inner" }; $inner->(); sub whatever { $inner->() } { my $inner; sub outer { $inner=sub {print "inner_of_outer"}; $inner->(); whatever(); } } outer(); __END__ global_inner inner_of_outer global_inner
Good night! Rolf
UPDATE: (*) and if you really want to do it, you have to simulate the mechanism of local for lexicals, by starting outer() with push @stack,$inner; and ending it with $inner=shift @stack
In reply to Re^6: sub fuction inside sub functioin?
by LanX
in thread sub fuction inside sub functioin?
by deewanagan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |