in reply to Sub Definitions Within Subs: Best Way to Exploit
Unfortunately, any subroutine that you define like that will be put into the namespace of the package that it's resident in. Thus, you cannot have lexically scoped subroutines. From what I understand, we will have lexically scoped subs in Perl6.
What you want is a closure:
my $bar = sub { print shift }; sub foo { print "Hello\n"; $bar->( "Saluton mondo!\n" ); } foo(); $bar->( "Hello world!\n" );
Hmm... as I recall, the proper definition of a closure is an anonymous code reference that contains a reference to a lexically scoped variable that is defined outside of itself. Since this example doesn't really do that, is it, strictly speaking, a closure?
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re: Sub Definitions Within Subs: Best Way to Exploit
by perrin (Chancellor) on Oct 11, 2001 at 05:20 UTC | |
by htoug (Deacon) on Oct 11, 2001 at 09:15 UTC | |
by perrin (Chancellor) on Oct 11, 2001 at 18:09 UTC | |
by htoug (Deacon) on Oct 12, 2001 at 12:33 UTC |