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

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.

Replies are listed 'Best First'.
Re^8: Scope::Upper localize?
by Anonymous Monk on Sep 11, 2012 at 21:14 UTC

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

    Do I need to convince you?

    I only need convince myself before I chose to use it (or not) in my code?

      No, you don't need to convince me. So far, you haven't shown the concrete use case at hand, nor convinced me of the abstract idea that there may be a use case. You seem to want to keep your use case to yourself, but are somehow prevented from saying so. I don't know why that is the case. It just confirms to me that without a concrete use case, discussing the use of heavy machinery does not make sense, as the costs and benefits cannot be made clear.

      But then again, this was mostly for my idle curiosity, and thus not really important. It allowed me to revisit funny parts of the toolshed, and provided idle amusement for a few minutes, even though the promise of learning a new code situation did not come through.

        -- you haven't shown the concrete use case at hand, nor convinced me of the abstract idea that there may be a use case.

        Is *that* a requirement of this place in order to ask this question?

        If not, why persist in asking it?

        -- You seem to want to keep your use case to yourself,

        Is that against the rules?

        -- , discussing the use of heavy machinery

        Do you see me make any request for discussion of the heavy machinery?

        -- provided idle amusement for a few minutes,

        Glad to have been of service.

        -- You seem to want to keep your use case to yourself,

        Yes. Is that so hard to respect?

        I wish to keep it to myself, because I have no interest in having other people throw their opinions of what they think I should or should not do into the discussion. (fat chance.)

        The question was clear and simple. The answer equally so. No room for prolonged discussion or heated debate. Just "how"? Like this. Done.

        But there is always someone, or multiple somones, that feel the need to try and impose their views on life, the universe and everything, on everyone else.

        And that was why this thread was started anonymously. But, there are always those around that can (mis)use their privileges to see through the sham of anonimity, and pop up to assert their opinion.

        This subthread, as with the other subthreads of a similar ilk, are exactly what I wished to avoid by posting anonymously.