in reply to Re: Closures and undefined subs
in thread Closures and undefined subs

As soon as your out subroutine's scope is exited both of in and in2 are restored to their previous contents.

That's not a problem since he doesn't create any external references to in_2. He'd need lexicals in that case. For example, he'd need to use lexicals if the outer sub returned \&in_2.

Using lexicals doesn't help here. In addition to the more complex call syntax, using lexicals would create a memory leak if recursion was involved unless you explicitly undefed the lexical at the end of the outer sub.

Replies are listed 'Best First'.
Re^3: Closures and undefined subs
by Anonymous Monk on Sep 26, 2007 at 14:39 UTC
    I am afraid use solved my problem but I am not happy for that :-)
    RungeKutta needs \&in_2 in its own call.
    I my opinion I need to write the closures as Rungekutta needs some equation in the differentiation sub. In this case the equation is too complex and needs variables that are imported into the outer sub of the package. So I guess I am in need of that much levels of complexity.
    But what do you mean with lexicals? Could you give a "simple" example? Thank you.

      But what do you mean with lexicals? Could you give a "simple" example? Thank you.

      Short for "lexical variables".

      my $var;

      RungeKutta needs \&in_2 in its own call.

      Not a problem. See the solution I added to my original post in this thread.

Re^3: Closures and undefined subs
by ursus (Acolyte) on Sep 28, 2007 at 22:41 UTC
    I disagree (which doesn't mean I'm right :-) - the code is parsed with the rest of the file; all refs to that code will be to the same piece. Lexicals FTW!