Make sv_force_normal_flags cope with shared hash key scalars
[p5sagit/p5-mst-13.2.git] / lib / Tie / Hash.pm
CommitLineData
64d0c973 1package Tie::Hash;
cb1a09d0 2
b75c8c73 3our $VERSION = '1.00';
4
cb1a09d0 5=head1 NAME
6
d5582e24 7Tie::Hash, Tie::StdHash, Tie::ExtraHash - base class definitions for tied hashes
cb1a09d0 8
9=head1 SYNOPSIS
10
11 package NewHash;
64d0c973 12 require Tie::Hash;
bbc7dcd2 13
64d0c973 14 @ISA = (Tie::Hash);
bbc7dcd2 15
cb1a09d0 16 sub DELETE { ... } # Provides needed method
17 sub CLEAR { ... } # Overrides inherited method
bbc7dcd2 18
19
cb1a09d0 20 package NewStdHash;
64d0c973 21 require Tie::Hash;
bbc7dcd2 22
64d0c973 23 @ISA = (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 30 package NewExtraHash;
31 require Tie::Hash;
32
33 @ISA = (Tie::ExtraHash);
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 38 # the reference to the actual storage
39 sub DELETE {
40 $_[0][1]->('del', $_[0][0], $_[1]); # Call the report writer
96820f7c 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 49 tie %new_extra_hash, 'NewExtraHash',
50 sub {warn "Doing \U$_[1]\E of $_[2].\n"};
cb1a09d0 51
52=head1 DESCRIPTION
53
54This module provides some skeletal methods for hash-tying classes. See
64d0c973 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 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 64
65For developers wishing to write their own tied hashes, the required methods
64d0c973 66are briefly defined below. See the L<perltie> section for more detailed
67descriptive, as well as example code:
68
bbc7dcd2 69=over 4
cb1a09d0 70
71=item TIEHASH classname, LIST
72
64d0c973 73The method invoked by the command C<tie %hash, classname>. Associates a new
cb1a09d0 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 89
90=item NEXTKEY this, lastkey
91
51c7a601 92Return the next key in the hash.
cb1a09d0 93
94=item EXISTS this, key
95
96Verify that I<key> exists with the tied hash I<this>.
97
01020589 98The B<Tie::Hash> implementation is a stub that simply croaks.
99
cb1a09d0 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
108=back
109
d5582e24 110=head1 Inheriting from B<Tie::StdHash>
111
112The accessor methods assume that the actual storage for the data in the tied
113hash is in the hash referenced by C<tied(%tiedhash)>. Thus overwritten
15634f32 114C<TIEHASH> method should return a hash reference, and the remaining methods
d5582e24 115should operate on the hash referenced by the first argument:
116
117 package ReportHash;
118 our @ISA = 'Tie::StdHash';
119
120 sub TIEHASH {
121 my $storage = bless {}, shift;
122 warn "New ReportHash created, stored in $storage.\n";
123 $storage
124 }
125 sub STORE {
126 warn "Storing data with key $_[1] at $_[0].\n";
127 $_[0]{$_[1]} = $_[2]
128 }
129
cb1a09d0 130
d5582e24 131=head1 Inheriting from B<Tie::ExtraHash>
132
133The accessor methods assume that the actual storage for the data in the tied
134hash is in the hash referenced by C<(tied(%tiedhash))[0]>. Thus overwritten
15634f32 135C<TIEHASH> method should return an array reference with the first
d5582e24 136element being a hash reference, and the remaining methods should operate on the
194eaab5 137hash C<< %{ $_[0]->[0] } >>:
d5582e24 138
139 package ReportHash;
1db7d662 140 our @ISA = 'Tie::ExtraHash';
d5582e24 141
142 sub TIEHASH {
1db7d662 143 my $class = shift;
144 my $storage = bless [{}, @_], $class;
d5582e24 145 warn "New ReportHash created, stored in $storage.\n";
1db7d662 146 $storage;
d5582e24 147 }
148 sub STORE {
149 warn "Storing data with key $_[1] at $_[0].\n";
150 $_[0][0]{$_[1]} = $_[2]
151 }
152
15634f32 153The default C<TIEHASH> method stores "extra" arguments to tie() starting
d5582e24 154from offset 1 in the array referenced by C<tied(%tiedhash)>; this is the
155same storage algorithm as in TIEHASH subroutine above. Hence, a typical
156package inheriting from B<Tie::ExtraHash> does not need to overwrite this
157method.
158
159=head1 C<UNTIE> and C<DESTROY>
160
161The methods C<UNTIE> and C<DESTROY> are not defined in B<Tie::Hash>,
162B<Tie::StdHash>, or B<Tie::ExtraHash>. Tied hashes do not require
163presense of these methods, but if defined, the methods will be called in
164proper time, see L<perltie>.
165
166If needed, these methods should be defined by the package inheriting from
167B<Tie::Hash>, B<Tie::StdHash>, or B<Tie::ExtraHash>.
cb1a09d0 168
169=head1 MORE INFORMATION
170
8dcee03e 171The packages relating to various DBM-related implementations (F<DB_File>,
cb1a09d0 172F<NDBM_File>, etc.) show examples of general tied hashes, as does the
64d0c973 173L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
cb1a09d0 174good working examples.
175
176=cut
a6006777 177
a0d0e21e 178use Carp;
d3a7d8c7 179use warnings::register;
a0d0e21e 180
181sub new {
4633a7c4 182 my $pkg = shift;
183 $pkg->TIEHASH(@_);
a0d0e21e 184}
185
186# Grandfather "new"
187
188sub TIEHASH {
4633a7c4 189 my $pkg = shift;
cc6b7395 190 if (defined &{"${pkg}::new"}) {
7e6d00f8 191 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
4633a7c4 192 $pkg->new(@_);
a0d0e21e 193 }
194 else {
4633a7c4 195 croak "$pkg doesn't define a TIEHASH method";
a0d0e21e 196 }
197}
198
199sub EXISTS {
4633a7c4 200 my $pkg = ref $_[0];
201 croak "$pkg doesn't define an EXISTS method";
a0d0e21e 202}
203
204sub CLEAR {
205 my $self = shift;
206 my $key = $self->FIRSTKEY(@_);
207 my @keys;
208
209 while (defined $key) {
210 push @keys, $key;
211 $key = $self->NEXTKEY(@_, $key);
212 }
213 foreach $key (@keys) {
214 $self->DELETE(@_, $key);
215 }
216}
217
64d0c973 218# The Tie::StdHash package implements standard perl hash behaviour.
748a9306 219# It exists to act as a base class for classes which only wish to
220# alter some parts of their behaviour.
221
64d0c973 222package Tie::StdHash;
d5582e24 223# @ISA = qw(Tie::Hash); # would inherit new() only
748a9306 224
225sub TIEHASH { bless {}, $_[0] }
226sub STORE { $_[0]->{$_[1]} = $_[2] }
227sub FETCH { $_[0]->{$_[1]} }
228sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
229sub NEXTKEY { each %{$_[0]} }
230sub EXISTS { exists $_[0]->{$_[1]} }
231sub DELETE { delete $_[0]->{$_[1]} }
232sub CLEAR { %{$_[0]} = () }
233
d5582e24 234package Tie::ExtraHash;
235
236sub TIEHASH { my $p = shift; bless [{}, @_], $p }
237sub STORE { $_[0][0]{$_[1]} = $_[2] }
238sub FETCH { $_[0][0]{$_[1]} }
239sub FIRSTKEY { my $a = scalar keys %{$_[0][0]}; each %{$_[0][0]} }
240sub NEXTKEY { each %{$_[0][0]} }
241sub EXISTS { exists $_[0][0]->{$_[1]} }
242sub DELETE { delete $_[0][0]->{$_[1]} }
243sub CLEAR { %{$_[0][0]} = () }
244
a0d0e21e 2451;