Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Using references as hash keys

by xdg (Monsignor)
on Oct 16, 2005 at 12:04 UTC ( #500549=note: print w/replies, xml ) Need Help??

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.


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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://500549]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2023-02-08 23:03 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (44 votes). Check out past polls.