Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "DBM_Filter 3" | |
132 | .TH DBM_Filter 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | DBM_Filter \-\- Filter DBM keys/values | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 2 | |
138 | \& use DBM_Filter ; | |
139 | \& use SDBM_File; # or DB_File, or GDBM_File, or NDBM_File, or ODBM_File | |
140 | .Ve | |
141 | .PP | |
142 | .Vb 1 | |
143 | \& $db = tie %hash, ... | |
144 | .Ve | |
145 | .PP | |
146 | .Vb 2 | |
147 | \& $db->Filter_Push(Fetch => sub {...}, | |
148 | \& Store => sub {...}); | |
149 | .Ve | |
150 | .PP | |
151 | .Vb 2 | |
152 | \& $db->Filter_Push('my_filter1'); | |
153 | \& $db->Filter_Push('my_filter2', params...); | |
154 | .Ve | |
155 | .PP | |
156 | .Vb 2 | |
157 | \& $db->Filter_Key_Push(...) ; | |
158 | \& $db->Filter_Value_Push(...) ; | |
159 | .Ve | |
160 | .PP | |
161 | .Vb 2 | |
162 | \& $db->Filter_Pop(); | |
163 | \& $db->Filtered(); | |
164 | .Ve | |
165 | .PP | |
166 | .Vb 1 | |
167 | \& package DBM_Filter::my_filter1; | |
168 | .Ve | |
169 | .PP | |
170 | .Vb 2 | |
171 | \& sub Store { ... } | |
172 | \& sub Fetch { ... } | |
173 | .Ve | |
174 | .PP | |
175 | .Vb 1 | |
176 | \& 1; | |
177 | .Ve | |
178 | .PP | |
179 | .Vb 1 | |
180 | \& package DBM_Filter::my_filter2; | |
181 | .Ve | |
182 | .PP | |
183 | .Vb 8 | |
184 | \& sub Filter | |
185 | \& { | |
186 | \& my @opts = @_; | |
187 | \& ... | |
188 | \& return ( | |
189 | \& sub Store { ... }, | |
190 | \& sub Fetch { ... } ); | |
191 | \& } | |
192 | .Ve | |
193 | .PP | |
194 | .Vb 1 | |
195 | \& 1; | |
196 | .Ve | |
197 | .SH "DESCRIPTION" | |
198 | .IX Header "DESCRIPTION" | |
199 | This module provides an interface that allows filters to be applied | |
200 | to tied Hashes associated with \s-1DBM\s0 files. It builds on the \s-1DBM\s0 Filter | |
201 | hooks that are present in all the *DB*_File modules included with the | |
202 | standard Perl source distribution from version 5.6.1 onwards. In addition | |
203 | to the *DB*_File modules distributed with Perl, the BerkeleyDB module, | |
204 | available on \s-1CPAN\s0, supports the \s-1DBM\s0 Filter hooks. See perldbmfilter | |
205 | for more details on the \s-1DBM\s0 Filter hooks. | |
206 | .SH "What is a DBM Filter?" | |
207 | .IX Header "What is a DBM Filter?" | |
208 | A \s-1DBM\s0 Filter allows the keys and/or values in a tied hash to be modified | |
209 | by some user-defined code just before it is written to the \s-1DBM\s0 file and | |
210 | just after it is read back from the \s-1DBM\s0 file. For example, this snippet | |
211 | of code | |
212 | .PP | |
213 | .Vb 1 | |
214 | \& $some_hash{"abc"} = 42; | |
215 | .Ve | |
216 | .PP | |
217 | could potentially trigger two filters, one for the writing of the key | |
218 | \&\*(L"abc\*(R" and another for writing the value 42. Similarly, this snippet | |
219 | .PP | |
220 | .Vb 1 | |
221 | \& my ($key, $value) = each %some_hash | |
222 | .Ve | |
223 | .PP | |
224 | will trigger two filters, one for the reading of the key and one for | |
225 | the reading of the value. | |
226 | .PP | |
227 | Like the existing \s-1DBM\s0 Filter functionality, this module arranges for the | |
228 | \&\f(CW$_\fR variable to be populated with the key or value that a filter will | |
229 | check. This usually means that most \s-1DBM\s0 filters tend to be very short. | |
230 | .Sh "So what's new?" | |
231 | .IX Subsection "So what's new?" | |
232 | The main enhancements over the standard \s-1DBM\s0 Filter hooks are: | |
233 | .IP "\(bu" 4 | |
234 | A cleaner interface. | |
235 | .IP "\(bu" 4 | |
236 | The ability to easily apply multiple filters to a single \s-1DBM\s0 file. | |
237 | .IP "\(bu" 4 | |
238 | The ability to create \*(L"canned\*(R" filters. These allow commonly used filters | |
239 | to be packaged into a stand-alone module. | |
240 | .SH "METHODS" | |
241 | .IX Header "METHODS" | |
242 | This module will arrange for the following methods to be available via | |
243 | the object returned from the \f(CW\*(C`tie\*(C'\fR call. | |
244 | .Sh "$db\->\fIFilter_Push()\fP" | |
245 | .IX Subsection "$db->Filter_Push()" | |
246 | .Sh "$db\->\fIFilter_Key_Push()\fP" | |
247 | .IX Subsection "$db->Filter_Key_Push()" | |
248 | .Sh "$db\->\fIFilter_Value_Push()\fP" | |
249 | .IX Subsection "$db->Filter_Value_Push()" | |
250 | Add a filter to filter stack for the database, \f(CW$db\fR. The three formats | |
251 | vary only in whether they apply to the \s-1DBM\s0 key, the \s-1DBM\s0 value or both. | |
252 | .IP "Filter_Push" 5 | |
253 | .IX Item "Filter_Push" | |
254 | The filter is applied to \fIboth\fR keys and values. | |
255 | .IP "Filter_Key_Push" 5 | |
256 | .IX Item "Filter_Key_Push" | |
257 | The filter is applied to the key \fIonly\fR. | |
258 | .IP "Filter_Value_Push" 5 | |
259 | .IX Item "Filter_Value_Push" | |
260 | The filter is applied to the value \fIonly\fR. | |
261 | .Sh "$db\->\fIFilter_Pop()\fP" | |
262 | .IX Subsection "$db->Filter_Pop()" | |
263 | Removes the last filter that was applied to the \s-1DBM\s0 file associated with | |
264 | \&\f(CW$db\fR, if present. | |
265 | .Sh "$db\->\fIFiltered()\fP" | |
266 | .IX Subsection "$db->Filtered()" | |
267 | Returns \s-1TRUE\s0 if there are any filters applied to the \s-1DBM\s0 associated | |
268 | with \f(CW$db\fR. Otherwise returns \s-1FALSE\s0. | |
269 | .SH "Writing a Filter" | |
270 | .IX Header "Writing a Filter" | |
271 | Filters can be created in two main ways | |
272 | .Sh "Immediate Filters" | |
273 | .IX Subsection "Immediate Filters" | |
274 | An immediate filter allows you to specify the filter code to be used | |
275 | at the point where the filter is applied to a dbm. In this mode the | |
276 | Filter_*_Push methods expects to receive exactly two parameters. | |
277 | .PP | |
278 | .Vb 3 | |
279 | \& my $db = tie %hash, 'SDBM_File', ... | |
280 | \& $db->Filter_Push( Store => sub { }, | |
281 | \& Fetch => sub { }); | |
282 | .Ve | |
283 | .PP | |
284 | The code reference associated with \f(CW\*(C`Store\*(C'\fR will be called before any | |
285 | key/value is written to the database and the code reference associated | |
286 | with \f(CW\*(C`Fetch\*(C'\fR will be called after any key/value is read from the | |
287 | database. | |
288 | .PP | |
289 | For example, here is a sample filter that adds a trailing \s-1NULL\s0 character | |
290 | to all strings before they are written to the \s-1DBM\s0 file, and removes the | |
291 | trailing \s-1NULL\s0 when they are read from the \s-1DBM\s0 file | |
292 | .PP | |
293 | .Vb 3 | |
294 | \& my $db = tie %hash, 'SDBM_File', ... | |
295 | \& $db->Filter_Push( Store => sub { $_ .= "\ex00" ; }, | |
296 | \& Fetch => sub { s/\ex00$// ; }); | |
297 | .Ve | |
298 | .PP | |
299 | Points to note: | |
300 | .IP "1." 5 | |
301 | Both the Store and Fetch filters manipulate \f(CW$_\fR. | |
302 | .Sh "Canned Filters" | |
303 | .IX Subsection "Canned Filters" | |
304 | Immediate filters are useful for one-off situations. For more generic | |
305 | problems it can be useful to package the filter up in its own module. | |
306 | .PP | |
307 | The usage is for a canned filter is: | |
308 | .PP | |
309 | .Vb 1 | |
310 | \& $db->Filter_Push("name", params) | |
311 | .Ve | |
312 | .PP | |
313 | where | |
314 | .ie n .IP """name""" 5 | |
315 | .el .IP "``name''" 5 | |
316 | .IX Item "name" | |
317 | is the name of the module to load. If the string specified does not | |
318 | contain the package separator characters \*(L"::\*(R", it is assumed to refer to | |
319 | the full module name \*(L"DBM_Filter::name\*(R". This means that the full names | |
320 | for canned filters, \*(L"null\*(R" and \*(L"utf8\*(R", included with this module are: | |
321 | .Sp | |
322 | .Vb 2 | |
323 | \& DBM_Filter::null | |
324 | \& DBM_Filter::utf8 | |
325 | .Ve | |
326 | .IP "params" 5 | |
327 | .IX Item "params" | |
328 | any optional parameters that need to be sent to the filter. See the | |
329 | encode filter for an example of a module that uses parameters. | |
330 | .PP | |
331 | The module that implements the canned filter can take one of two | |
332 | forms. Here is a template for the first | |
333 | .PP | |
334 | .Vb 1 | |
335 | \& package DBM_Filter::null ; | |
336 | .Ve | |
337 | .PP | |
338 | .Vb 2 | |
339 | \& use strict; | |
340 | \& use warnings; | |
341 | .Ve | |
342 | .PP | |
343 | .Vb 4 | |
344 | \& sub Store | |
345 | \& { | |
346 | \& # store code here | |
347 | \& } | |
348 | .Ve | |
349 | .PP | |
350 | .Vb 4 | |
351 | \& sub Fetch | |
352 | \& { | |
353 | \& # fetch code here | |
354 | \& } | |
355 | .Ve | |
356 | .PP | |
357 | .Vb 1 | |
358 | \& 1; | |
359 | .Ve | |
360 | .PP | |
361 | Notes: | |
362 | .IP "1." 5 | |
363 | The package name uses the \f(CW\*(C`DBM_Filter::\*(C'\fR prefix. | |
364 | .IP "2." 5 | |
365 | The module \fImust\fR have both a Store and a Fetch method. If only one is | |
366 | present, or neither are present, a fatal error will be thrown. | |
367 | .PP | |
368 | The second form allows the filter to hold state information using a | |
369 | closure, thus: | |
370 | .PP | |
371 | .Vb 1 | |
372 | \& package DBM_Filter::encoding ; | |
373 | .Ve | |
374 | .PP | |
375 | .Vb 2 | |
376 | \& use strict; | |
377 | \& use warnings; | |
378 | .Ve | |
379 | .PP | |
380 | .Vb 3 | |
381 | \& sub Filter | |
382 | \& { | |
383 | \& my @params = @_ ; | |
384 | .Ve | |
385 | .PP | |
386 | .Vb 6 | |
387 | \& ... | |
388 | \& return { | |
389 | \& Store => sub { $_ = $encoding->encode($_) }, | |
390 | \& Fetch => sub { $_ = $encoding->decode($_) } | |
391 | \& } ; | |
392 | \& } | |
393 | .Ve | |
394 | .PP | |
395 | .Vb 1 | |
396 | \& 1; | |
397 | .Ve | |
398 | .PP | |
399 | In this instance the \*(L"Store\*(R" and \*(L"Fetch\*(R" methods are encapsulated inside a | |
400 | \&\*(L"Filter\*(R" method. | |
401 | .SH "Filters Included" | |
402 | .IX Header "Filters Included" | |
403 | A number of canned filers are provided with this module. They cover a | |
404 | number of the main areas that filters are needed when interfacing with | |
405 | \&\s-1DBM\s0 files. They also act as templates for your own filters. | |
406 | .PP | |
407 | The filter included are: | |
408 | .IP "* utf8" 5 | |
409 | .IX Item "utf8" | |
410 | This module will ensure that all data written to the \s-1DBM\s0 will be encoded | |
411 | in \s-1UTF\-8\s0. | |
412 | .Sp | |
413 | This module needs the Encode module. | |
414 | .IP "* encode" 5 | |
415 | .IX Item "encode" | |
416 | Allows you to choose the character encoding will be store in the \s-1DBM\s0 file. | |
417 | .IP "* compress" 5 | |
418 | .IX Item "compress" | |
419 | This filter will compress all data before it is written to the database | |
420 | and uncompressed it on reading. | |
421 | .Sp | |
422 | This module needs Compress::Zlib. | |
423 | .IP "* int32" 5 | |
424 | .IX Item "int32" | |
425 | This module is used when interoperating with a C/\*(C+ application that | |
426 | uses a C int as either the key and/or value in the \s-1DBM\s0 file. | |
427 | .IP "* null" 5 | |
428 | .IX Item "null" | |
429 | This module ensures that all data written to the \s-1DBM\s0 file is null | |
430 | terminated. This is useful when you have a perl script that needs | |
431 | to interoperate with a \s-1DBM\s0 file that a C program also uses. A fairly | |
432 | common issue is for the C application to include the terminating null | |
433 | in a string when it writes to the \s-1DBM\s0 file. This filter will ensure that | |
434 | all data written to the \s-1DBM\s0 file can be read by the C application. | |
435 | .SH "NOTES" | |
436 | .IX Header "NOTES" | |
437 | .Sh "Maintain Round Trip Integrity" | |
438 | .IX Subsection "Maintain Round Trip Integrity" | |
439 | When writing a \s-1DBM\s0 filter it is \fIvery\fR important to ensure that it is | |
440 | possible to retrieve all data that you have written when the \s-1DBM\s0 filter | |
441 | is in place. In practice, this means that whatever transformation is | |
442 | applied to the data in the Store method, the \fIexact\fR inverse operation | |
443 | should be applied in the Fetch method. | |
444 | .PP | |
445 | If you don't provide an exact inverse transformation, you will find that | |
446 | code like this will not behave as you expect. | |
447 | .PP | |
448 | .Vb 4 | |
449 | \& while (my ($k, $v) = each %hash) | |
450 | \& { | |
451 | \& ... | |
452 | \& } | |
453 | .Ve | |
454 | .PP | |
455 | Depending on the transformation, you will find that one or more of the | |
456 | following will happen | |
457 | .IP "1" 5 | |
458 | .IX Item "1" | |
459 | The loop will never terminate. | |
460 | .IP "2" 5 | |
461 | .IX Item "2" | |
462 | Too few records will be retrieved. | |
463 | .IP "3" 5 | |
464 | .IX Item "3" | |
465 | Too many will be retrieved. | |
466 | .IP "4" 5 | |
467 | .IX Item "4" | |
468 | The loop will do the right thing for a while, but it will unexpectedly fail. | |
469 | .Sh "Don't mix filtered & non-filtered data in the same database file." | |
470 | .IX Subsection "Don't mix filtered & non-filtered data in the same database file." | |
471 | This is just a restatement of the previous section. Unless you are | |
472 | completely certain you know what you are doing, avoid mixing filtered & | |
473 | non-filtered data. | |
474 | .SH "EXAMPLE" | |
475 | .IX Header "EXAMPLE" | |
476 | Say you need to interoperate with a legacy C application that stores | |
477 | keys as C ints and the values and null terminated \s-1UTF\-8\s0 strings. Here | |
478 | is how you would set that up | |
479 | .PP | |
480 | .Vb 1 | |
481 | \& my $db = tie %hash, 'SDBM_File', ... | |
482 | .Ve | |
483 | .PP | |
484 | .Vb 1 | |
485 | \& $db->Filter_Key_Push('int32') ; | |
486 | .Ve | |
487 | .PP | |
488 | .Vb 2 | |
489 | \& $db->Filter_Value_Push('utf8'); | |
490 | \& $db->Filter_Value_Push('null'); | |
491 | .Ve | |
492 | .SH "SEE ALSO" | |
493 | .IX Header "SEE ALSO" | |
494 | <DB_File>, GDBM_File, NDBM_File, ODBM_File, SDBM_File, perldbmfilter | |
495 | .SH "AUTHOR" | |
496 | .IX Header "AUTHOR" | |
497 | Paul Marquess <pmqs@cpan.org> |