develooper Front page | perl.perl5.porters | Postings from September 2012

Re: [perl #104118] localised tied scalar referenced elsewhere hasstrange effects

Thread Previous | Thread Next
From:
Ricardo Signes
Date:
September 17, 2012 15:56
Subject:
Re: [perl #104118] localised tied scalar referenced elsewhere hasstrange effects
Message ID:
20120917225602.GD7633@cancer.codesimply.com
* Father Chrysostomos <perlbug-followup@perl.org> [2011-11-20T19:37:23]
> The hard part is figuring out how it is supposed to work.  When you write
> ‘local $foo’ on a regular scalar, the *foo{SCALAR} slot is temporarily
> replaced with another scalar.  If you have a reference to the original $foo
> elsewhere, you can modify it even during the localisation.  The modified
> value of $foo is what is visible after the localisation goes out of scope.
> 
> Localised tied scalars are a different creature.  The scalar is replaced with
> a new scalar, like an untied variable, but then the new scalar is tied to the
> very same object.  The old scalar has its private flags copied to the public
> flags, so that a simple SvSETMAGIC will restore the previous value.  But that
> implementation detail leaks out as through a sieve when a reference to the
> original scalar exists elsewhere.  That is why "$a" is skipping FETCH.

Not only are they different from untied scalars, but also from tied arrays and
hashes.

Tied arrays and hashes cease to be tied after localization, after some work by
Dave M. and discussion here:

  http://www.xray.mpe.mpg.de/cgi-bin/w3glimpse2html/perl5-porters/2010-04/msg00561.html

In fact, tied scalars were also no longer tied after local, after that thread,
but then arose another thread:

  http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2010-05/msg00627.html

David Golden notes that this change broke File::chdir and Dave Mitchell says
he'll revert it unless there is some argument.  There is no contribution to the
thread whatsoever (other than me noting that I don't really use File::chdir
anymore; I'd already switched to the no-localization File::push).  The
scalar-untying is reverted in 89adf4e8e0de17fedf78328b32920f47635850d0.

It's a tough nut.  The current behavior stinks, and breaking stuff stinks, and
so does inconsistency between tied scalars and tied arrays.  If I had my
druthers, I'd not have the tie survive localization, but I think this may be
a difficult behavior to sell the deprecation of.

-- 
rjbs

Thread Previous | Thread Next


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