Too many my $fh:s.
[p5sagit/p5-mst-13.2.git] / lib / Tie / RefHash.pm
1 package Tie::RefHash;
2
3 our $VERSION = 1.30;
4
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;
14     tie HASHVARIABLE, 'Tie::RefHash::Nestable', LIST;
15
16     untie HASHVARIABLE;
17
18 =head1 DESCRIPTION
19
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,
24 included as part of Tie::RefHash.
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
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
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
50     tie %h, 'Tie::RefHash::Nestable';
51     $h{$a}->{$b} = 1;
52     for (keys %h, keys %{$h{$a}}) {
53        print ref($_), "\n";
54     }
55
56 =head1 AUTHOR
57
58 Gurusamy Sarathy        gsar@activestate.com
59
60 'Nestable' by Ed Avis   ed@membled.com
61
62 =head1 VERSION
63
64 Version 1.30
65
66 =head1 SEE ALSO
67
68 perl(1), perlfunc(1), perltie(1)
69
70 =cut
71
72 use Tie::Hash;
73 use vars '@ISA';
74 @ISA = qw(Tie::Hash);
75 use strict;
76
77 sub 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
87 sub FETCH {
88   my($s, $k) = @_;
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   }
100 }
101
102 sub 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
113 sub DELETE {
114   my($s, $k) = @_;
115   (ref $k) ? delete($s->[0]{"$k"}) : delete($s->[1]{$k});
116 }
117
118 sub EXISTS {
119   my($s, $k) = @_;
120   (ref $k) ? exists($s->[0]{"$k"}) : exists($s->[1]{$k});
121 }
122
123 sub FIRSTKEY {
124   my $s = shift;
125   keys %{$s->[0]};      # reset iterator
126   keys %{$s->[1]};      # reset iterator
127   $s->[2] = 0;
128   $s->NEXTKEY;
129 }
130
131 sub 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
145 sub CLEAR {
146   my $s = shift;
147   $s->[2] = 0;
148   %{$s->[0]} = ();
149   %{$s->[1]} = ();
150 }
151
152 package Tie::RefHash::Nestable;
153 use vars '@ISA';
154 @ISA = 'Tie::RefHash';
155
156 sub 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
165 1;