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