Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: using references as keys in a hash.

by pfaut (Priest)
on Feb 23, 2003 at 02:11 UTC ( [id://237838]=note: print w/replies, xml ) Need Help??


in reply to using references as keys in a hash.

Perl can use the stringified reference as a key in a hash but that's not the same thing as using the reference itself. The reference is converted to a string to use as a key. The resulting key is just a scalar.

You might want to look into Tie::RefHash.

--- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';

Replies are listed 'Best First'.
Re: Re: using references as keys in a hash.
by habit_forming (Monk) on Feb 23, 2003 at 02:25 UTC
    Thank you for your reply. I understand that what perl is doing but the fact I cannot get the data back from the stringified reference is a bit... well... annoying.

      It is a tradeoff for speed. Defining the standard hash such that each key is forced to be a string allows the lookups to dereference keys to be very fast indeed.

      Since the requirement for objects (ie real, unstringified references) as keys seems to be rare, it makes sense to gain the extra speed for the common case. Since Tie::RefHash is available, the additional inconvenience for this rare case is small, so it seems still to be a good trade.

      If I understand correctly, the plan for perl6 is to allow access to alternate behaviours by a different mechanism (declare a property on the hash) which will make it easier to switch in much faster implementations than perl5's tying mechanism allows, but the underlying tradeoff will remain the same - the default will be the implementation that allows fastest key lookup, ie strings.

      Hugo
      Well, perhaps you can. One could write a piece of XS or Inline::C that tries to recreate the reference, by peeking what's at that memory address. But the value might have been garbage collected, and then you run into trouble.

      Abigail

        Devel::Pointer but don't tell anyone I told you:)


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found