in reply to Re^2: Defining a sub within a sub: OK?
in thread Defining a sub within a sub: OK?

* The reasoning behind sub NAME { } constructs being realized early is so that you can call subroutines even if their definition is "later" in the code.

That's mostly true, but not very precise.

If a subroutine is defined after it is called, the call has to use parenthesis. If it doesn't, it's interpreted as a string literal (or as an error if strict subs are in effect).

$ perl -E 'say 1, foo; sub foo { 3 }' 1foo $ perl -E 'sub foo { 3 }; say 1, foo' 13 $ perl -E 'say 1, foo(); sub foo { 3 }' 13

However it the call uses parenthesis, there's no need it has to be know at compile time at all:

$ perl -E 'eval "sub foo { 3 }"; say 1, foo();' 13

But I think for this discussion it's more important to ask when the lexical pad of sub a is being built and destroyed. As long as the a is not part of the call chain, $x is in no lex pad, and sub b can't work in a meaningful way.

Perl 6 - links to (nearly) everything that is Perl 6.