This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Math::BigRat 0.22
[perl5.git] / lib / Tie / Hash.pm
CommitLineData
64d0c973 1package Tie::Hash;
cb1a09d0 2
98225a64 3our $VERSION = '1.02';
b75c8c73 4
cb1a09d0
AD
5=head1 NAME
6
d5582e24 7Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes
cb1a09d0
AD
8
9=head1 SYNOPSIS
10
11 package NewHash;
64d0c973 12 require Tie::Hash;
bbc7dcd2 13
abc0156b 14 @ISA = qw(Tie::Hash);
bbc7dcd2 15
cb1a09d0
AD
16 sub DELETE { ... } # Provides needed method
17 sub CLEAR { ... } # Overrides inherited method
bbc7dcd2
MS
18
19
cb1a09d0 20 package NewStdHash;
64d0c973 21 require Tie::Hash;
bbc7dcd2 22
abc0156b 23 @ISA = qw(Tie::StdHash);
bbc7dcd2 24
cb1a09d0 25 # All methods provided by default, define only those needing overrides
d5582e24 26 # Accessors access the storage in %{$_[0]};
15634f32 27 # TIEHASH should return a reference to the actual storage
cb1a09d0 28 sub DELETE { ... }
bbc7dcd2 29
d5582e24
IZ
30 package NewExtraHash;
31 require Tie::Hash;
32
abc0156b 33 @ISA = qw(Tie::ExtraHash);
d5582e24
IZ
34
35 # All methods provided by default, define only those needing overrides
36 # Accessors access the storage in %{$_[0][0]};
15634f32 37 # TIEHASH should return an array reference with the first element being
d5582e24
IZ
38 # the reference to the actual storage
39 sub DELETE {
40 $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
96820f7c
SR
41 delete $_[0][0]->{$_[1]}; # $_[0]->SUPER::DELETE($_[1])
42 }
d5582e24 43
bbc7dcd2 44
cb1a09d0 45 package main;
bbc7dcd2 46
c954a603 47 tie %new_hash, 'NewHash';
48 tie %new_std_hash, 'NewStdHash';
d5582e24
IZ
49 tie %new_extra_hash, 'NewExtraHash',
50 sub {warn "Doing \U$_[1]\E of $_[2].\n"};
cb1a09d0
AD
51
52=head1 DESCRIPTION
53
54This module provides some skeletal methods for hash-tying classes. See
64d0c973
RR
55L<perltie> for a list of the functions required in order to tie a hash
56to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
d5582e24
IZ
57as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> and
58B<Tie::ExtraHash> packages
59provide most methods for hashes described in L<perltie> (the exceptions
60are C<UNTIE> and C<DESTROY>). They cause tied hashes to behave exactly like standard hashes,
61and allow for selective overwriting of methods. B<Tie::Hash> grandfathers the
62C<new> method: it is used if C<TIEHASH> is not defined
63in the case a class forgets to include a C<TIEHASH> method.
cb1a09d0
AD
64
65For developers wishing to write their own tied hashes, the required methods
64d0c973
RR
66are briefly defined below. See the L<perltie> section for more detailed
67descriptive, as well as example code:
68
bbc7dcd2 69=over 4
cb1a09d0
AD
70
71=item TIEHASH classname, LIST
72
64d0c973 73The method invoked by the command C<tie %hash, classname>. Associates a new
cb1a09d0
AD
74hash instance with the specified class. C<LIST> would represent additional
75arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
76complete the association.
77
78=item STORE this, key, value
79
80Store datum I<value> into I<key> for the tied hash I<this>.
81
82=item FETCH this, key
83
84Retrieve the datum in I<key> for the tied hash I<this>.
85
86=item FIRSTKEY this
87
51c7a601 88Return the first key in the hash.
cb1a09d0
AD
89
90=item NEXTKEY this, lastkey
91
51c7a601 92Return the next key in the hash.
cb1a09d0
AD
93
94=item EXISTS this, key
95
96Verify that I<key> exists with the tied hash I<this>.
97
01020589
GS
98The B<Tie::Hash> implementation is a stub that simply croaks.
99
cb1a09d0
AD
100=item DELETE this, key
101
102Delete the key I<key> from the tied hash I<this>.
103
104=item CLEAR this
105
106Clear all values from the tied hash I<this>.
107
a3bcc51e
TP
108=item SCALAR this
109
110Returns what evaluating the hash in scalar context yields.
111
112B<Tie::Hash> does not implement this method (but B<Tie::StdHash>
113and B<Tie::ExtraHash> do).
114
cb1a09d0
AD
115=back
116
d5582e24
IZ
117=head1 Inheriting from B<Tie::StdHash>
118
119The accessor methods assume that the actual storage for the data in the tied
120hash is in the hash referenced by C<tied(%tiedhash)>. Thus overwritten
15634f32 121C<TIEHASH> method should return a hash reference, and the remaining methods
d5582e24
IZ
122should operate on the hash referenced by the first argument:
123
124 package ReportHash;
125 our @ISA = 'Tie::StdHash';
126
127 sub TIEHASH {
128 my $storage = bless {}, shift;
129 warn "New ReportHash created, stored in $storage.\n";
130 $storage
131 }
132 sub STORE {
133 warn "Storing data with key $_[1] at $_[0].\n";
134 $_[0]{$_[1]} = $_[2]
135 }
136
cb1a09d0 137
d5582e24
IZ
138=head1 Inheriting from B<Tie::ExtraHash>
139
140The accessor methods assume that the actual storage for the data in the tied
a3bcc51e 141hash is in the hash referenced by C<(tied(%tiedhash))-E<gt>[0]>. Thus overwritten
15634f32 142C<TIEHASH> method should return an array reference with the first
d5582e24 143element being a hash reference, and the remaining methods should operate on the
194eaab5 144hash C<< %{ $_[0]->[0] } >>:
d5582e24
IZ
145
146 package ReportHash;
1db7d662 147 our @ISA = 'Tie::ExtraHash';
d5582e24
IZ
148
149 sub TIEHASH {
1db7d662
BT
150 my $class = shift;
151 my $storage = bless [{}, @_], $class;
d5582e24 152 warn "New ReportHash created, stored in $storage.\n";
1db7d662 153 $storage;
d5582e24
IZ
154 }
155 sub STORE {
156 warn "Storing data with key $_[1] at $_[0].\n";
157 $_[0][0]{$_[1]} = $_[2]
158 }
159
15634f32 160The default C<TIEHASH> method stores "extra" arguments to tie() starting
d5582e24
IZ
161from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
162same storage algorithm as in TIEHASH subroutine above. Hence, a typical
163package inheriting from B<Tie::ExtraHash> does not need to overwrite this
164method.
165
a3bcc51e 166=head1 C<SCALAR>, C<UNTIE> and C<DESTROY>
d5582e24
IZ
167
168The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
169B<Tie::StdHash>, or B<Tie::ExtraHash>. Tied hashes do not require
3c4b39be 170presence of these methods, but if defined, the methods will be called in
d5582e24
IZ
171proper time, see L<perltie>.
172
a3bcc51e
TP
173C<SCALAR> is only defined in B<Tie::StdHash> and B<Tie::ExtraHash>.
174
d5582e24 175If needed, these methods should be defined by the package inheriting from
a3bcc51e
TP
176B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>. See L<pertie/"SCALAR">
177to find out what happens when C<SCALAR> does not exist.
cb1a09d0
AD
178
179=head1 MORE INFORMATION
180
8dcee03e 181The packages relating to various DBM-related implementations (F<DB_File>,
cb1a09d0 182F<NDBM_File>, etc.) show examples of general tied hashes, as does the
64d0c973 183L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
cb1a09d0
AD
184good working examples.
185
186=cut
a6006777 187
a0d0e21e 188use Carp;
d3a7d8c7 189use warnings::register;
a0d0e21e
LW
190
191sub new {
4633a7c4
LW
192 my $pkg = shift;
193 $pkg->TIEHASH(@_);
a0d0e21e
LW
194}
195
196# Grandfather "new"
197
198sub TIEHASH {
4633a7c4 199 my $pkg = shift;
cc6b7395 200 if (defined &{"${pkg}::new"}) {
7e6d00f8 201 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
4633a7c4 202 $pkg->new(@_);
a0d0e21e
LW
203 }
204 else {
4633a7c4 205 croak "$pkg doesn't define a TIEHASH method";
a0d0e21e
LW
206 }
207}
208
209sub EXISTS {
4633a7c4
LW
210 my $pkg = ref $_[0];
211 croak "$pkg doesn't define an EXISTS method";
a0d0e21e
LW
212}
213
214sub CLEAR {
215 my $self = shift;
216 my $key = $self->FIRSTKEY(@_);
217 my @keys;
218
219 while (defined $key) {
220 push @keys, $key;
221 $key = $self->NEXTKEY(@_, $key);
222 }
223 foreach $key (@keys) {
224 $self->DELETE(@_, $key);
225 }
226}
227
64d0c973 228# The Tie::StdHash package implements standard perl hash behaviour.
748a9306
LW
229# It exists to act as a base class for classes which only wish to
230# alter some parts of their behaviour.
231
64d0c973 232package Tie::StdHash;
d5582e24 233# @ISA = qw(Tie::Hash); # would inherit new() only
748a9306
LW
234
235sub TIEHASH { bless {}, $_[0] }
236sub STORE { $_[0]->{$_[1]} = $_[2] }
237sub FETCH { $_[0]->{$_[1]} }
238sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
239sub NEXTKEY { each %{$_[0]} }
240sub EXISTS { exists $_[0]->{$_[1]} }
241sub DELETE { delete $_[0]->{$_[1]} }
242sub CLEAR { %{$_[0]} = () }
a3bcc51e 243sub SCALAR { scalar %{$_[0]} }
748a9306 244
d5582e24
IZ
245package Tie::ExtraHash;
246
247sub TIEHASH { my $p = shift; bless [{}, @_], $p }
248sub STORE { $_[0][0]{$_[1]} = $_[2] }
249sub FETCH { $_[0][0]{$_[1]} }
250sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
251sub NEXTKEY { each %{$_[0][0]} }
252sub EXISTS { exists $_[0][0]->{$_[1]} }
253sub DELETE { delete $_[0][0]->{$_[1]} }
254sub CLEAR { %{$_[0][0]} = () }
a3bcc51e 255sub SCALAR { scalar %{$_[0][0]} }
d5582e24 256
a0d0e21e 2571;