steves has asked for the wisdom of the Perl Monks concerning the following question:
This one surprised and stumped me. I found it chasing down a bug and boiled it down to the test case below. Basically, if I tie a hash then pass references to elements of that tied hash to sub's, those references appear in the sub as different values. Not always -- it depends on where in the arg list the reference is. Without the tie, it behaves as expected. I've tried several ties, so it's not specific to Tie::IxHash. This is Perl 5.6.1.
produces ...use strict; use Tie::IxHash; my $h = {}; tie %$h, "Tie::IxHash"; my $a; $h->{FOO} = 'foo'; print "reference to a = ", \$a, "\n"; print "reference to h->{FOO} = ", \$h->{FOO}, "\n"; show_refs(\$a, \$h->{FOO}); print "reverse order ...\n"; show_refs(\$h->{FOO}, \$a); sub show_refs { my $ref; foreach $ref (@_) { print "in show_refs, ref = $ref\n"; } }
The first call has the reference to hash element $h->{FOO} as a different value than it is outside the sub; the second ('reverse order') has it the same.reference to a = SCALAR(0x102d4c) reference to h->{FOO} = SCALAR(0x13bdb8) in show_refs, ref = SCALAR(0x102d4c) in show_refs, ref = SCALAR(0x13bdd0) reverse order ... in show_refs, ref = SCALAR(0x13bdb8) in show_refs, ref = SCALAR(0x102d4c)
Any idea what's going on here? I'd expect a reference to $h->{FOO} to be the same as what it is when I first check it -- not change when passed to a sub. FYI, I've also noticed that references to elements of tied hashes are the same for all elements after the tie, although this does not seem to affect the ability to dereference them later. tie magic I assume ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: references to elements of tied hashes
by broquaint (Abbot) on Feb 05, 2003 at 12:03 UTC | |
|
Re: references to elements of tied hashes
by steves (Curate) on Feb 05, 2003 at 21:07 UTC | |
by steves (Curate) on Sep 10, 2004 at 19:56 UTC | |
by steves (Curate) on Sep 11, 2004 at 23:26 UTC |