With Guard, you get to set up a call back when the scope is left to clean up whatever.

Ah - sorry - I meant that the localizing code returns such a guard object, so that the caller can then decide whether to clean up at exit or to pass that guard on upwards again.

I'm still unaware of where that implicit localization-at-a-distance would be good design. The following code seems to me to solve the problem in a pure-perl fashion, and keeps the localization somewhat explicit, instead of prodding through the enclosing pads by name:

#!perl -w use strict; use Guard; sub alias {\@_}; sub localize { my @values = @{ +pop }; # or any other convenient method my $aliases=alias(@_); my @saved= @_; #warn Dumper \@saved; $_[$_] = $values[$_] for 0..$#values; guard { $aliases->[$_]=$saved[$_] for 0..$#saved; }; }; my ($a,$b)=('a','b'); print "Start\n"; print qq($a $b),"\n"; my $restore=localize($a,$b,['foo','bar']); print "Localized\n"; print qq($a $b),"\n"; undef $restore; print "Restored\n"; print qq($a $b), "\n"; __END__ Start a b Localized foo bar Restored a b

In reply to Re^5: Scope::Upper localize? by Corion
in thread Scope::Upper localize? by Anonymous Monk

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.