5 Tie::RefHash - use references as hash keys
11 tie HASHVARIABLE, 'Tie::RefHash', LIST;
12 tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
18 This module provides the ability to use references as hash keys if you
19 first C<tie> the hash variable to this module. Normally, only the
20 keys of the tied hash itself are preserved as references; to use
21 references as keys in hashes-of-hashes, use Tie::RefHash::Nestable,
22 included as part of Tie::Hash.
24 It is implemented using the standard perl TIEHASH interface. Please
25 see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
27 The Nestable version works by looking for hash references being stored
28 and converting them to tied hashes so that they too can have
29 references as keys. This will happen without warning whenever you
30 store a reference to one of your own hashes in the tied hash.
35 tie %h, 'Tie::RefHash';
41 %h = ($a => 1, $b => 2, $c => 3, $d => 4, $e => 5);
48 tie %h, 'Tie::RefHash::Nestable';
50 for (keys %h, keys %{$h{$a}}) {
56 Gurusamy Sarathy gsar@activestate.com
60 Version 1.21 22 Jun 1999
64 perl(1), perlfunc(1), perltie(1)
78 $s->STORE(shift, shift);
86 if (defined $s->[0]{"$k"}) {
101 $s->[0]{"$k"} = [$k, $v];
111 (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
116 (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
121 keys %{$s->[0]}; # reset iterator
122 keys %{$s->[1]}; # reset iterator
131 if (($k, $v) = each %{$s->[0]}) {
132 return $s->[0]{"$k"}[0];
138 return each %{$s->[1]};
148 package Tie::RefHash::Nestable;
149 use vars '@ISA'; @ISA = qw(Tie::RefHash);
153 if (ref($v) eq 'HASH' and not tied %$v) {
155 tie %$v, ref($s), @elems;
157 $s->SUPER::STORE($k, $v);