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
This behaviour is due to the fact that while tied hashes behave very much like real hashes, internally they're very different (I've had trouble with this, see. lvalue trickery). When you retrieve a value from a tied hash you get back a new value every time even if you are accessing the same value in the hash e.g
{ package MyTH; use Tie::Hash; @ISA = qw( Tie::StdHash ); sub FETCH { return $_[0]->{$_[1]} } } use Devel::Peek; tie my %h, "MyTH"; $h{foo} = 1; Dump($h{foo}) for 1 .. 3; __output__ SV = PVMG(0x811a810) at 0x811f4a4 REFCNT = 1 FLAGS = (TEMP,GMG,SMG,RMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x8103ab8 ... SV = PVMG(0x811a810) at 0x811f4a4 REFCNT = 1 FLAGS = (TEMP,GMG,SMG,RMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x8104480 ... SV = PVMG(0x811a810) at 0x811f4a4 REFCNT = 1 FLAGS = (TEMP,GMG,SMG,RMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x8104480 ...
There a temporary value is created each time, but the value is always the same thing. Now if you create a reference you also get a temporary value, and stringified it will always change. What you're seeing in your code is that temporary reference being created each time and therefore a different stringification. So as you can see much 'magic' is going on behind the scenes of tied variables.
HTH

_________
broquaint


In reply to Re: references to elements of tied hashes by broquaint
in thread 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.