Some sample uses:
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

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.