5 TieHash, TieHash::Std - base class definitions for tied hashes
14 sub DELETE { ... } # Provides needed method
15 sub CLEAR { ... } # Overrides inherited method
21 @ISA = (TieHash::Std);
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<perlfunc/tie> for a list of the functions required in order to tie a hash
36 to a package. The basic B<TieHash> package provides a C<new> method, as well
37 as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<TieHash::Std> package
38 provides most methods required for hashes in L<perlfunc/tie>. It inherits from
39 B<TieHash>, and causes tied hashes to behave exactly like standard hashes,
40 allowing for selective overloading of methods. The B<new> method is provided
41 as grandfathering in the case a class forgets to include a B<TIEHASH> method.
43 For developers wishing to write their own tied hashes, the required methods
46 =item TIEHASH classname, LIST
48 The method invoked by the command C<tie %hash, class>. Associates a new
49 hash instance with the specified class. C<LIST> would represent additional
50 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
51 complete the association.
53 =item STORE this, key, value
55 Store datum I<value> into I<key> for the tied hash I<this>.
59 Retrieve the datum in I<key> for the tied hash I<this>.
63 Return the (key, value) pair for the first key in the hash.
65 =item NEXTKEY this, lastkey
67 Return the next (key, value) pair for the hash.
69 =item EXISTS this, key
71 Verify that I<key> exists with the tied hash I<this>.
73 =item DELETE this, key
75 Delete the key I<key> from the tied hash I<this>.
79 Clear all values from the tied hash I<this>.
85 The L<perlfunc/tie> documentation includes a method called C<DESTROY> as
86 a necessary method for tied hashes. Neither B<TieHash> nor B<TieHash::Std>
87 define a default for this method.
89 The C<CLEAR> method provided by these two packages is not listed in the
90 L<perlfunc/tie> section.
92 =head1 MORE INFORMATION
94 The packages relating to various DBM-related implemetations (F<DB_File>,
95 F<NDBM_File>, etc.) show examples of general tied hashes, as does the
96 L<Config> module. While these do not utilize B<TieHash>, they serve as
97 good working examples.
112 if (defined &{"{$pkg}::new"}) {
113 carp "WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing"
118 croak "$pkg doesn't define a TIEHASH method";
124 croak "$pkg doesn't define an EXISTS method";
129 my $key = $self->FIRSTKEY(@_);
132 while (defined $key) {
134 $key = $self->NEXTKEY(@_, $key);
136 foreach $key (@keys) {
137 $self->DELETE(@_, $key);
141 # The TieHash::Std package implements standard perl hash behaviour.
142 # It exists to act as a base class for classes which only wish to
143 # alter some parts of their behaviour.
145 package TieHash::Std;
148 sub TIEHASH { bless {}, $_[0] }
149 sub STORE { $_[0]->{$_[1]} = $_[2] }
150 sub FETCH { $_[0]->{$_[1]} }
151 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
152 sub NEXTKEY { each %{$_[0]} }
153 sub EXISTS { exists $_[0]->{$_[1]} }
154 sub DELETE { delete $_[0]->{$_[1]} }
155 sub CLEAR { %{$_[0]} = () }