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 "PERLDBMFILTER 1" | |
132 | .TH PERLDBMFILTER 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perldbmfilter \- Perl DBM Filters | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& $db = tie %hash, 'DBM', ... | |
139 | .Ve | |
140 | .PP | |
141 | .Vb 4 | |
142 | \& $old_filter = $db->filter_store_key ( sub { ... } ); | |
143 | \& $old_filter = $db->filter_store_value( sub { ... } ); | |
144 | \& $old_filter = $db->filter_fetch_key ( sub { ... } ); | |
145 | \& $old_filter = $db->filter_fetch_value( sub { ... } ); | |
146 | .Ve | |
147 | .SH "DESCRIPTION" | |
148 | .IX Header "DESCRIPTION" | |
149 | The four \f(CW\*(C`filter_*\*(C'\fR methods shown above are available in all the \s-1DBM\s0 | |
150 | modules that ship with Perl, namely DB_File, GDBM_File, NDBM_File, | |
151 | ODBM_File and SDBM_File. | |
152 | .PP | |
153 | Each of the methods work identically, and are used to install (or | |
154 | uninstall) a single \s-1DBM\s0 Filter. The only difference between them is the | |
155 | place that the filter is installed. | |
156 | .PP | |
157 | To summarise: | |
158 | .IP "\fBfilter_store_key\fR" 5 | |
159 | .IX Item "filter_store_key" | |
160 | If a filter has been installed with this method, it will be invoked | |
161 | every time you write a key to a \s-1DBM\s0 database. | |
162 | .IP "\fBfilter_store_value\fR" 5 | |
163 | .IX Item "filter_store_value" | |
164 | If a filter has been installed with this method, it will be invoked | |
165 | every time you write a value to a \s-1DBM\s0 database. | |
166 | .IP "\fBfilter_fetch_key\fR" 5 | |
167 | .IX Item "filter_fetch_key" | |
168 | If a filter has been installed with this method, it will be invoked | |
169 | every time you read a key from a \s-1DBM\s0 database. | |
170 | .IP "\fBfilter_fetch_value\fR" 5 | |
171 | .IX Item "filter_fetch_value" | |
172 | If a filter has been installed with this method, it will be invoked | |
173 | every time you read a value from a \s-1DBM\s0 database. | |
174 | .PP | |
175 | You can use any combination of the methods from none to all four. | |
176 | .PP | |
177 | All filter methods return the existing filter, if present, or \f(CW\*(C`undef\*(C'\fR | |
178 | in not. | |
179 | .PP | |
180 | To delete a filter pass \f(CW\*(C`undef\*(C'\fR to it. | |
181 | .Sh "The Filter" | |
182 | .IX Subsection "The Filter" | |
183 | When each filter is called by Perl, a local copy of \f(CW$_\fR will contain | |
184 | the key or value to be filtered. Filtering is achieved by modifying | |
185 | the contents of \f(CW$_\fR. The return code from the filter is ignored. | |
186 | .Sh "An Example \*(-- the \s-1NULL\s0 termination problem." | |
187 | .IX Subsection "An Example the NULL termination problem." | |
188 | \&\s-1DBM\s0 Filters are useful for a class of problems where you \fIalways\fR | |
189 | want to make the same transformation to all keys, all values or both. | |
190 | .PP | |
191 | For example, consider the following scenario. You have a \s-1DBM\s0 database | |
192 | that you need to share with a third-party C application. The C application | |
193 | assumes that \fIall\fR keys and values are \s-1NULL\s0 terminated. Unfortunately | |
194 | when Perl writes to \s-1DBM\s0 databases it doesn't use \s-1NULL\s0 termination, so | |
195 | your Perl application will have to manage \s-1NULL\s0 termination itself. When | |
196 | you write to the database you will have to use something like this: | |
197 | .PP | |
198 | .Vb 1 | |
199 | \& $hash{"$key\e0"} = "$value\e0"; | |
200 | .Ve | |
201 | .PP | |
202 | Similarly the \s-1NULL\s0 needs to be taken into account when you are considering | |
203 | the length of existing keys/values. | |
204 | .PP | |
205 | It would be much better if you could ignore the \s-1NULL\s0 terminations issue | |
206 | in the main application code and have a mechanism that automatically | |
207 | added the terminating \s-1NULL\s0 to all keys and values whenever you write to | |
208 | the database and have them removed when you read from the database. As I'm | |
209 | sure you have already guessed, this is a problem that \s-1DBM\s0 Filters can | |
210 | fix very easily. | |
211 | .PP | |
212 | .Vb 4 | |
213 | \& use strict; | |
214 | \& use warnings; | |
215 | \& use SDBM_File; | |
216 | \& use Fcntl; | |
217 | .Ve | |
218 | .PP | |
219 | .Vb 3 | |
220 | \& my %hash; | |
221 | \& my $filename = "filt"; | |
222 | \& unlink $filename; | |
223 | .Ve | |
224 | .PP | |
225 | .Vb 2 | |
226 | \& my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640) | |
227 | \& or die "Cannot open $filename: $!\en"; | |
228 | .Ve | |
229 | .PP | |
230 | .Vb 6 | |
231 | \& # Install DBM Filters | |
232 | \& $db->filter_fetch_key ( sub { s/\e0$// } ); | |
233 | \& $db->filter_store_key ( sub { $_ .= "\e0" } ); | |
234 | \& $db->filter_fetch_value( | |
235 | \& sub { no warnings 'uninitialized'; s/\e0$// } ); | |
236 | \& $db->filter_store_value( sub { $_ .= "\e0" } ); | |
237 | .Ve | |
238 | .PP | |
239 | .Vb 5 | |
240 | \& $hash{"abc"} = "def"; | |
241 | \& my $a = $hash{"ABC"}; | |
242 | \& # ... | |
243 | \& undef $db; | |
244 | \& untie %hash; | |
245 | .Ve | |
246 | .PP | |
247 | The code above uses SDBM_File, but it will work with any of the \s-1DBM\s0 | |
248 | modules. | |
249 | .PP | |
250 | Hopefully the contents of each of the filters should be | |
251 | self\-explanatory. Both \*(L"fetch\*(R" filters remove the terminating \s-1NULL\s0, | |
252 | and both \*(L"store\*(R" filters add a terminating \s-1NULL\s0. | |
253 | .Sh "Another Example \*(-- Key is a C int." | |
254 | .IX Subsection "Another Example Key is a C int." | |
255 | Here is another real-life example. By default, whenever Perl writes to | |
256 | a \s-1DBM\s0 database it always writes the key and value as strings. So when | |
257 | you use this: | |
258 | .PP | |
259 | .Vb 1 | |
260 | \& $hash{12345} = "something"; | |
261 | .Ve | |
262 | .PP | |
263 | the key 12345 will get stored in the \s-1DBM\s0 database as the 5 byte string | |
264 | \&\*(L"12345\*(R". If you actually want the key to be stored in the \s-1DBM\s0 database | |
265 | as a C int, you will have to use \f(CW\*(C`pack\*(C'\fR when writing, and \f(CW\*(C`unpack\*(C'\fR | |
266 | when reading. | |
267 | .PP | |
268 | Here is a \s-1DBM\s0 Filter that does it: | |
269 | .PP | |
270 | .Vb 6 | |
271 | \& use strict; | |
272 | \& use warnings; | |
273 | \& use DB_File; | |
274 | \& my %hash; | |
275 | \& my $filename = "filt"; | |
276 | \& unlink $filename; | |
277 | .Ve | |
278 | .PP | |
279 | .Vb 2 | |
280 | \& my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH | |
281 | \& or die "Cannot open $filename: $!\en"; | |
282 | .Ve | |
283 | .PP | |
284 | .Vb 6 | |
285 | \& $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ); | |
286 | \& $db->filter_store_key ( sub { $_ = pack ("i", $_) } ); | |
287 | \& $hash{123} = "def"; | |
288 | \& # ... | |
289 | \& undef $db; | |
290 | \& untie %hash; | |
291 | .Ve | |
292 | .PP | |
293 | The code above uses DB_File, but again it will work with any of the | |
294 | \&\s-1DBM\s0 modules. | |
295 | .PP | |
296 | This time only two filters have been used \*(-- we only need to manipulate | |
297 | the contents of the key, so it wasn't necessary to install any value | |
298 | filters. | |
299 | .SH "SEE ALSO" | |
300 | .IX Header "SEE ALSO" | |
301 | DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File. | |
302 | .SH "AUTHOR" | |
303 | .IX Header "AUTHOR" | |
304 | Paul Marquess |