Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Scope::Upper localize?

by Anonymous Monk
on Sep 11, 2012 at 19:49 UTC ( [id://993049]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Scope::Upper localize?
in thread Scope::Upper localize?

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

For my purposes, the only work to be done at scope exit, is for the previous value of the localized variable to be restored.

All the work is in deriving the value(s) of the variable(s) that need localizing. A common pattern of boilerplate code needed at the *top* of many different scopes and so a candidate for wrapping up in a sub.

But with local you cannot localize variables in your caller. With localize() you can. And the only required clean up is taken care of by the normal scope clean up.

Replies are listed 'Best First'.
Re^5: Scope::Upper localize?
by Corion (Patriarch) on Sep 11, 2012 at 20:11 UTC
    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

      -- Pure perl fashion

      Guard isn't pure perl.

      If you find that mess preferable to localize(), go for it.

        I only used Guard for convenience. It can easily be replaced by

        package My::Guard; use strict; sub new { bless $_[1] => $_[0]; }; sub DESTROY { $_[0]->() };

        The full (and really pure Perl) program then becomes:

        #!perl -w package My::Guard; use strict; sub new { bless $_[1] => $_[0]; }; sub DESTROY { $_[0]->() }; package main; use strict; sub alias {\@_}; sub localize { my @values = @{ +pop }; # or any other convenient method my $aliases=alias(@_); my @saved= @_; #warn Dumper \@saved; $_[$_] = $values[$_] for 0..$#values; My::Guard->new(sub{ $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";

        But still, you haven't convinced me that there are problems to which Scope::Upper is a good solution.

        Update: As an afterthought, for somebody who thinks they need Scope::Upper, I think you should investigate the easier and potentially less powerful methods first, like having a destructor invoke a callback. I don't consider this a rare/outlandish technique, and there are many packages implementing this, like AtExit, Aspect::Guard, Scope::Guard and ReleaseAction.

        Update2: Of course, this clever idea has a big drawback. It doesn't work for arrays and hashes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://993049]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found