- 论坛徽章:
- 0
|
http://perldoc.perl.org/perlfaq4 ... ce-as-a-hash-key%3F
How can I use a reference as a hash key?
(contributed by brian d foy and Ben Morrow)
Hash keys are strings, so you can't really use a reference as the key. When you try to do that, perl turns the reference into its stringified form (for instance, HASH(0xDEADBEEF) ). From there you can't get back the reference from the stringified form, at least without doing some extra work on your own.
Remember that the entry in the hash will still be there even if the referenced variable goes out of scope, and that it is entirely possible for Perl to subsequently allocate a different variable at the same address. This will mean a new variable might accidentally be associated with the value for an old.
If you have Perl 5.10 or later, and you just want to store a value against the reference for lookup later, you can use the core Hash::Util::Fieldhash module. This will also handle renaming the keys if you use multiple threads (which causes all variables to be reallocated at new addresses, changing their stringification), and garbage-collecting the entries when the referenced variable goes out of scope.
If you actually need to be able to get a real reference back from each hash entry, you can use the Tie::RefHash module, which does the required work for you. |
|