in reply to Re: Local for lexicals (KISS)
in thread Local for lexicals

Unfortunately, this still (if I may use loose language) copies rather than aliasing, hence runs into trouble if $x is a tied variable (probably among other circumstances).

Given

sub Tie::TIESCALAR { return bless \$_[1] => $class } sub Tie::FETCH { return ${$_[0]} } sub Tie::STORE { $_[0] = "I'm not tied any more" }
if I replace my $x = 1 by tie my $x, 'Tie', 1, then the given code blows up upon leaving the inner scope.

UPDATE: Oops, $class should be $_[0].

Replies are listed 'Best First'.
Re^3: Local for lexicals (KISS)
by tye (Sage) on Aug 10, 2009 at 21:10 UTC

    Yes, providing quite broken 'tie' implementations can make any code blow up. q-:

    Fixing your code so that it works and also demonstrates that things don't stop being tied shows that my code works fine with such things:

    #!/usr/bin/perl -w use strict; # my $x = 1; tie my $x, 'Tie', 1; my $f = sub { $x }; print $f->(), "\n"; # => 1 { my $scope= tempSet( \$x, 2 ); print $f->(), "\n"; # => 2 } print $f->(), "\n"; # => 1 sub TempSet::DESTROY { shift(@_)->() } sub tempSet { my( $sv, $new )= @_; my $prior= $$sv; my $restore= bless sub { $$sv= $prior }, 'TempSet'; $$sv= $new; return $restore; } sub Tie::TIESCALAR { return bless \$_[1] => $_[0] } sub Tie::FETCH { return "${$_[0]} is still tied at line " . (caller()) +[2] } sub Tie::STORE { ( ${$_[0]} = $_[1] ) =~ s/ is still tied at line \d+$ +// }

    which produces

    1 is still tied at line 7 2 is still tied at line 10 1 is still tied at line 12

    (Updated code to make tied-ness more apparent.)

    (Yes, in constrast, local is implemented in a way that it temporarily hides the tied nature of the localized scalar.)

    - tye        

      Fixing your code so that it works and also demonstrates that things don't stop being tied shows that my code works fine with such things …
      I agree that my code isn't a very good implementation of a tied variable, but it is what I meant (UPDATE 3: except for the silly out-of-place $class), and fixing it breaks the example. :-) (Your code doesn't quite fix it, because you try to assign to a read-only variable. Something like sub Tie::TIESCALAR { return bless \( my $o = $_[1] ) => $_[0] } would be better, I think.)

      As a less offensive example, if I replace your STORE by

      sub Tie::STORE { ${$_[0]} = $_[1] unless defined ${$_[0]} }
      then it's clear that the localisation isn't really protecting anything in the inner scope from changing:
      tie my $x, 'Tie'; my $f = sub { $x }; { my $scope = tempSet(\$x, 2); print $f->(), "\n"; # => 2 is still tied } $x = 1; print $f->(), "\n"; # => 2 is still tied
      Your comment
      Yes, in contrast, local is implemented in a way that it temporarily hides the tied nature of the localized scalar.
      hits the nail on your head—I want precisely something that has the same effect for lexicals, of setting aside their previous value, whatever that value is, and restoring it undisturbed afterwards. (In particular, $x should not be tied in the inner scope, even if it is outside.)

      UPDATE 1: Actually, testing with tie our $x, 'Tie' and an interior local $x seems to show that localisation of globals doesn't behave the way I want, either! (See Re^6: Local for lexicals (untie) for a simpler example.)
      UPDATE 2: Changed if defined test to unless defined.

        then the given code blows up
        I agree that my code isn't a very good implementation of a tied variable, but it is what I meant

        Code that doesn't compile (under strict) is what you meant? And it is your STORE that causes things to "blow up". Just to be clear, my code works just fine if given a tied implementation that isn't seriously broken.

        Your code doesn't quite fix it, because you try to assign to a read-only variable

        I ran my code and got no complaints about assigning to read-only variables. Did you get such an error from Perl when running my code? Or are you determining that a read-only variable is being assigned to based on visual inspection of the code? Or did you plug my code into some other example and get that error? This might be a difference between Perl versions. I just kept your TIESCALAR code as close to what you wrote as I could.

        Of course, I could just put my $temp = $x; $x = 2 at the top of the scope and $x = $temp at the end [...] but this isn't completely satisfactory: For example, if $x starts off tied, then it will no longer be after my $temp = $x; $x = $temp.

        You are wrong there as well. my $temp= $x; $x= $temp; doesn't leave $x no longer tied.

        So I have some doubt of what your real requirements are. Yes, local causes a variable to temporarily not be tied (something that I find mostly to be an accident of implementation choice not something that makes sense as an intentional feature). You state that you want this behavior of local (but don't really explain why) but you also think that my code won't work with a tied variable, when it works just fine and you complain that the lexical variable will end up untied when that doesn't happen.

        So I can see wanting code to not "blow up" with "tied variables (probably among other circumstances)". And I can see not wanting the variable to end up untied. But neither of those are problems with my implementation.

        If somebody goes out of their way to tie $x before invoking lambda $x => sub { $x**2 }, then I think they would also be surprised to have that tie have no impact each time sub { $x**2 } gets called.

        - tye