Commit | Line | Data |
5f05dabc |
1 | package Tie::RefHash; |
2 | |
296b60dd |
3 | our $VERSION = 1.32; |
b75c8c73 |
4 | |
5f05dabc |
5 | =head1 NAME |
6 | |
7 | Tie::RefHash - use references as hash keys |
8 | |
9 | =head1 SYNOPSIS |
10 | |
11 | require 5.004; |
12 | use Tie::RefHash; |
13 | tie HASHVARIABLE, 'Tie::RefHash', LIST; |
778e8f97 |
14 | tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST; |
5f05dabc |
15 | |
16 | untie HASHVARIABLE; |
17 | |
18 | =head1 DESCRIPTION |
19 | |
778e8f97 |
20 | This module provides the ability to use references as hash keys if you |
21 | first C<tie> the hash variable to this module. Normally, only the |
22 | keys of the tied hash itself are preserved as references; to use |
23 | references as keys in hashes-of-hashes, use Tie::RefHash::Nestable, |
8b2fd6cc |
24 | included as part of Tie::RefHash. |
5f05dabc |
25 | |
26 | It is implemented using the standard perl TIEHASH interface. Please |
27 | see the C<tie> entry in perlfunc(1) and perltie(1) for more information. |
28 | |
778e8f97 |
29 | The Nestable version works by looking for hash references being stored |
30 | and converting them to tied hashes so that they too can have |
31 | references as keys. This will happen without warning whenever you |
32 | store a reference to one of your own hashes in the tied hash. |
33 | |
5f05dabc |
34 | =head1 EXAMPLE |
35 | |
36 | use Tie::RefHash; |
37 | tie %h, 'Tie::RefHash'; |
38 | $a = []; |
39 | $b = {}; |
40 | $c = \*main; |
41 | $d = \"gunk"; |
42 | $e = sub { 'foo' }; |
43 | %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5); |
44 | $a->[0] = 'foo'; |
45 | $b->{foo} = 'bar'; |
46 | for (keys %h) { |
47 | print ref($_), "\n"; |
48 | } |
49 | |
778e8f97 |
50 | tie %h, 'Tie::RefHash::Nestable'; |
51 | $h{$a}->{$b} = 1; |
52 | for (keys %h, keys %{$h{$a}}) { |
53 | print ref($_), "\n"; |
54 | } |
5f05dabc |
55 | |
56 | =head1 AUTHOR |
57 | |
da41ffc5 |
58 | Gurusamy Sarathy gsar@activestate.com |
5f05dabc |
59 | |
d3f88289 |
60 | 'Nestable' by Ed Avis ed@membled.com |
61 | |
5f05dabc |
62 | =head1 VERSION |
63 | |
296b60dd |
64 | Version 1.32 |
5f05dabc |
65 | |
66 | =head1 SEE ALSO |
67 | |
68 | perl(1), perlfunc(1), perltie(1) |
69 | |
70 | =cut |
71 | |
5f05dabc |
72 | use Tie::Hash; |
8b2fd6cc |
73 | use vars '@ISA'; |
5f05dabc |
74 | @ISA = qw(Tie::Hash); |
75 | use strict; |
76 | |
05d3035d |
77 | require overload; # to support objects with overloaded "" |
60ad8d77 |
78 | |
5f05dabc |
79 | sub TIEHASH { |
80 | my $c = shift; |
81 | my $s = []; |
82 | bless $s, $c; |
83 | while (@_) { |
84 | $s->STORE(shift, shift); |
85 | } |
86 | return $s; |
87 | } |
88 | |
89 | sub FETCH { |
90 | my($s, $k) = @_; |
778e8f97 |
91 | if (ref $k) { |
60ad8d77 |
92 | my $kstr = overload::StrVal($k); |
93 | if (defined $s->[0]{$kstr}) { |
94 | $s->[0]{$kstr}[1]; |
778e8f97 |
95 | } |
96 | else { |
97 | undef; |
98 | } |
99 | } |
100 | else { |
101 | $s->[1]{$k}; |
102 | } |
5f05dabc |
103 | } |
104 | |
105 | sub STORE { |
106 | my($s, $k, $v) = @_; |
107 | if (ref $k) { |
60ad8d77 |
108 | $s->[0]{overload::StrVal($k)} = [$k, $v]; |
5f05dabc |
109 | } |
110 | else { |
111 | $s->[1]{$k} = $v; |
112 | } |
113 | $v; |
114 | } |
115 | |
116 | sub DELETE { |
117 | my($s, $k) = @_; |
18592d64 |
118 | (ref $k) |
119 | ? (delete($s->[0]{overload::StrVal($k)}) || [])->[1] |
120 | : delete($s->[1]{$k}); |
5f05dabc |
121 | } |
122 | |
123 | sub EXISTS { |
124 | my($s, $k) = @_; |
60ad8d77 |
125 | (ref $k) ? exists($s->[0]{overload::StrVal($k)}) : exists($s->[1]{$k}); |
5f05dabc |
126 | } |
127 | |
128 | sub FIRSTKEY { |
129 | my $s = shift; |
da41ffc5 |
130 | keys %{$s->[0]}; # reset iterator |
131 | keys %{$s->[1]}; # reset iterator |
60ad8d77 |
132 | $s->[2] = 0; # flag for iteration, see NEXTKEY |
5f05dabc |
133 | $s->NEXTKEY; |
134 | } |
135 | |
136 | sub NEXTKEY { |
137 | my $s = shift; |
138 | my ($k, $v); |
139 | if (!$s->[2]) { |
140 | if (($k, $v) = each %{$s->[0]}) { |
60ad8d77 |
141 | return $v->[0]; |
5f05dabc |
142 | } |
143 | else { |
144 | $s->[2] = 1; |
145 | } |
146 | } |
147 | return each %{$s->[1]}; |
148 | } |
149 | |
150 | sub CLEAR { |
151 | my $s = shift; |
152 | $s->[2] = 0; |
153 | %{$s->[0]} = (); |
154 | %{$s->[1]} = (); |
155 | } |
156 | |
778e8f97 |
157 | package Tie::RefHash::Nestable; |
8b2fd6cc |
158 | use vars '@ISA'; |
159 | @ISA = 'Tie::RefHash'; |
778e8f97 |
160 | |
161 | sub STORE { |
162 | my($s, $k, $v) = @_; |
163 | if (ref($v) eq 'HASH' and not tied %$v) { |
164 | my @elems = %$v; |
165 | tie %$v, ref($s), @elems; |
166 | } |
167 | $s->SUPER::STORE($k, $v); |
168 | } |
169 | |
5f05dabc |
170 | 1; |