Commit | Line | Data |
9fe6733a |
1 | =head1 NAME |
2 | |
3 | perldbmfilter - Perl DBM Filters |
4 | |
5 | =head1 SYNOPSIS |
6 | |
7 | $db = tie %hash, 'DBM', ... |
8 | |
9 | $old_filter = $db->filter_store_key ( sub { ... } ) ; |
10 | $old_filter = $db->filter_store_value( sub { ... } ) ; |
11 | $old_filter = $db->filter_fetch_key ( sub { ... } ) ; |
12 | $old_filter = $db->filter_fetch_value( sub { ... } ) ; |
13 | |
14 | =head1 DESCRIPTION |
15 | |
16 | The four C<filter_*> methods shown above are available in all the DBM |
17 | modules that ship with Perl, namely DB_File, GDBM_File, NDBM_File, |
18 | ODBM_File and SDBM_File. |
19 | |
20 | Each of the methods work identically, and are used to install (or |
21 | uninstall) a single DBM Filter. The only difference between them is the |
22 | place that the filter is installed. |
23 | |
24 | To summarise: |
25 | |
26 | =over 5 |
27 | |
28 | =item B<filter_store_key> |
29 | |
30 | If a filter has been installed with this method, it will be invoked |
31 | every time you write a key to a DBM database. |
32 | |
33 | =item B<filter_store_value> |
34 | |
35 | If a filter has been installed with this method, it will be invoked |
36 | every time you write a value to a DBM database. |
37 | |
38 | |
39 | =item B<filter_fetch_key> |
40 | |
41 | If a filter has been installed with this method, it will be invoked |
42 | every time you read a key from a DBM database. |
43 | |
44 | =item B<filter_fetch_value> |
45 | |
46 | If a filter has been installed with this method, it will be invoked |
47 | every time you read a value from a DBM database. |
48 | |
49 | =back |
50 | |
51 | You can use any combination of the methods from none to all four. |
52 | |
53 | All filter methods return the existing filter, if present, or C<undef> |
54 | in not. |
55 | |
56 | To delete a filter pass C<undef> to it. |
57 | |
58 | =head2 The Filter |
59 | |
60 | When each filter is called by Perl, a local copy of C<$_> will contain |
61 | the key or value to be filtered. Filtering is achieved by modifying |
62 | the contents of C<$_>. The return code from the filter is ignored. |
63 | |
64 | =head2 An Example -- the NULL termination problem. |
65 | |
66 | DBM Filters are useful for a class of problems where you I<always> |
67 | want to make the same transformation to all keys, all values or both. |
68 | |
69 | For example, consider the following scenario. You have a DBM database |
70 | that you need to share with a third-party C application. The C application |
71 | assumes that I<all> keys and values are NULL terminated. Unfortunately |
72 | when Perl writes to DBM databases it doesn't use NULL termination, so |
73 | your Perl application will have to manage NULL termination itself. When |
74 | you write to the database you will have to use something like this: |
75 | |
76 | $hash{"$key\0"} = "$value\0" ; |
77 | |
78 | Similarly the NULL needs to be taken into account when you are considering |
79 | the length of existing keys/values. |
80 | |
81 | It would be much better if you could ignore the NULL terminations issue |
82 | in the main application code and have a mechanism that automatically |
83 | added the terminating NULL to all keys and values whenever you write to |
84 | the database and have them removed when you read from the database. As I'm |
85 | sure you have already guessed, this is a problem that DBM Filters can |
86 | fix very easily. |
87 | |
88 | use strict ; |
89 | use SDBM_File ; |
90 | use Fcntl ; |
91 | |
92 | my %hash ; |
93 | my $filename = "/tmp/filt" ; |
94 | unlink $filename ; |
95 | |
96 | my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640) |
97 | or die "Cannot open $filename: $!\n" ; |
98 | |
99 | # Install DBM Filters |
100 | $db->filter_fetch_key ( sub { s/\0$// } ) ; |
101 | $db->filter_store_key ( sub { $_ .= "\0" } ) ; |
102 | $db->filter_fetch_value( sub { s/\0$// } ) ; |
103 | $db->filter_store_value( sub { $_ .= "\0" } ) ; |
104 | |
105 | $hash{"abc"} = "def" ; |
106 | my $a = $hash{"ABC"} ; |
107 | # ... |
108 | undef $db ; |
109 | untie %hash ; |
110 | |
111 | The code above uses SDBM_File, but it will work with any of the DBM |
112 | modules. |
113 | |
114 | Hopefully the contents of each of the filters should be |
115 | self-explanatory. Both "fetch" filters remove the terminating NULL, |
116 | and both "store" filters add a terminating NULL. |
117 | |
118 | |
119 | =head2 Another Example -- Key is a C int. |
120 | |
121 | Here is another real-life example. By default, whenever Perl writes to |
122 | a DBM database it always writes the key and value as strings. So when |
123 | you use this: |
124 | |
125 | $hash{12345} = "soemthing" ; |
126 | |
127 | the key 12345 will get stored in the DBM database as the 5 byte string |
128 | "12345". If you actually want the key to be stored in the DBM database |
129 | as a C int, you will have to use C<pack> when writing, and C<unpack> |
130 | when reading. |
131 | |
132 | Here is a DBM Filter that does it: |
133 | |
134 | use strict ; |
135 | use DB_File ; |
136 | my %hash ; |
137 | my $filename = "/tmp/filt" ; |
138 | unlink $filename ; |
139 | |
140 | |
141 | my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH |
142 | or die "Cannot open $filename: $!\n" ; |
143 | |
144 | $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ) ; |
145 | $db->filter_store_key ( sub { $_ = pack ("i", $_) } ) ; |
146 | $hash{123} = "def" ; |
147 | # ... |
148 | undef $db ; |
149 | untie %hash ; |
150 | |
151 | The code above uses DB_File, but again it will work with any of the |
152 | DBM modules. |
153 | |
154 | This time only two filters have been used -- we only need to manipulate |
155 | the contents of the key, so it wasn't necessary to install any value |
156 | filters. |
157 | |
158 | =head1 SEE ALSO |
159 | |
160 | L<DB_File>, L<GDBM_File>, L<NDBM_File>, L<ODBM_File> and L<SDBM_File>. |
161 | |
162 | =head1 AUTHOR |
163 | |
164 | Paul Marquess |
165 | |