Others have pointed out why this is the way it is and pointed you toward modules that will work around it, but I'd like to point out a simpler, more blindingly obvious solution: Go ahead and use the reference as your key, but also store it as a value (in addition to whatever other values you are storing).

There are two ways to do this, and which one you pick is a matter of style. You can use parallel hashes, with the same key across two or more hashes returning a related set of values, or you can use a nested hash. The latter is easier to make look similar to what you have in your code...

%nestedhash = { $someref => { ref => $someref, val => "STUFF", }, $anotherref => { ref => $anotherref, val => stuff(), }, }

Though the way hashes tend to be used in the real world, you're more likely to end up with something more like this...

while (($ref, $val) = get_pair()) { my %thisrecord = { ref => $ref, val => $val }; $record{$ref} = \%thisrecord; }

Personally, I tend to use parallel hashes, which accomplishes roughly the same thing in a slightly different way, like so...

while (($r, $v) = get_pair()) { $ref{$r}=$r; $val{$r}=$v; }

sub H{$_=shift;while($_){$c=0;while(s/^2//){$c++;}s/^4//;$ v.=(' ','|','_',"\n",'\\','/')[$c]}$v}sub A{$_=shift;while ($_){$d=hex chop;for(1..4){$pl.=($d%2)?4:2;$d>>=1}}$pl}$H= "16f6da116f6db14b4b0906c4f324";print H(A($H)) # -- jonadab

In reply to Re: using references as keys in a hash. by jonadab
in thread using references as keys in a hash. by habit_forming

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.