PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

Detail info available here

But that article doesn't explain why the issue only exists for closure in nested sub

mod_perl doesn't free global resources,being aware of this, still doesn't explain why the issue only exists in nested sub closure.

Anyone knows the exact reason?

Replies are listed 'Best First'.
Re: About the closure issue in nested sub
by moritz (Cardinal) on Sep 09, 2011 at 11:48 UTC

    The answer is that Perl 5 really doesn't have "nested subs".

    Subs live in the symbol table, not inside a lexical scope, so the "inner" sub isn't really inside the "outer" sub -- it is in the symbol table, and thus can be called before the outer sub is called.

    The fact that perl still allows you to write a sub inside a sub comes from a cheat that makes it possible, but that cheat doesn't extend to the closure semantics you seem to expect.

    (The experience from watching and patching Perl 6 compilers shows that nested named subs and correct closure semantics are a pain to get right (if the inner sub is visible outside the outer sub), no wonder the perl 5 folks haven't felt like fixing this).