I've released a module named Array::AsHash which allows one to use references as hash keys (it actually does a lot more than this, but this is one of the features). Normally, this works fine:

my $array = Array::AsHash->new({array => \@array}); my $value = $array->get($some_reference);

It does this by checking the Scalar::Util::refaddr of the reference. However, this can fail if someone wants to override equality. In Python, you can get around this by overriding the __hash__() method of object. In Perl, we can't do this with unblessed references. Thus, the following will never succeed:

my $aref = [1,2,3]; my $array = Array::AsHash->new({ array => [ $aref => 'Ovid' ] }); print $array->get($aref); # prints "Ovid" print $array->get([1,2,3]); # fails

Ordinarily, this is fine. However, if we only care about the values a reference contains and not whether or not it's the same reference, we're stuck. I'd like to be able to pass the constructor a coderef which will determine equality of references beyond simply comparing their addresses. Things like the Test::More::is_deeply function or the useful Test::Deep module would be great, but they're tied to Perl's testing framework.

Can anyone offer suggestions?

For those wondering why I want to do this, I don't need this myself (yet), but I'm creating a flexible general purpose tool for folks and I'd like to give them the ability to override behavior as needed.

Cheers,
Ovid

New address of my CGI Course.


In reply to Using references as hash keys by Ovid

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.