Debian Codegraph @

upstream/debian-unstable/man3/DBM_Filter.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "DBM_Filter 3perl"
58 .TH DBM_Filter 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 DBM_Filter \-\- Filter DBM keys/values
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 2
68 \& use DBM_Filter ;
69 \& use SDBM_File; # or DB_File, GDBM_File, NDBM_File, or ODBM_File
70 \&
71 \& $db = tie %hash, ...
72 \&
73 \& $db\->Filter_Push(Fetch => sub {...},
74 \& Store => sub {...});
75 \&
76 \& $db\->Filter_Push(\*(Aqmy_filter1\*(Aq);
77 \& $db\->Filter_Push(\*(Aqmy_filter2\*(Aq, params...);
78 \&
79 \& $db\->Filter_Key_Push(...) ;
80 \& $db\->Filter_Value_Push(...) ;
81 \&
82 \& $db\->Filter_Pop();
83 \& $db\->Filtered();
84 \&
85 \& package DBM_Filter::my_filter1;
86 \&
87 \& sub Store { ... }
88 \& sub Fetch { ... }
89 \&
90 \& 1;
91 \&
92 \& package DBM_Filter::my_filter2;
93 \&
94 \& sub Filter
95 \& {
96 \& my @opts = @_;
97 \& ...
98 \& return (
99 \& sub Store { ... },
100 \& sub Fetch { ... } );
101 \& }
102 \&
103 \& 1;
104 .Ve
105 .SH DESCRIPTION
106 .IX Header "DESCRIPTION"
107 This module provides an interface that allows filters to be applied
108 to tied Hashes associated with DBM files. It builds on the DBM Filter
109 hooks that are present in all the *DB*_File modules included with the
110 standard Perl source distribution from version 5.6.1 onwards. In addition
111 to the *DB*_File modules distributed with Perl, the BerkeleyDB module,
112 available on CPAN, supports the DBM Filter hooks. See perldbmfilter
113 for more details on the DBM Filter hooks.
114 .SH "What is a DBM Filter?"
115 .IX Header "What is a DBM Filter?"
116 A DBM Filter allows the keys and/or values in a tied hash to be modified
117 by some user-defined code just before it is written to the DBM file and
118 just after it is read back from the DBM file. For example, this snippet
119 of code
120 .PP
121 .Vb 1
122 \& $some_hash{"abc"} = 42;
123 .Ve
124 .PP
125 could potentially trigger two filters, one for the writing of the key
126 "abc" and another for writing the value 42. Similarly, this snippet
127 .PP
128 .Vb 1
129 \& my ($key, $value) = each %some_hash
130 .Ve
131 .PP
132 will trigger two filters, one for the reading of the key and one for
133 the reading of the value.
134 .PP
135 Like the existing DBM Filter functionality, this module arranges for the
136 \&\f(CW$_\fR variable to be populated with the key or value that a filter will
137 check. This usually means that most DBM filters tend to be very short.
138 .SS "So what's new?"
139 .IX Subsection "So what's new?"
140 The main enhancements over the standard DBM Filter hooks are:
141 .IP \(bu 4
142 A cleaner interface.
143 .IP \(bu 4
144 The ability to easily apply multiple filters to a single DBM file.
145 .IP \(bu 4
146 The ability to create "canned" filters. These allow commonly used filters
147 to be packaged into a stand-alone module.
148 .SH METHODS
149 .IX Header "METHODS"
150 This module will arrange for the following methods to be available via
151 the object returned from the \f(CW\*(C`tie\*(C'\fR call.
152 .ie n .SS "$db\->\fBFilter_Push()\fP / $db\->\fBFilter_Key_Push()\fP / $db\->\fBFilter_Value_Push()\fP"
153 .el .SS "\f(CW$db\fP\->\fBFilter_Push()\fP / \f(CW$db\fP\->\fBFilter_Key_Push()\fP / \f(CW$db\fP\->\fBFilter_Value_Push()\fP"
154 .IX Subsection "$db->Filter_Push() / $db->Filter_Key_Push() / $db->Filter_Value_Push()"
155 Add a filter to filter stack for the database, \f(CW$db\fR. The three formats
156 vary only in whether they apply to the DBM key, the DBM value or both.
157 .IP Filter_Push 5
158 .IX Item "Filter_Push"
159 The filter is applied to \fIboth\fR keys and values.
160 .IP Filter_Key_Push 5
161 .IX Item "Filter_Key_Push"
162 The filter is applied to the key \fIonly\fR.
163 .IP Filter_Value_Push 5
164 .IX Item "Filter_Value_Push"
165 The filter is applied to the value \fIonly\fR.
166 .ie n .SS $db\->\fBFilter_Pop()\fP
167 .el .SS \f(CW$db\fP\->\fBFilter_Pop()\fP
168 .IX Subsection "$db->Filter_Pop()"
169 Removes the last filter that was applied to the DBM file associated with
170 \&\f(CW$db\fR, if present.
171 .ie n .SS $db\->\fBFiltered()\fP
172 .el .SS \f(CW$db\fP\->\fBFiltered()\fP
173 .IX Subsection "$db->Filtered()"
174 Returns TRUE if there are any filters applied to the DBM associated
175 with \f(CW$db\fR. Otherwise returns FALSE.
176 .SH "Writing a Filter"
177 .IX Header "Writing a Filter"
178 Filters can be created in two main ways
179 .SS "Immediate Filters"
180 .IX Subsection "Immediate Filters"
181 An immediate filter allows you to specify the filter code to be used
182 at the point where the filter is applied to a dbm. In this mode the
183 Filter_*_Push methods expects to receive exactly two parameters.
184 .PP
185 .Vb 3
186 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ...
187 \& $db\->Filter_Push( Store => sub { },
188 \& Fetch => sub { });
189 .Ve
190 .PP
191 The code reference associated with \f(CW\*(C`Store\*(C'\fR will be called before any
192 key/value is written to the database and the code reference associated
193 with \f(CW\*(C`Fetch\*(C'\fR will be called after any key/value is read from the
194 database.
195 .PP
196 For example, here is a sample filter that adds a trailing NULL character
197 to all strings before they are written to the DBM file, and removes the
198 trailing NULL when they are read from the DBM file
199 .PP
200 .Vb 3
201 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ...
202 \& $db\->Filter_Push( Store => sub { $_ .= "\ex00" ; },
203 \& Fetch => sub { s/\ex00$// ; });
204 .Ve
205 .PP
206 Points to note:
207 .IP 1. 5
208 Both the Store and Fetch filters manipulate \f(CW$_\fR.
209 .SS "Canned Filters"
210 .IX Subsection "Canned Filters"
211 Immediate filters are useful for one-off situations. For more generic
212 problems it can be useful to package the filter up in its own module.
213 .PP
214 The usage is for a canned filter is:
215 .PP
216 .Vb 1
217 \& $db\->Filter_Push("name", params)
218 .Ve
219 .PP
220 where
221 .IP """name""" 5
222 .IX Item """name"""
223 is the name of the module to load. If the string specified does not
224 contain the package separator characters "::", it is assumed to refer to
225 the full module name "DBM_Filter::name". This means that the full names
226 for canned filters, "null" and "utf8", included with this module are:
227 .Sp
228 .Vb 2
229 \& DBM_Filter::null
230 \& DBM_Filter::utf8
231 .Ve
232 .IP params 5
233 .IX Item "params"
234 any optional parameters that need to be sent to the filter. See the
235 encode filter for an example of a module that uses parameters.
236 .PP
237 The module that implements the canned filter can take one of two
238 forms. Here is a template for the first
239 .PP
240 .Vb 1
241 \& package DBM_Filter::null ;
242 \&
243 \& use strict;
244 \& use warnings;
245 \&
246 \& sub Store
247 \& {
248 \& # store code here
249 \& }
250 \&
251 \& sub Fetch
252 \& {
253 \& # fetch code here
254 \& }
255 \&
256 \& 1;
257 .Ve
258 .PP
259 Notes:
260 .IP 1. 5
261 The package name uses the \f(CW\*(C`DBM_Filter::\*(C'\fR prefix.
262 .IP 2. 5
263 The module \fImust\fR have both a Store and a Fetch method. If only one is
264 present, or neither are present, a fatal error will be thrown.
265 .PP
266 The second form allows the filter to hold state information using a
267 closure, thus:
268 .PP
269 .Vb 1
270 \& package DBM_Filter::encoding ;
271 \&
272 \& use strict;
273 \& use warnings;
274 \&
275 \& sub Filter
276 \& {
277 \& my @params = @_ ;
278 \&
279 \& ...
280 \& return {
281 \& Store => sub { $_ = $encoding\->encode($_) },
282 \& Fetch => sub { $_ = $encoding\->decode($_) }
283 \& } ;
284 \& }
285 \&
286 \& 1;
287 .Ve
288 .PP
289 In this instance the "Store" and "Fetch" methods are encapsulated inside a
290 "Filter" method.
291 .SH "Filters Included"
292 .IX Header "Filters Included"
293 A number of canned filers are provided with this module. They cover a
294 number of the main areas that filters are needed when interfacing with
295 DBM files. They also act as templates for your own filters.
296 .PP
297 The filter included are:
298 .IP \(bu 5
299 utf8
300 .Sp
301 This module will ensure that all data written to the DBM will be encoded
302 in UTF\-8.
303 .Sp
304 This module needs the Encode module.
305 .IP \(bu 5
306 encode
307 .Sp
308 Allows you to choose the character encoding will be store in the DBM file.
309 .IP \(bu 5
310 compress
311 .Sp
312 This filter will compress all data before it is written to the database
313 and uncompressed it on reading.
314 .Sp
315 This module needs Compress::Zlib.
316 .IP \(bu 5
317 int32
318 .Sp
319 This module is used when interoperating with a C/C++ application that
320 uses a C int as either the key and/or value in the DBM file.
321 .IP \(bu 5
322 null
323 .Sp
324 This module ensures that all data written to the DBM file is null
325 terminated. This is useful when you have a perl script that needs
326 to interoperate with a DBM file that a C program also uses. A fairly
327 common issue is for the C application to include the terminating null
328 in a string when it writes to the DBM file. This filter will ensure that
329 all data written to the DBM file can be read by the C application.
330 .SH NOTES
331 .IX Header "NOTES"
332 .SS "Maintain Round Trip Integrity"
333 .IX Subsection "Maintain Round Trip Integrity"
334 When writing a DBM filter it is \fIvery\fR important to ensure that it is
335 possible to retrieve all data that you have written when the DBM filter
336 is in place. In practice, this means that whatever transformation is
337 applied to the data in the Store method, the \fIexact\fR inverse operation
338 should be applied in the Fetch method.
339 .PP
340 If you don't provide an exact inverse transformation, you will find that
341 code like this will not behave as you expect.
342 .PP
343 .Vb 4
344 \& while (my ($k, $v) = each %hash)
345 \& {
346 \& ...
347 \& }
348 .Ve
349 .PP
350 Depending on the transformation, you will find that one or more of the
351 following will happen
352 .IP 1. 5
353 The loop will never terminate.
354 .IP 2. 5
355 Too few records will be retrieved.
356 .IP 3. 5
357 Too many will be retrieved.
358 .IP 4. 5
359 The loop will do the right thing for a while, but it will unexpectedly fail.
360 .SS "Don't mix filtered & non-filtered data in the same database file."
361 .IX Subsection "Don't mix filtered & non-filtered data in the same database file."
362 This is just a restatement of the previous section. Unless you are
363 completely certain you know what you are doing, avoid mixing filtered &
364 non-filtered data.
365 .SH EXAMPLE
366 .IX Header "EXAMPLE"
367 Say you need to interoperate with a legacy C application that stores
368 keys as C ints and the values and null terminated UTF\-8 strings. Here
369 is how you would set that up
370 .PP
371 .Vb 1
372 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ...
373 \&
374 \& $db\->Filter_Key_Push(\*(Aqint32\*(Aq) ;
375 \&
376 \& $db\->Filter_Value_Push(\*(Aqutf8\*(Aq);
377 \& $db\->Filter_Value_Push(\*(Aqnull\*(Aq);
378 .Ve
379 .SH "SEE ALSO"
380 .IX Header "SEE ALSO"
381 <DB_File>, GDBM_File, NDBM_File, ODBM_File, SDBM_File, perldbmfilter
382 .SH AUTHOR
383 .IX Header "AUTHOR"
384 Paul Marquess <[email protected]>