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 = (Tie::StdHash);
25 # All methods provided by default, define only those needing overrides
26 # Accessors access the storage in %{$_[0]};
27 # TIEHASH should return a reference to the actual storage
33 @ISA = (Tie::ExtraHash);
35 # All methods provided by default, define only those needing overrides
36 # Accessors access the storage in %{$_[0][0]};
37 # TIEHASH should return an array reference with the first element being
38 # the reference to the actual storage
40 $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
41 delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1])
47 tie %new_hash, 'NewHash';
48 tie %new_std_hash, 'NewStdHash';
49 tie %new_extra_hash, 'NewExtraHash',
50 sub {warn "Doing \U$_[1]\E of $_[2].\n"};
54 This module provides some skeletal methods for hash-tying classes. See
55 L<perltie> for a list of the functions required in order to tie a hash
56 to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
57 as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> and
58 B<Tie::ExtraHash> packages
59 provide most methods for hashes described in L<perltie> (the exceptions
60 are C<UNTIE> and C<DESTROY>). They cause tied hashes to behave exactly like standard hashes,
61 and allow for selective overwriting of methods. B<Tie::Hash> grandfathers the
62 C<new> method: it is used if C<TIEHASH> is not defined
63 in the case a class forgets to include a C<TIEHASH> method.
65 For developers wishing to write their own tied hashes, the required methods
66 are briefly defined below. See the L<perltie> section for more detailed
67 descriptive, as well as example code:
71 =item TIEHASH classname, LIST
73 The method invoked by the command C<tie %hash, classname>. Associates a new
74 hash instance with the specified class. C<LIST> would represent additional
75 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
76 complete the association.
78 =item STORE this, key, value
80 Store datum I<value> into I<key> for the tied hash I<this>.
84 Retrieve the datum in I<key> for the tied hash I<this>.
88 Return the first key in the hash.
90 =item NEXTKEY this, lastkey
92 Return the next key in the hash.
94 =item EXISTS this, key
96 Verify that I<key> exists with the tied hash I<this>.
98 The B<Tie::Hash> implementation is a stub that simply croaks.
100 =item DELETE this, key
102 Delete the key I<key> from the tied hash I<this>.
106 Clear all values from the tied hash I<this>.
110 =head1 Inheriting from B<Tie::StdHash>
112 The accessor methods assume that the actual storage for the data in the tied
113 hash is in the hash referenced by C<tied(%tiedhash)>. Thus overwritten
114 C<TIEHASH> method should return a hash reference, and the remaining methods
115 should operate on the hash referenced by the first argument:
118 our @ISA = 'Tie::StdHash';
121 my $storage = bless {}, shift;
122 warn "New ReportHash created, stored in $storage.\n";
126 warn "Storing data with key $_[1] at $_[0].\n";
131 =head1 Inheriting from B<Tie::ExtraHash>
133 The accessor methods assume that the actual storage for the data in the tied
134 hash is in the hash referenced by C<(tied(%tiedhash))[0]>. Thus overwritten
135 C<TIEHASH> method should return an array reference with the first
136 element being a hash reference, and the remaining methods should operate on the
137 hash C<< %{ $_[0]->[0] } >>:
140 our @ISA = 'Tie::ExtraHash';
144 my $storage = bless [{}, @_], $class;
145 warn "New ReportHash created, stored in $storage.\n";
149 warn "Storing data with key $_[1] at $_[0].\n";
150 $_[0][0]{$_[1]} = $_[2]
153 The default C<TIEHASH> method stores "extra" arguments to tie() starting
154 from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
155 same storage algorithm as in TIEHASH subroutine above. Hence, a typical
156 package inheriting from B<Tie::ExtraHash> does not need to overwrite this
159 =head1 C<UNTIE> and C<DESTROY>
161 The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
162 B<Tie::StdHash>, or B<Tie::ExtraHash>. Tied hashes do not require
163 presense of these methods, but if defined, the methods will be called in
164 proper time, see L<perltie>.
166 If needed, these methods should be defined by the package inheriting from
167 B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>.
169 =head1 MORE INFORMATION
171 The packages relating to various DBM-related implementations (F<DB_File>,
172 F<NDBM_File>, etc.) show examples of general tied hashes, as does the
173 L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
174 good working examples.
179 use warnings::register;
190 if (defined &{"${pkg}::new"}) {
191 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
195 croak "$pkg doesn't define a TIEHASH method";
201 croak "$pkg doesn't define an EXISTS method";
206 my $key = $self->FIRSTKEY(@_);
209 while (defined $key) {
211 $key = $self->NEXTKEY(@_, $key);
213 foreach $key (@keys) {
214 $self->DELETE(@_, $key);
218 # The Tie::StdHash package implements standard perl hash behaviour.
219 # It exists to act as a base class for classes which only wish to
220 # alter some parts of their behaviour.
222 package Tie::StdHash;
223 # @ISA = qw(Tie::Hash); # would inherit new() only
225 sub TIEHASH { bless {}, $_[0] }
226 sub STORE { $_[0]->{$_[1]} = $_[2] }
227 sub FETCH { $_[0]->{$_[1]} }
228 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
229 sub NEXTKEY { each %{$_[0]} }
230 sub EXISTS { exists $_[0]->{$_[1]} }
231 sub DELETE { delete $_[0]->{$_[1]} }
232 sub CLEAR { %{$_[0]} = () }
234 package Tie::ExtraHash;
236 sub TIEHASH { my $p = shift; bless [{}, @_], $p }
237 sub STORE { $_[0][0]{$_[1]} = $_[2] }
238 sub FETCH { $_[0][0]{$_[1]} }
239 sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
240 sub NEXTKEY { each %{$_[0][0]} }
241 sub EXISTS { exists $_[0][0]->{$_[1]} }
242 sub DELETE { delete $_[0][0]->{$_[1]} }
243 sub CLEAR { %{$_[0][0]} = () }