Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://913653]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2018-06-24 07:41 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.