5 Tie::Hash, Tie::StdHash - base class definitions for tied hashes
14 sub DELETE { ... } # Provides needed method
15 sub CLEAR { ... } # Overrides inherited method
21 @ISA = (Tie::StdHash);
23 # All methods provided by default, define only those needing overrides
29 tie %new_hash, 'NewHash';
30 tie %new_std_hash, 'NewStdHash';
34 This module provides some skeletal methods for hash-tying classes. See
35 L<perltie> for a list of the functions required in order to tie a hash
36 to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
37 as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> package
38 provides most methods required for hashes in L<perltie>. It inherits from
39 B<Tie::Hash>, and causes tied hashes to behave exactly like standard hashes,
40 allowing for selective overloading of methods. The C<new> method is provided
41 as grandfathering in the case a class forgets to include a C<TIEHASH> method.
43 For developers wishing to write their own tied hashes, the required methods
44 are briefly defined below. See the L<perltie> section for more detailed
45 descriptive, as well as example code:
49 =item TIEHASH classname, LIST
51 The method invoked by the command C<tie %hash, classname>. Associates a new
52 hash instance with the specified class. C<LIST> would represent additional
53 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
54 complete the association.
56 =item STORE this, key, value
58 Store datum I<value> into I<key> for the tied hash I<this>.
62 Retrieve the datum in I<key> for the tied hash I<this>.
66 Return the (key, value) pair for the first key in the hash.
68 =item NEXTKEY this, lastkey
70 Return the next key for the hash.
72 =item EXISTS this, key
74 Verify that I<key> exists with the tied hash I<this>.
76 The B<Tie::Hash> implementation is a stub that simply croaks.
78 =item DELETE this, key
80 Delete the key I<key> from the tied hash I<this>.
84 Clear all values from the tied hash I<this>.
90 The L<perltie> documentation includes a method called C<DESTROY> as
91 a necessary method for tied hashes. Neither B<Tie::Hash> nor B<Tie::StdHash>
92 define a default for this method. This is a standard for class packages,
93 but may be omitted in favor of a simple default.
95 =head1 MORE INFORMATION
97 The packages relating to various DBM-related implementations (F<DB_File>,
98 F<NDBM_File>, etc.) show examples of general tied hashes, as does the
99 L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
100 good working examples.
105 use warnings::register;
116 if (defined &{"${pkg}::new"}) {
117 warnings::warn "WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing"
118 if warnings::enabled();
122 croak "$pkg doesn't define a TIEHASH method";
128 croak "$pkg doesn't define an EXISTS method";
133 my $key = $self->FIRSTKEY(@_);
136 while (defined $key) {
138 $key = $self->NEXTKEY(@_, $key);
140 foreach $key (@keys) {
141 $self->DELETE(@_, $key);
145 # The Tie::StdHash package implements standard perl hash behaviour.
146 # It exists to act as a base class for classes which only wish to
147 # alter some parts of their behaviour.
149 package Tie::StdHash;
150 @ISA = qw(Tie::Hash);
152 sub TIEHASH { bless {}, $_[0] }
153 sub STORE { $_[0]->{$_[1]} = $_[2] }
154 sub FETCH { $_[0]->{$_[1]} }
155 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
156 sub NEXTKEY { each %{$_[0]} }
157 sub EXISTS { exists $_[0]->{$_[1]} }
158 sub DELETE { delete $_[0]->{$_[1]} }
159 sub CLEAR { %{$_[0]} = () }