in reply to Re: localizing lexical without messing with tie ?
in thread localizing lexical without messing with tie ?

> local doesn't remove the magic — the variable is still tied in the block

argh you're right, I didn't test what I didn't expect... I would explicitly need to untie the localized var.

package NewStdScalar; require Tie::Scalar; @ISA = qw(Tie::StdScalar); sub FETCH { return ${+shift}++ } package main; $\="\n"; $,="\t"; tie our $scalar, 'NewStdScalar'; sub prsc {print $scalar }; $scalar=0; prsc(); { local $scalar=42; # untie $scalar; # uncomment this to make it work prsc(); prsc(); prsc(); print $scalar; } prsc(); __DATA__ 0 42 43 44 45 1

Sorry, ATM I'm too tired to look further into the examples you posted, later more.

But I think I will go the simple way to exclude the use of tied variables by throwing a warning. Thx,

Cheers Rolf

Replies are listed 'Best First'.
Re^3: localizing lexical without messing with tie ?
by ikegami (Patriarch) on Sep 08, 2010 at 17:01 UTC

    You don't seem to know what you're looking for. At the very least, it wasn't clearly communicated. Please clarify.

    Update: Maybe it's one of these:

    Override a global package variable for a static scope:

    our $var = 'a'; { my $var = 'x'; print "$var\n"; } # x print "$var\n"; # a
    my $var = 'a'; { my $var = 'x'; print "$var\n"; } # x print "$var\n"; # a

    Override a global package variable for a dynamic scope: (Doesn't remove magic. Doesn't localise pos() except maybe on recent Perls.)

    our $var = 'a'; sub f { print "$var\n"; } { local $var = 'x'; f(); } # x print "$var\n"; # a

    Override a global package variable for a dynamic scope:

    our $var = 'a'; sub f { print "$var\n"; } { local *var; *var = 'x'; f(); } # x print "$var\n"; # a

    Override a global lexical variable for a dynamic scope:

    my $var = 'a'; sub f { print "$var\n"; } { ?????; $var = 'x'; f(); } # x print "$var\n"; # a

        see Re^2: localizing lexical without messing with tie ?.

        What do you call "passed variable"?

        As I said, an additional local untie does what I wanted.

        Doubtful. The untie isn't local. I've provided a solution that works where you used untie.