Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: reference as hash keys and values

by armstd (Friar)
on Jul 11, 2011 at 05:08 UTC ( #913653=note: print w/ replies, xml ) Need Help??


in reply to reference as hash keys and values

As already pointed out, references cannot effectively be used as hash keys.

I eventually came up with a use case once, where I really wanted to be able to identify my objects uniquely, for building Sets of unique objects and such. With a tip from a friend, I came up with the following scheme. Instead of using the reference itself, use a reliable string representation of the reference. Scalar::Util::refaddr() can provide such, for a single-process oriented use-case anyway.

my $uniqueID = Scalar::Util::refaddr( $self ); $objectsHash->{$uniqueID} = $self;

From then on, given any structure using those IDs as hash keys, you can retrieve the referenced structure/object from the $objectsHash index. Inside-out Objects use essentially the same means for a slightly different purpose. Track a unique objectID, not the object itself.

In my ideal world, concrete objects provide the override to identify themselves, but my abstract classes still want to be able to do something in the absence of that override.

--Dave


Comment on Re: reference as hash keys and values
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-09-22 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls