develooper Front page | perl.perl5.porters | Postings from June 2002

Localising tie()d variables

Thread Next
From:
Steve Hay
Date:
June 27, 2002 01:48
Subject:
Localising tie()d variables
Message ID:
3D1ACFB7.5010705@uk.radan.com
Hi,

(Please copy any replies to steve.hay@uk.radan.com since I'm not on this 
mailing list. Thanks.)

I was on the verge of reporting some strange behaviour with local() and 
tie() when I noted in perl-5.8.0-RC1 that things are all up in the air a 
bit anyway regarding array and hashes, specifically that 
local(@tied_array) and local(%tied_hash) do not work as described in the 
perlsub manpage.

The perldelta manpage says that the old value is not restored correctly. 
 In fact, I find that the old value is not taken away correctly to start 
with: the new local() variable *is* still tie()'d when perlsub says it 
will not be.  The following test program and it's output was run using 
perl-5.8.0-RC1:

----------

use strict;
use warnings;

use Tie::Scalar;
use Tie::Array;
use Tie::Hash;
use Tie::Handle;

our($scalar, @array, %hash);

print "1. \$scalar = ", tied($scalar) || '', "\n";
tie $scalar, 'Tie::StdScalar';
print "2. \$scalar = ", tied($scalar) || '', "\n";
{
    local $scalar;
    print "3. \$scalar = ", tied($scalar) || '', "\n";
}
print "4. \$scalar = ", tied($scalar) || '', "\n";
untie $scalar;
print "5. \$scalar = ", tied($scalar) || '', "\n";
print "\n";

print "1. \@array = ", tied(@array) || '', "\n";
tie @array, 'Tie::StdArray';
print "2. \@array = ", tied(@array) || '', "\n";
{
    local @array;
    print "3. \@array = ", tied(@array) || '', "\n";
}
print "4. \@array = ", tied(@array) || '', "\n";
untie @array;
print "5. \@array = ", tied(@array) || '', "\n";
print "\n";

print "1. \%hash = ", tied(%hash) || '', "\n";
tie %hash, 'Tie::StdHash';
print "2. \%hash = ", tied(%hash) || '', "\n";
{
    local %hash;
    print "3. \%hash = ", tied(%hash) || '', "\n";
}
print "4. \%hash = ", tied(%hash) || '', "\n";
untie %hash;
print "5. \%hash = ", tied(%hash) || '', "\n";
print "\n";

print "1. HANDLE = ", tied(*HANDLE) || '', "\n";
tie *HANDLE, 'Tie::StdHandle';
print "2. HANDLE = ", tied(*HANDLE) || '', "\n";
{
    local *HANDLE;
    print "3. HANDLE = ", tied(*HANDLE) || '', "\n";
}
print "4. HANDLE = ", tied(*HANDLE) || '', "\n";
print "(The bogus warning printed next is already logged " .
    "as Perl Bug ID 20020623.006.)\n";
untie *HANDLE;
print "5. HANDLE = ", tied(*HANDLE) || '', "\n";
print "\n";

----------

1. $scalar =
2. $scalar = Tie::StdScalar=SCALAR(0x1b825d0)
3. $scalar = Tie::StdScalar=SCALAR(0x1b825d0)
4. $scalar = Tie::StdScalar=SCALAR(0x1b825d0)
5. $scalar =

1. @array =
2. @array = Tie::StdArray=ARRAY(0x1b6022c)
3. @array = Tie::StdArray=ARRAY(0x1b6022c)
4. @array = Tie::StdArray=ARRAY(0x1b6022c)
5. @array =

1. %hash =
2. %hash = Tie::StdHash=HASH(0x1b825d0)
3. %hash = Tie::StdHash=HASH(0x1b825d0)
4. %hash = Tie::StdHash=HASH(0x1b825d0)
5. %hash =

1. HANDLE =
2. HANDLE = Tie::StdHandle=GLOB(0x1bab3c4)
3. HANDLE = Tie::StdHandle=GLOB(0x1bab3c4)
4. HANDLE = Tie::StdHandle=GLOB(0x1bab3c4)
(The bogus warning printed next is already logged as Perl Bug ID 
20020623.006.)
untie attempted while 2 inner references still exist at D:\Temp\tie.pl 
line 57.
5. HANDLE =

----------

I have two questions about this:

Firstly, is it the intention to have localised tied arrays/hashes not 
tied (and to fix whatever bug there is later restoring the old value) as 
documented in perlsub, or is it the intention for them to remain tied as 
appears to be the case in the above test.

Secondly, is the situation with tied scalars correct or subject to 
change?  I cannot find any reference to what should happen to a 
localised tied scalar.  The above test shows that it remains tied.  This 
is actually the behaviour that I would like in some code that I'm 
writing (a module with a tied global that end-users may want to 
temporarily alter via a local() copy), but seems at odds with the 
documented behaviour of tied composite types.

Thanks in advance for any thoughts you may have on this.

- Steve



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