7 Tie::Hash, Tie::StdHash - 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
31 tie %new_hash, 'NewHash';
32 tie %new_std_hash, 'NewStdHash';
36 This module provides some skeletal methods for hash-tying classes. See
37 L<perltie> for a list of the functions required in order to tie a hash
38 to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
39 as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> package
40 provides most methods required for hashes in L<perltie>. It inherits from
41 B<Tie::Hash>, and causes tied hashes to behave exactly like standard hashes,
42 allowing for selective overloading of methods. The C<new> method is provided
43 as grandfathering in the case a class forgets to include a C<TIEHASH> method.
45 For developers wishing to write their own tied hashes, the required methods
46 are briefly defined below. See the L<perltie> section for more detailed
47 descriptive, as well as example code:
51 =item TIEHASH classname, LIST
53 The method invoked by the command C<tie %hash, classname>. Associates a new
54 hash instance with the specified class. C<LIST> would represent additional
55 arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
56 complete the association.
58 =item STORE this, key, value
60 Store datum I<value> into I<key> for the tied hash I<this>.
64 Retrieve the datum in I<key> for the tied hash I<this>.
68 Return the first key in the hash.
70 =item NEXTKEY this, lastkey
72 Return the next key in the hash.
74 =item EXISTS this, key
76 Verify that I<key> exists with the tied hash I<this>.
78 The B<Tie::Hash> implementation is a stub that simply croaks.
80 =item DELETE this, key
82 Delete the key I<key> from the tied hash I<this>.
86 Clear all values from the tied hash I<this>.
92 The L<perltie> documentation includes a method called C<DESTROY> as
93 a necessary method for tied hashes. Neither B<Tie::Hash> nor B<Tie::StdHash>
94 define a default for this method. This is a standard for class packages,
95 but may be omitted in favor of a simple default.
97 =head1 MORE INFORMATION
99 The packages relating to various DBM-related implementations (F<DB_File>,
100 F<NDBM_File>, etc.) show examples of general tied hashes, as does the
101 L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
102 good working examples.
107 use warnings::register;
118 if (defined &{"${pkg}::new"}) {
119 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
123 croak "$pkg doesn't define a TIEHASH method";
129 croak "$pkg doesn't define an EXISTS method";
134 my $key = $self->FIRSTKEY(@_);
137 while (defined $key) {
139 $key = $self->NEXTKEY(@_, $key);
141 foreach $key (@keys) {
142 $self->DELETE(@_, $key);
146 # The Tie::StdHash package implements standard perl hash behaviour.
147 # It exists to act as a base class for classes which only wish to
148 # alter some parts of their behaviour.
150 package Tie::StdHash;
151 @ISA = qw(Tie::Hash);
153 sub TIEHASH { bless {}, $_[0] }
154 sub STORE { $_[0]->{$_[1]} = $_[2] }
155 sub FETCH { $_[0]->{$_[1]} }
156 sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
157 sub NEXTKEY { each %{$_[0]} }
158 sub EXISTS { exists $_[0]->{$_[1]} }
159 sub DELETE { delete $_[0]->{$_[1]} }
160 sub CLEAR { %{$_[0]} = () }