moritz has pretty much answered the technical aspect of your question, but now I am curious about your design choice. What are you trying to accomplish with this closure you are creating? There is a good chance that there is a different way of doing what you are looking for that is less complicated

My instinct is that you are trying to perform some sort of wrapping functionality with out increasing the stack frame you are working with. In otherwords, do something, then call your function, but keep @_ exactly as was originally called. Below is an example.

use strict; sub trace_wrapper_no_frame { my $f=shift; return sub { print "Trace, no frame\n"; goto $f; }; } sub trace_wrapper_frame { my $f=shift; return sub { print "Trace, with frame\n"; $f->(@_); # <----- LINE 15 }; } sub who_says_hello { print(join(" ",caller), "\n"); print "Hello, $_[0]\n"; } who_says_hello("World"); # <------ LINE 26 print '-' x 20, "\n"; my $frame_wrapped_hello = trace_wrapper_frame(\&who_says_hello); $frame_wrapped_hello->("dolly"); print '-' x 20, "\n"; my $no_frame_wrapped_hello = trace_wrapper_no_frame(\&who_says_hello); + $no_frame_wrapped_hello->("Fifty"); #<----- LINE 36

The output looks like the following

From line # 26 Hello, World -------------------- Trace, with frame From line # 15 Hello, dolly -------------------- Trace, no frame From line # 36 Hello, Fifty

But don't do that if you don't have to. Hook::LexWrap is a great module that does it all for you, but with more features allowing you to specify pre and post call wrappers, and allows you to lexically scope said wrapping if you so desire.

.... Then again, this might have nothing to do with what you want...


In reply to Re: Scope problem in closures with an eval statement by Fiftyvolts
in thread Scope problem in closures with an eval statement by citromatik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.