in reply to localizing lexical without messing with tie ?

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

{ local $tied = 'x'; ... }
is roughly the same as
{ my $restorer = localiser($tied, 'x'); ... }

given a function that creates an object that restores $tied on destruction.

use Sub::ScopeFinalizer qw( scope_finalizer ); sub localiser { my $var_ref = \$_[0]; my $saved = $$var_ref; $$var_ref = $_[1]; return scope_finalizer { $$var_ref = $saved }; }

(Sub::ScopeFinalizer is trivial to re-implement.)

Demonstration:

use strict; use warnings; use Sub::ScopeFinalizer qw( scope_finalizer ); { package Tied; use Tie::Scalar; our @ISA = qw( Tie::StdScalar ); sub FETCH { return ${ $_[0] }++ } } sub localiser { my $var_ref = \$_[0]; my $saved = $$var_ref; $$var_ref = $_[1]; return scope_finalizer { $$var_ref = $saved }; } { tie our $tied_pkg, 'Tied'; $tied_pkg = 'a'; print $tied_pkg; # a { local $tied_pkg = 'x'; print $tied_pkg; # x print $tied_pkg; # y print $tied_pkg; # z } print $tied_pkg; # b print $tied_pkg; # c print "\n"; } { tie my $tied_lex, 'Tied'; $tied_lex = 'a'; print $tied_lex; # a { my $restorer = localiser($tied_lex, 'x'); print $tied_lex; # x print $tied_lex; # y print $tied_lex; # z } print $tied_lex; # b print $tied_lex; # c print "\n"; }
axyzbc axyzbc

Replies are listed 'Best First'.
Re^2: localizing lexical without messing with tie ?
by LanX (Saint) on Sep 08, 2010 at 16:31 UTC
    > 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

      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