in reply to Re^2: Local for lexicals (KISS)
in thread Local for lexicals
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Local for lexicals (KISS)
by JadeNB (Chaplain) on Aug 10, 2009 at 21:29 UTC | |
by tye (Sage) on Aug 10, 2009 at 22:20 UTC | |
by JadeNB (Chaplain) on Aug 10, 2009 at 23:17 UTC | |
by JadeNB (Chaplain) on Aug 10, 2009 at 22:52 UTC | |
by tye (Sage) on Aug 10, 2009 at 23:59 UTC |