in reply to Making variables visible between calls.
Allow the function to take an argument.
sub create_f { my $func = shift; return sub { my $var = 'xpto'; $func->( $var ); } } my $f = create_f( sub { my $var = shift; print ">> $var <<\n"; } );
Otherwise, I don't see a way to do it. The sub can't reach outside its scope for variables that aren't global.
You could cook up something with eval, maybe.
sub create_f { my $func = shift; return sub { my $var = "xpto"; eval $func; } } my $function = create_f( 'print ">> $var <<\n"' ); $function->(); __END__ >> xpto <<
I'm not sure if that fits with your real problem or not.
|
|---|