in reply to Scope problem in closures with an eval statement

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...