1DBM_Filter(3pm) Perl Programmers Reference Guide DBM_Filter(3pm)
2
3
4
6 DBM_Filter -- Filter DBM keys/values
7
9 use DBM_Filter ;
10 use SDBM_File; # or DB_File, or GDBM_File, or NDBM_File, or ODBM_File
11
12 $db = tie %hash, ...
13
14 $db->Filter_Push(Fetch => sub {...},
15 Store => sub {...});
16
17 $db->Filter_Push('my_filter1');
18 $db->Filter_Push('my_filter2', params...);
19
20 $db->Filter_Key_Push(...) ;
21 $db->Filter_Value_Push(...) ;
22
23 $db->Filter_Pop();
24 $db->Filtered();
25
26 package DBM_Filter::my_filter1;
27
28 sub Store { ... }
29 sub Fetch { ... }
30
31 1;
32
33 package DBM_Filter::my_filter2;
34
35 sub Filter
36 {
37 my @opts = @_;
38 ...
39 return (
40 sub Store { ... },
41 sub Fetch { ... } );
42 }
43
44 1;
45
47 This module provides an interface that allows filters to be applied to
48 tied Hashes associated with DBM files. It builds on the DBM Filter
49 hooks that are present in all the *DB*_File modules included with the
50 standard Perl source distribution from version 5.6.1 onwards. In addi‐
51 tion to the *DB*_File modules distributed with Perl, the BerkeleyDB
52 module, available on CPAN, supports the DBM Filter hooks. See perldbm‐
53 filter for more details on the DBM Filter hooks.
54
56 A DBM Filter allows the keys and/or values in a tied hash to be modi‐
57 fied by some user-defined code just before it is written to the DBM
58 file and just after it is read back from the DBM file. For example,
59 this snippet of code
60
61 $some_hash{"abc"} = 42;
62
63 could potentially trigger two filters, one for the writing of the key
64 "abc" and another for writing the value 42. Similarly, this snippet
65
66 my ($key, $value) = each %some_hash
67
68 will trigger two filters, one for the reading of the key and one for
69 the reading of the value.
70
71 Like the existing DBM Filter functionality, this module arranges for
72 the $_ variable to be populated with the key or value that a filter
73 will check. This usually means that most DBM filters tend to be very
74 short.
75
76 So what's new?
77
78 The main enhancements over the standard DBM Filter hooks are:
79
80 · A cleaner interface.
81
82 · The ability to easily apply multiple filters to a single DBM file.
83
84 · The ability to create "canned" filters. These allow commonly used
85 filters to be packaged into a stand-alone module.
86
88 This module will arrange for the following methods to be available via
89 the object returned from the "tie" call.
90
91 $db->Filter_Push()
92
93 $db->Filter_Key_Push()
94
95 $db->Filter_Value_Push()
96
97 Add a filter to filter stack for the database, $db. The three formats
98 vary only in whether they apply to the DBM key, the DBM value or both.
99
100 Filter_Push
101 The filter is applied to both keys and values.
102
103 Filter_Key_Push
104 The filter is applied to the key only.
105
106 Filter_Value_Push
107 The filter is applied to the value only.
108
109 $db->Filter_Pop()
110
111 Removes the last filter that was applied to the DBM file associated
112 with $db, if present.
113
114 $db->Filtered()
115
116 Returns TRUE if there are any filters applied to the DBM associated
117 with $db. Otherwise returns FALSE.
118
120 Filters can be created in two main ways
121
122 Immediate Filters
123
124 An immediate filter allows you to specify the filter code to be used at
125 the point where the filter is applied to a dbm. In this mode the Fil‐
126 ter_*_Push methods expects to receive exactly two parameters.
127
128 my $db = tie %hash, 'SDBM_File', ...
129 $db->Filter_Push( Store => sub { },
130 Fetch => sub { });
131
132 The code reference associated with "Store" will be called before any
133 key/value is written to the database and the code reference associated
134 with "Fetch" will be called after any key/value is read from the data‐
135 base.
136
137 For example, here is a sample filter that adds a trailing NULL charac‐
138 ter to all strings before they are written to the DBM file, and removes
139 the trailing NULL when they are read from the DBM file
140
141 my $db = tie %hash, 'SDBM_File', ...
142 $db->Filter_Push( Store => sub { $_ .= "\x00" ; },
143 Fetch => sub { s/\x00$// ; });
144
145 Points to note:
146
147 1. Both the Store and Fetch filters manipulate $_.
148
149 Canned Filters
150
151 Immediate filters are useful for one-off situations. For more generic
152 problems it can be useful to package the filter up in its own module.
153
154 The usage is for a canned filter is:
155
156 $db->Filter_Push("name", params)
157
158 where
159
160 "name"
161 is the name of the module to load. If the string specified does
162 not contain the package separator characters "::", it is assumed
163 to refer to the full module name "DBM_Filter::name". This means
164 that the full names for canned filters, "null" and "utf8",
165 included with this module are:
166
167 DBM_Filter::null
168 DBM_Filter::utf8
169
170 params
171 any optional parameters that need to be sent to the filter. See
172 the encode filter for an example of a module that uses parameters.
173
174 The module that implements the canned filter can take one of two forms.
175 Here is a template for the first
176
177 package DBM_Filter::null ;
178
179 use strict;
180 use warnings;
181
182 sub Store
183 {
184 # store code here
185 }
186
187 sub Fetch
188 {
189 # fetch code here
190 }
191
192 1;
193
194 Notes:
195
196 1. The package name uses the "DBM_Filter::" prefix.
197
198 2. The module must have both a Store and a Fetch method. If only one
199 is present, or neither are present, a fatal error will be thrown.
200
201 The second form allows the filter to hold state information using a
202 closure, thus:
203
204 package DBM_Filter::encoding ;
205
206 use strict;
207 use warnings;
208
209 sub Filter
210 {
211 my @params = @_ ;
212
213 ...
214 return {
215 Store => sub { $_ = $encoding->encode($_) },
216 Fetch => sub { $_ = $encoding->decode($_) }
217 } ;
218 }
219
220 1;
221
222 In this instance the "Store" and "Fetch" methods are encapsulated
223 inside a "Filter" method.
224
226 A number of canned filers are provided with this module. They cover a
227 number of the main areas that filters are needed when interfacing with
228 DBM files. They also act as templates for your own filters.
229
230 The filter included are:
231
232 * utf8
233 This module will ensure that all data written to the DBM will be
234 encoded in UTF-8.
235
236 This module needs the Encode module.
237
238 * encode
239 Allows you to choose the character encoding will be store in the
240 DBM file.
241
242 * compress
243 This filter will compress all data before it is written to the
244 database and uncompressed it on reading.
245
246 This module needs Compress::Zlib.
247
248 * int32
249 This module is used when interoperating with a C/C++ application
250 that uses a C int as either the key and/or value in the DBM file.
251
252 * null
253 This module ensures that all data written to the DBM file is null
254 terminated. This is useful when you have a perl script that needs
255 to interoperate with a DBM file that a C program also uses. A
256 fairly common issue is for the C application to include the termi‐
257 nating null in a string when it writes to the DBM file. This fil‐
258 ter will ensure that all data written to the DBM file can be read
259 by the C application.
260
262 Maintain Round Trip Integrity
263
264 When writing a DBM filter it is very important to ensure that it is
265 possible to retrieve all data that you have written when the DBM filter
266 is in place. In practice, this means that whatever transformation is
267 applied to the data in the Store method, the exact inverse operation
268 should be applied in the Fetch method.
269
270 If you don't provide an exact inverse transformation, you will find
271 that code like this will not behave as you expect.
272
273 while (my ($k, $v) = each %hash)
274 {
275 ...
276 }
277
278 Depending on the transformation, you will find that one or more of the
279 following will happen
280
281 1 The loop will never terminate.
282
283 2 Too few records will be retrieved.
284
285 3 Too many will be retrieved.
286
287 4 The loop will do the right thing for a while, but it will unex‐
288 pectedly fail.
289
290 Don't mix filtered & non-filtered data in the same database file.
291
292 This is just a restatement of the previous section. Unless you are com‐
293 pletely certain you know what you are doing, avoid mixing filtered &
294 non-filtered data.
295
297 Say you need to interoperate with a legacy C application that stores
298 keys as C ints and the values and null terminated UTF-8 strings. Here
299 is how you would set that up
300
301 my $db = tie %hash, 'SDBM_File', ...
302
303 $db->Filter_Key_Push('int32') ;
304
305 $db->Filter_Value_Push('utf8');
306 $db->Filter_Value_Push('null');
307
309 <DB_File>, GDBM_File, NDBM_File, ODBM_File, SDBM_File, perldbmfilter
310
312 Paul Marquess <pmqs@cpan.org>
313
314
315
316perl v5.8.8 2001-09-21 DBM_Filter(3pm)