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.

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"; } }
produces ...
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)
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.

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 ...


In reply to references to elements of tied hashes by steves

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.