7 Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes
16 sub DELETE { ... } # Provides needed method
17 sub CLEAR { ... } # Overrides inherited method
23 @ISA = qw(Tie::StdHash);
25 # All methods provided by default, define
26 # only those needing overrides
27 # Accessors access the storage in %{$_[0]};
28 # TIEHASH should return a reference to the actual storage
34 @ISA = qw(Tie::ExtraHash);
36 # All methods provided by default, define
37 # only those needing overrides
38 # Accessors access the storage in %{$_[0][0]};
39 # TIEHASH should return an array reference with the first element
40 # being the reference to the actual storage
42 $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
43 delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1])
49 tie %new_hash, 'NewHash';
50 tie %new_std_hash, 'NewStdHash';
51 tie %new_extra_hash, 'NewExtraHash',
52 sub {warn "Doing \U$_[1]\E of $_[2].\n"};
56 This module provides some skeletal methods for hash-tying classes. See
57 L<perltie> for a list of the functions required in order to tie a hash
58 to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
59 as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> and
60 B<Tie::ExtraHash> packages
61 provide most methods for hashes described in L<perltie> (the exceptions
62 are C<UNTIE> and C<DESTROY>). They cause tied hashes to behave exactly like standard hashes,
63 and allow for selective overwriting of methods. B<Tie::Hash> grandfathers the
64 C<new> method: it is used if C<TIEHASH> is not defined
65 in the case a class forgets to include a C<TIEHASH> method.
67 For developers wishing to write their own tied hashes, the required methods
68 are briefly defined below. See the L<perltie> section for more detailed
69 descriptive, as well as example code:
73 =item TIEHASH classname, LIST
75 The method invoked by the command C<tie %hash, classname>. Associates a new
76 hash instance with the specified class. C<LIST> would represent additional
77 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
78 complete the association.
80 =item STORE this, key, value
82 Store datum I<value> into I<key> for the tied hash I<this>.
86 Retrieve the datum in I<key> for the tied hash I<this>.
90 Return the first key in the hash.
92 =item NEXTKEY this, lastkey
94 Return the next key in the hash.
96 =item EXISTS this, key
98 Verify that I<key> exists with the tied hash I<this>.
100 The B<Tie::Hash> implementation is a stub that simply croaks.
102 =item DELETE this, key
104 Delete the key I<key> from the tied hash I<this>.
108 Clear all values from the tied hash I<this>.
112 Returns what evaluating the hash in scalar context yields.
114 B<Tie::Hash> does not implement this method (but B<Tie::StdHash>
115 and B<Tie::ExtraHash> do).
119 =head1 Inheriting from B<Tie::StdHash>
121 The accessor methods assume that the actual storage for the data in the tied
122 hash is in the hash referenced by C<tied(%tiedhash)>. Thus overwritten
123 C<TIEHASH> method should return a hash reference, and the remaining methods
124 should operate on the hash referenced by the first argument:
127 our @ISA = 'Tie::StdHash';
130 my $storage = bless {}, shift;
131 warn "New ReportHash created, stored in $storage.\n";
135 warn "Storing data with key $_[1] at $_[0].\n";
140 =head1 Inheriting from B<Tie::ExtraHash>
142 The accessor methods assume that the actual storage for the data in the tied
143 hash is in the hash referenced by C<(tied(%tiedhash))-E<gt>[0]>. Thus overwritten
144 C<TIEHASH> method should return an array reference with the first
145 element being a hash reference, and the remaining methods should operate on the
146 hash C<< %{ $_[0]->[0] } >>:
149 our @ISA = 'Tie::ExtraHash';
153 my $storage = bless [{}, @_], $class;
154 warn "New ReportHash created, stored in $storage.\n";
158 warn "Storing data with key $_[1] at $_[0].\n";
159 $_[0][0]{$_[1]} = $_[2]
162 The default C<TIEHASH> method stores "extra" arguments to tie() starting
163 from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
164 same storage algorithm as in TIEHASH subroutine above. Hence, a typical
165 package inheriting from B<Tie::ExtraHash> does not need to overwrite this
168 =head1 C<SCALAR>, C<UNTIE> and C<DESTROY>
170 The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
171 B<Tie::StdHash>, or B<Tie::ExtraHash>. Tied hashes do not require
172 presence of these methods, but if defined, the methods will be called in
173 proper time, see L<perltie>.
175 C<SCALAR> is only defined in B<Tie::StdHash> and B<Tie::ExtraHash>.
177 If needed, these methods should be defined by the package inheriting from
178 B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>. See L<perltie/"SCALAR">
179 to find out what happens when C<SCALAR> does not exist.
181 =head1 MORE INFORMATION
183 The packages relating to various DBM-related implementations (F<DB_File>,
184 F<NDBM_File>, etc.) show examples of general tied hashes, as does the
185 L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
186 good working examples.
191 use warnings::register;
202 my $pkg_new = $pkg -> can ('new');
204 if ($pkg_new and $pkg ne __PACKAGE__) {
205 my $my_new = __PACKAGE__ -> can ('new');
206 if ($pkg_new == $my_new) {
210 croak "$pkg must define either a TIEHASH() or a new() method";
213 warnings::warnif ("WARNING: calling ${pkg}->new since " .
214 "${pkg}->TIEHASH is missing");
218 croak "$pkg doesn't define a TIEHASH method";
224 croak "$pkg doesn't define an EXISTS method";
229 my $key = $self->FIRSTKEY(@_);
232 while (defined $key) {
234 $key = $self->NEXTKEY(@_, $key);
236 foreach $key (@keys) {
237 $self->DELETE(@_, $key);
241 # The Tie::StdHash package implements standard perl hash behaviour.
242 # It exists to act as a base class for classes which only wish to
243 # alter some parts of their behaviour.
245 package Tie::StdHash;
246 # @ISA = qw(Tie::Hash); # would inherit new() only
248 sub TIEHASH { bless {}, $_[0] }
249 sub STORE { $_[0]->{$_[1]} = $_[2] }
250 sub FETCH { $_[0]->{$_[1]} }
251 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
252 sub NEXTKEY { each %{$_[0]} }
253 sub EXISTS { exists $_[0]->{$_[1]} }
254 sub DELETE { delete $_[0]->{$_[1]} }
255 sub CLEAR { %{$_[0]} = () }
256 sub SCALAR { scalar %{$_[0]} }
258 package Tie::ExtraHash;
260 sub TIEHASH { my $p = shift; bless [{}, @_], $p }
261 sub STORE { $_[0][0]{$_[1]} = $_[2] }
262 sub FETCH { $_[0][0]{$_[1]} }
263 sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
264 sub NEXTKEY { each %{$_[0][0]} }
265 sub EXISTS { exists $_[0][0]->{$_[1]} }
266 sub DELETE { delete $_[0][0]->{$_[1]} }
267 sub CLEAR { %{$_[0][0]} = () }
268 sub SCALAR { scalar %{$_[0][0]} }