more pod patches
[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
64d0c973 7Tie::Hash, Tie::StdHash - 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
26 sub DELETE { ... }
bbc7dcd2 27
28
cb1a09d0 29 package main;
bbc7dcd2 30
c954a603 31 tie %new_hash, 'NewHash';
32 tie %new_std_hash, 'NewStdHash';
cb1a09d0 33
34=head1 DESCRIPTION
35
36This module provides some skeletal methods for hash-tying classes. See
64d0c973 37L<perltie> for a list of the functions required in order to tie a hash
38to a package. The basic B<Tie::Hash> package provides a C<new> method, as well
39as methods C<TIEHASH>, C<EXISTS> and C<CLEAR>. The B<Tie::StdHash> package
40provides most methods required for hashes in L<perltie>. It inherits from
41B<Tie::Hash>, and causes tied hashes to behave exactly like standard hashes,
42allowing for selective overloading of methods. The C<new> method is provided
43as grandfathering in the case a class forgets to include a C<TIEHASH> method.
cb1a09d0 44
45For developers wishing to write their own tied hashes, the required methods
64d0c973 46are briefly defined below. See the L<perltie> section for more detailed
47descriptive, as well as example code:
48
bbc7dcd2 49=over 4
cb1a09d0 50
51=item TIEHASH classname, LIST
52
64d0c973 53The method invoked by the command C<tie %hash, classname>. Associates a new
cb1a09d0 54hash instance with the specified class. C<LIST> would represent additional
55arguments (along the lines of L<AnyDBM_File> and compatriots) needed to
56complete the association.
57
58=item STORE this, key, value
59
60Store datum I<value> into I<key> for the tied hash I<this>.
61
62=item FETCH this, key
63
64Retrieve the datum in I<key> for the tied hash I<this>.
65
66=item FIRSTKEY this
67
68Return the (key, value) pair for the first key in the hash.
69
70=item NEXTKEY this, lastkey
71
a3cb178b 72Return the next key for the hash.
cb1a09d0 73
74=item EXISTS this, key
75
76Verify that I<key> exists with the tied hash I<this>.
77
01020589 78The B<Tie::Hash> implementation is a stub that simply croaks.
79
cb1a09d0 80=item DELETE this, key
81
82Delete the key I<key> from the tied hash I<this>.
83
84=item CLEAR this
85
86Clear all values from the tied hash I<this>.
87
88=back
89
90=head1 CAVEATS
91
64d0c973 92The L<perltie> documentation includes a method called C<DESTROY> as
93a necessary method for tied hashes. Neither B<Tie::Hash> nor B<Tie::StdHash>
94define a default for this method. This is a standard for class packages,
95but may be omitted in favor of a simple default.
cb1a09d0 96
97=head1 MORE INFORMATION
98
8dcee03e 99The packages relating to various DBM-related implementations (F<DB_File>,
cb1a09d0 100F<NDBM_File>, etc.) show examples of general tied hashes, as does the
64d0c973 101L<Config> module. While these do not utilize B<Tie::Hash>, they serve as
cb1a09d0 102good working examples.
103
104=cut
a6006777 105
a0d0e21e 106use Carp;
d3a7d8c7 107use warnings::register;
a0d0e21e 108
109sub new {
4633a7c4 110 my $pkg = shift;
111 $pkg->TIEHASH(@_);
a0d0e21e 112}
113
114# Grandfather "new"
115
116sub TIEHASH {
4633a7c4 117 my $pkg = shift;
cc6b7395 118 if (defined &{"${pkg}::new"}) {
7e6d00f8 119 warnings::warnif("WARNING: calling ${pkg}->new since ${pkg}->TIEHASH is missing");
4633a7c4 120 $pkg->new(@_);
a0d0e21e 121 }
122 else {
4633a7c4 123 croak "$pkg doesn't define a TIEHASH method";
a0d0e21e 124 }
125}
126
127sub EXISTS {
4633a7c4 128 my $pkg = ref $_[0];
129 croak "$pkg doesn't define an EXISTS method";
a0d0e21e 130}
131
132sub CLEAR {
133 my $self = shift;
134 my $key = $self->FIRSTKEY(@_);
135 my @keys;
136
137 while (defined $key) {
138 push @keys, $key;
139 $key = $self->NEXTKEY(@_, $key);
140 }
141 foreach $key (@keys) {
142 $self->DELETE(@_, $key);
143 }
144}
145
64d0c973 146# The Tie::StdHash package implements standard perl hash behaviour.
748a9306 147# It exists to act as a base class for classes which only wish to
148# alter some parts of their behaviour.
149
64d0c973 150package Tie::StdHash;
151@ISA = qw(Tie::Hash);
748a9306 152
153sub TIEHASH { bless {}, $_[0] }
154sub STORE { $_[0]->{$_[1]} = $_[2] }
155sub FETCH { $_[0]->{$_[1]} }
156sub FIRSTKEY { my $a = scalar keys %{$_[0]}; each %{$_[0]} }
157sub NEXTKEY { each %{$_[0]} }
158sub EXISTS { exists $_[0]->{$_[1]} }
159sub DELETE { delete $_[0]->{$_[1]} }
160sub CLEAR { %{$_[0]} = () }
161
a0d0e21e 1621;