[perl #24120] Tie::Hash documentation has broken code
[p5sagit/p5-mst-13.2.git] / lib / Tie / Hash.pm
CommitLineData
64d0c973 1package Tie::Hash;
cb1a09d0 2
b75c8c73 3our $VERSION = '1.00';
4
cb1a09d0 5=head1 NAME
6
d5582e24 7Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes
cb1a09d0 8
9=head1 SYNOPSIS
10
11 package NewHash;
64d0c973 12 require Tie::Hash;
bbc7dcd2 13
64d0c973 14 @ISA = (Tie::Hash);
bbc7dcd2 15
cb1a09d0 16 sub DELETE { ... } # Provides needed method
17 sub CLEAR { ... } # Overrides inherited method
bbc7dcd2 18
19
cb1a09d0 20 package NewStdHash;
64d0c973 21 require Tie::Hash;
bbc7dcd2 22
64d0c973 23 @ISA = (Tie::StdHash);
bbc7dcd2 24
cb1a09d0 25 # All methods provided by default, define only those needing overrides
d5582e24 26 # Accessors access the storage in %{$_[0]};
27 # TIEHANDLE should return a reference to the actual storage
cb1a09d0 28 sub DELETE { ... }
bbc7dcd2 29
d5582e24 30 package NewExtraHash;
31 require Tie::Hash;
32
33 @ISA = (Tie::ExtraHash);
34
35 # All methods provided by default, define only those needing overrides
36 # Accessors access the storage in %{$_[0][0]};
37 # TIEHANDLE should return an array reference with the first element being
38 # the reference to the actual storage
39 sub DELETE {
40 $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
41 delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1]) }
42
bbc7dcd2 43
cb1a09d0 44 package main;
bbc7dcd2 45
c954a603 46 tie %new_hash, 'NewHash';
47 tie %new_std_hash, 'NewStdHash';
d5582e24 48 tie %new_extra_hash, 'NewExtraHash',
49 sub {warn "Doing \U$_[1]\E of $_[2].\n"};
cb1a09d0 50
51=head1 DESCRIPTION
52
53This module provides some skeletal methods for hash-tying classes. See
64d0c973 54L<perltie> for a list of the functions required in order to tie a hash
55to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
d5582e24 56as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> and
57B<Tie::ExtraHash> packages
58provide most methods for hashes described in L<perltie> (the exceptions
59are C<UNTIE> and C<DESTROY>). They cause tied hashes to behave exactly like standard hashes,
60and allow for selective overwriting of methods. B<Tie::Hash> grandfathers the
61C<new> method: it is used if C<TIEHASH> is not defined
62in the case a class forgets to include a C<TIEHASH> method.
cb1a09d0 63
64For developers wishing to write their own tied hashes, the required methods
64d0c973 65are briefly defined below. See the L<perltie> section for more detailed
66descriptive, as well as example code:
67
bbc7dcd2 68=over 4
cb1a09d0 69
70=item TIEHASH classname, LIST
71
64d0c973 72The method invoked by the command C<tie %hash, classname>. Associates a new
cb1a09d0 73hash instance with the specified class. C<LIST> would represent additional
74arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
75complete the association.
76
77=item STORE this, key, value
78
79Store datum I<value> into I<key> for the tied hash I<this>.
80
81=item FETCH this, key
82
83Retrieve the datum in I<key> for the tied hash I<this>.
84
85=item FIRSTKEY this
86
51c7a601 87Return the first key in the hash.
cb1a09d0 88
89=item NEXTKEY this, lastkey
90
51c7a601 91Return the next key in the hash.
cb1a09d0 92
93=item EXISTS this, key
94
95Verify that I<key> exists with the tied hash I<this>.
96
01020589 97The B<Tie::Hash> implementation is a stub that simply croaks.
98
cb1a09d0 99=item DELETE this, key
100
101Delete the key I<key> from the tied hash I<this>.
102
103=item CLEAR this
104
105Clear all values from the tied hash I<this>.
106
107=back
108
d5582e24 109=head1 Inheriting from B<Tie::StdHash>
110
111The accessor methods assume that the actual storage for the data in the tied
112hash is in the hash referenced by C<tied(%tiedhash)>. Thus overwritten
113C<TIEHANDLE> method should return a hash reference, and the remaining methods
114should operate on the hash referenced by the first argument:
115
116 package ReportHash;
117 our @ISA = 'Tie::StdHash';
118
119 sub TIEHASH {
120 my $storage = bless {}, shift;
121 warn "New ReportHash created, stored in $storage.\n";
122 $storage
123 }
124 sub STORE {
125 warn "Storing data with key $_[1] at $_[0].\n";
126 $_[0]{$_[1]} = $_[2]
127 }
128
cb1a09d0 129
d5582e24 130=head1 Inheriting from B<Tie::ExtraHash>
131
132The accessor methods assume that the actual storage for the data in the tied
133hash is in the hash referenced by C<(tied(%tiedhash))[0]>. Thus overwritten
134C<TIEHANDLE> method should return an array reference with the first
135element being a hash reference, and the remaining methods should operate on the
194eaab5 136hash C<< %{ $_[0]->[0] } >>:
d5582e24 137
138 package ReportHash;
1db7d662 139 our @ISA = 'Tie::ExtraHash';
d5582e24 140
141 sub TIEHASH {
1db7d662 142 my $class = shift;
143 my $storage = bless [{}, @_], $class;
d5582e24 144 warn "New ReportHash created, stored in $storage.\n";
1db7d662 145 $storage;
d5582e24 146 }
147 sub STORE {
148 warn "Storing data with key $_[1] at $_[0].\n";
149 $_[0][0]{$_[1]} = $_[2]
150 }
151
152The default C<TIEHANDLE> method stores "extra" arguments to tie() starting
153from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
154same storage algorithm as in TIEHASH subroutine above. Hence, a typical
155package inheriting from B<Tie::ExtraHash> does not need to overwrite this
156method.
157
158=head1 C<UNTIE> and C<DESTROY>
159
160The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
161B<Tie::StdHash>, or B<Tie::ExtraHash>. Tied hashes do not require
162presense of these methods, but if defined, the methods will be called in
163proper time, see L<perltie>.
164
165If needed, these methods should be defined by the package inheriting from
166B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>.
cb1a09d0 167
168=head1 MORE INFORMATION
169
8dcee03e 170The packages relating to various DBM-related implementations (F<DB_File>,
cb1a09d0 171F<NDBM_File>, etc.) show examples of general tied hashes, as does the
64d0c973 172L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
cb1a09d0 173good working examples.
174
175=cut
a6006777 176
a0d0e21e 177use Carp;
d3a7d8c7 178use warnings::register;
a0d0e21e 179
180sub new {
4633a7c4 181 my $pkg = shift;
182 $pkg->TIEHASH(@_);
a0d0e21e 183}
184
185# Grandfather "new"
186
187sub TIEHASH {
4633a7c4 188 my $pkg = shift;
cc6b7395 189 if (defined &{"${pkg}::new"}) {
7e6d00f8 190 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
4633a7c4 191 $pkg->new(@_);
a0d0e21e 192 }
193 else {
4633a7c4 194 croak "$pkg doesn't define a TIEHASH method";
a0d0e21e 195 }
196}
197
198sub EXISTS {
4633a7c4 199 my $pkg = ref $_[0];
200 croak "$pkg doesn't define an EXISTS method";
a0d0e21e 201}
202
203sub CLEAR {
204 my $self = shift;
205 my $key = $self->FIRSTKEY(@_);
206 my @keys;
207
208 while (defined $key) {
209 push @keys, $key;
210 $key = $self->NEXTKEY(@_, $key);
211 }
212 foreach $key (@keys) {
213 $self->DELETE(@_, $key);
214 }
215}
216
64d0c973 217# The Tie::StdHash package implements standard perl hash behaviour.
748a9306 218# It exists to act as a base class for classes which only wish to
219# alter some parts of their behaviour.
220
64d0c973 221package Tie::StdHash;
d5582e24 222# @ISA = qw(Tie::Hash); # would inherit new() only
748a9306 223
224sub TIEHASH { bless {}, $_[0] }
225sub STORE { $_[0]->{$_[1]} = $_[2] }
226sub FETCH { $_[0]->{$_[1]} }
227sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
228sub NEXTKEY { each %{$_[0]} }
229sub EXISTS { exists $_[0]->{$_[1]} }
230sub DELETE { delete $_[0]->{$_[1]} }
231sub CLEAR { %{$_[0]} = () }
232
d5582e24 233package Tie::ExtraHash;
234
235sub TIEHASH { my $p = shift; bless [{}, @_], $p }
236sub STORE { $_[0][0]{$_[1]} = $_[2] }
237sub FETCH { $_[0][0]{$_[1]} }
238sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
239sub NEXTKEY { each %{$_[0][0]} }
240sub EXISTS { exists $_[0][0]->{$_[1]} }
241sub DELETE { delete $_[0][0]->{$_[1]} }
242sub CLEAR { %{$_[0][0]} = () }
243
a0d0e21e 2441;