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.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.