Too many my $fh:s.
[p5sagit/p5-mst-13.2.git] / lib / Tie / RefHash.pm
CommitLineData
5f05dabc 1package Tie::RefHash;
2
105cd853 3our $VERSION = 1.30;
b75c8c73 4
5f05dabc 5=head1 NAME
6
7Tie::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 20This module provides the ability to use references as hash keys if you
21first C<tie> the hash variable to this module. Normally, only the
22keys of the tied hash itself are preserved as references; to use
23references as keys in hashes-of-hashes, use Tie::RefHash::Nestable,
8b2fd6cc 24included as part of Tie::RefHash.
5f05dabc 25
26It is implemented using the standard perl TIEHASH interface. Please
27see the C<tie> entry in perlfunc(1) and perltie(1) for more information.
28
778e8f97 29The Nestable version works by looking for hash references being stored
30and converting them to tied hashes so that they too can have
31references as keys. This will happen without warning whenever you
32store 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 58Gurusamy Sarathy gsar@activestate.com
5f05dabc 59
d3f88289 60'Nestable' by Ed Avis ed@membled.com
61
5f05dabc 62=head1 VERSION
63
105cd853 64Version 1.30
5f05dabc 65
66=head1 SEE ALSO
67
68perl(1), perlfunc(1), perltie(1)
69
70=cut
71
5f05dabc 72use Tie::Hash;
8b2fd6cc 73use vars '@ISA';
5f05dabc 74@ISA = qw(Tie::Hash);
75use strict;
76
77sub TIEHASH {
78 my $c = shift;
79 my $s = [];
80 bless $s, $c;
81 while (@_) {
82 $s->STORE(shift, shift);
83 }
84 return $s;
85}
86
87sub FETCH {
88 my($s, $k) = @_;
778e8f97 89 if (ref $k) {
90 if (defined $s->[0]{"$k"}) {
91 $s->[0]{"$k"}[1];
92 }
93 else {
94 undef;
95 }
96 }
97 else {
98 $s->[1]{$k};
99 }
5f05dabc 100}
101
102sub STORE {
103 my($s, $k, $v) = @_;
104 if (ref $k) {
105 $s->[0]{"$k"} = [$k, $v];
106 }
107 else {
108 $s->[1]{$k} = $v;
109 }
110 $v;
111}
112
113sub DELETE {
114 my($s, $k) = @_;
115 (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
116}
117
118sub EXISTS {
119 my($s, $k) = @_;
120 (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
121}
122
123sub FIRSTKEY {
124 my $s = shift;
da41ffc5 125 keys %{$s->[0]}; # reset iterator
126 keys %{$s->[1]}; # reset iterator
5f05dabc 127 $s->[2] = 0;
128 $s->NEXTKEY;
129}
130
131sub NEXTKEY {
132 my $s = shift;
133 my ($k, $v);
134 if (!$s->[2]) {
135 if (($k, $v) = each %{$s->[0]}) {
136 return $s->[0]{"$k"}[0];
137 }
138 else {
139 $s->[2] = 1;
140 }
141 }
142 return each %{$s->[1]};
143}
144
145sub CLEAR {
146 my $s = shift;
147 $s->[2] = 0;
148 %{$s->[0]} = ();
149 %{$s->[1]} = ();
150}
151
778e8f97 152package Tie::RefHash::Nestable;
8b2fd6cc 153use vars '@ISA';
154@ISA = 'Tie::RefHash';
778e8f97 155
156sub STORE {
157 my($s, $k, $v) = @_;
158 if (ref($v) eq 'HASH' and not tied %$v) {
159 my @elems = %$v;
160 tie %$v, ref($s), @elems;
161 }
162 $s->SUPER::STORE($k, $v);
163}
164
5f05dabc 1651;