in reply to Defining a sub within a sub: OK?
I have seen the trick on PM of local-izing a glob within a subroutine and then defining a named subroutine via that glob, but otherwise the approach for private subroutines seems to be to use anonymous subs defined within a sub.
The statement my sub foo { ... } produces a "my sub" not yet implemented ... error in 5.10 (and before AFAIR), so someone is thinking about this!
Updates:>perl -wMstrict -le "print level2(); sub level1 { sub level2 { return 'level2(): foo' } print 'level1(): ', level2(); } level1(); print level2(); " level2(): foo level1(): level2(): foo level2(): foo
>perl -wMstrict -le "sub level1 { my $x = shift || 'default'; local *foo = sub { return 'foo' . $x }; print foo(); } level1('bar'); level1(); foo(); " foobar foodefault Undefined subroutine &main::foo called at -e line 1.
|
---|