http://qs1969.pair.com?node_id=500549


in reply to Using references as hash keys

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.

You could just extract the relevant code from is_deeply and provide a "deep_references" option in the constructor as to whether references should be compared by their refaddr or by their contents. For this, you'll need to keep a copy of the references, too, hashed on their refaddrs to be able to retrieve them for comparison. (I think you'll want a regular reference, not weak one -- and the semantic will then be that storing something under a reference increments the refcount for that reference.)

The other way to go is more like tye's suggestion around the hashing function, so if you want deep references, you freeze their contents into a string and hash on that.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.