References as hash keys (Srinivasan's "Advanced Perl Programming")?

A

Adam Funk

Srinivasan's "Advanced Perl Programming" (1997) says

--> Perl requires hash keys to be strings, so when you use a
--> reference as a key, Perl uses the reference's string
--> representation.... But when you later retrieve the key from
--> this hash, it will remain a string and will thus be unusable
--> as a reference. It is possible that a future release of Perl
--> may lift the restriction that hash keys have to be strings,
--> but for the moment, the only recourse ... is to use the
--> Tie::RefHash module.... There are few algorithms that
--> require references to be used as hash keys and fewer still
--> that cannot live with this restriction.

Is this still (5.8.x) the case? What practical effect does this
limitation have?
 
A

Adam Funk

The practical effect is that if you do want to use references as hash
keys, you can, but if you need to dereference the references, you can't
store the references only in the hash as keys unless you use the
Tie::RefHash module. Without using that module, you need to store the
references separately from the hash, in an array for example, and
dereference those values and use them as keys to the hash.

Like the documents say, this is rarely necessary. I only contemplated
doing it once in many years of Perl programming, and, although it
worked, I soon found it unnecessary and abandoned it.

Interesting; thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top