Commit | Line | Data |
64d0c973 |
1 | package Tie::Hash; |
cb1a09d0 |
2 | |
b75c8c73 |
3 | our $VERSION = '1.00'; |
4 | |
cb1a09d0 |
5 | =head1 NAME |
6 | |
64d0c973 |
7 | Tie::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 | |
36 | This module provides some skeletal methods for hash-tying classes. See |
64d0c973 |
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. |
cb1a09d0 |
44 | |
45 | For developers wishing to write their own tied hashes, the required methods |
64d0c973 |
46 | are briefly defined below. See the L<perltie> section for more detailed |
47 | descriptive, as well as example code: |
48 | |
bbc7dcd2 |
49 | =over 4 |
cb1a09d0 |
50 | |
51 | =item TIEHASH classname, LIST |
52 | |
64d0c973 |
53 | The method invoked by the command C<tie %hash, classname>. Associates a new |
cb1a09d0 |
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. |
57 | |
58 | =item STORE this, key, value |
59 | |
60 | Store datum I<value> into I<key> for the tied hash I<this>. |
61 | |
62 | =item FETCH this, key |
63 | |
64 | Retrieve the datum in I<key> for the tied hash I<this>. |
65 | |
66 | =item FIRSTKEY this |
67 | |
51c7a601 |
68 | Return the first key in the hash. |
cb1a09d0 |
69 | |
70 | =item NEXTKEY this, lastkey |
71 | |
51c7a601 |
72 | Return the next key in the hash. |
cb1a09d0 |
73 | |
74 | =item EXISTS this, key |
75 | |
76 | Verify that I<key> exists with the tied hash I<this>. |
77 | |
01020589 |
78 | The B<Tie::Hash> implementation is a stub that simply croaks. |
79 | |
cb1a09d0 |
80 | =item DELETE this, key |
81 | |
82 | Delete the key I<key> from the tied hash I<this>. |
83 | |
84 | =item CLEAR this |
85 | |
86 | Clear all values from the tied hash I<this>. |
87 | |
88 | =back |
89 | |
90 | =head1 CAVEATS |
91 | |
64d0c973 |
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. |
cb1a09d0 |
96 | |
97 | =head1 MORE INFORMATION |
98 | |
8dcee03e |
99 | The packages relating to various DBM-related implementations (F<DB_File>, |
cb1a09d0 |
100 | F<NDBM_File>, etc.) show examples of general tied hashes, as does the |
64d0c973 |
101 | L<Config> module. While these do not utilize B<Tie::Hash>, they serve as |
cb1a09d0 |
102 | good working examples. |
103 | |
104 | =cut |
a6006777 |
105 | |
a0d0e21e |
106 | use Carp; |
d3a7d8c7 |
107 | use warnings::register; |
a0d0e21e |
108 | |
109 | sub new { |
4633a7c4 |
110 | my $pkg = shift; |
111 | $pkg->TIEHASH(@_); |
a0d0e21e |
112 | } |
113 | |
114 | # Grandfather "new" |
115 | |
116 | sub 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 | |
127 | sub EXISTS { |
4633a7c4 |
128 | my $pkg = ref $_[0]; |
129 | croak "$pkg doesn't define an EXISTS method"; |
a0d0e21e |
130 | } |
131 | |
132 | sub 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 |
150 | package Tie::StdHash; |
151 | @ISA = qw(Tie::Hash); |
748a9306 |
152 | |
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]} = () } |
161 | |
a0d0e21e |
162 | 1; |