1Tie::RefHash(3pm) Perl Programmers Reference Guide Tie::RefHash(3pm)
2
3
4
6 Tie::RefHash - use references as hash keys
7
9 require 5.004;
10 use Tie::RefHash;
11 tie HASHVARIABLE, 'Tie::RefHash', LIST;
12 tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
13
14 untie HASHVARIABLE;
15
17 This module provides the ability to use references as hash keys if you
18 first "tie" the hash variable to this module. Normally, only the keys
19 of the tied hash itself are preserved as references; to use references
20 as keys in hashes-of-hashes, use Tie::RefHash::Nestable, included as
21 part of Tie::RefHash.
22
23 It is implemented using the standard perl TIEHASH interface. Please
24 see the "tie" entry in perlfunc(1) and perltie(1) for more information.
25
26 The Nestable version works by looking for hash references being stored
27 and converting them to tied hashes so that they too can have references
28 as keys. This will happen without warning whenever you store a refer‐
29 ence to one of your own hashes in the tied hash.
30
32 use Tie::RefHash;
33 tie %h, 'Tie::RefHash';
34 $a = [];
35 $b = {};
36 $c = \*main;
37 $d = \"gunk";
38 $e = sub { 'foo' };
39 %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
40 $a->[0] = 'foo';
41 $b->{foo} = 'bar';
42 for (keys %h) {
43 print ref($_), "\n";
44 }
45
46 tie %h, 'Tie::RefHash::Nestable';
47 $h{$a}->{$b} = 1;
48 for (keys %h, keys %{$h{$a}}) {
49 print ref($_), "\n";
50 }
51
53 Gurusamy Sarathy gsar@activestate.com
54
55 'Nestable' by Ed Avis ed@membled.com
56
58 Version 1.32
59
61 perl(1), perlfunc(1), perltie(1)
62
63
64
65perl v5.8.8 2001-09-21 Tie::RefHash(3pm)