develooper Front page | perl.perl5.porters | Postings from May 2003

Re: [perl #22290] scalar untie doesn't always work

Thread Previous
From:
Graham Barr
Date:
May 23, 2003 00:43
Subject:
Re: [perl #22290] scalar untie doesn't always work
Message ID:
20030523084301.F85484@pobox.com
On Fri, May 23, 2003 at 04:36:47AM -0000, David Muir Sharnoff wrote:
> ---------- begin program ----------
> #!/usr/bin/perl -I.
> 
> my $x = 7;
> tie $x, 'Overlay', 8;
> print "tied x = $x.\n";
> untie $x;
> print "untied x = $x.\n";
> 
> package Overlay;
> 
> sub TIESCALAR
> {
> 	my $pkg = shift;
> 	my $y = shift;
> 	return bless \$y, $pkg;
> }
> 
> sub FETCH
> {
> 	my $self = shift;
> 	return "<$$self>";
> }
> 
> sub STORE { }
> ---------- end program ----------
> 
> In the above program, the untie only partially works.  After
> the untie, tied($x) no longer returns anything however, if I
> access $x, the value I get is "<8>" instead of the "7" it should
> have reverted to.

There is nothing in the documentation to say that it should
be reverted to its old value.

> If I add UNTIE or DESTROY methods to Overlay, they are called.
> 
> If I drop the 'print "tied x = $x.\n";' line then $x remains at "7".

Thats because when FETCH is called perl needs somewhere to place the
result before actually using it. In the case of a tied SCALAR it uses the
scalar that is tied. So there is now way that the old value can be
reverted to. After untie, a scalar will always hold the last value
that was fetched or set.

Graham.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About