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

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

Thread Previous | Thread Next
Ricardo Signes
September 17, 2012 15:56
Re: [perl #104118] localised tied scalar referenced elsewhere hasstrange effects
Message ID:
* Father Chrysostomos <> [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

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

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

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.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About