use ScopeSaver; use strict; use warnings; our $ss; { my $x = 1; { my $y = 2; my $z = 3; $ss = eval new ScopeSaver(lexical => ['$y']); } # $x doesn't need to be listed as lexical (though it would be harmle +ss) # since its scope hasn't ended at this time $ss->scope_eval('print "x is $x\n"'); # $y is still available because it was listed $ss->scope_eval('print "y is $y\n"'); # $z is no longer available but wasn't listed to preserve $ss->scope_eval('print "z is ", (defined $z?$z:"undef"), "\n"'); } { my $closure; { my $x = "original"; $closure = sub { print "in $x closure\n" }; } { my $x = "rebound"; $ss = eval new ScopeSaver(lexical => ['$x']); } # in original closure: &$closure; # rebound closure: $closure = $ss->rebind($closure); &$closure; } { our ($x,$y) = 613..614; { my ($x,$y) = 666..667; $ss = eval new ScopeSaver(global => ['$x'], private => ['$y']); } # $x will be the global, not the lexical: $ss->scope_eval('print "x is $x\n"'); # $y will be a freshly allocated lexical for each eval, # not the global or the outside lexical: $ss->scope_eval('print "y is ", (defined $y?$y:"undef"), "\n"'); }
In reply to Re: Rebinding closures by scope
by ysth
in thread Rebinding closures by scope
by ysth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |