1PERLDBMFILTER(1)       Perl Programmers Reference Guide       PERLDBMFILTER(1)
2
3
4

NAME

6       perldbmfilter - Perl DBM Filters
7

SYNOPSIS

9           $db = tie %hash, 'DBM', ...
10
11           $old_filter = $db->filter_store_key  ( sub { ... } );
12           $old_filter = $db->filter_store_value( sub { ... } );
13           $old_filter = $db->filter_fetch_key  ( sub { ... } );
14           $old_filter = $db->filter_fetch_value( sub { ... } );
15

DESCRIPTION

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

SEE ALSO

153       DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File.
154

AUTHOR

156       Paul Marquess
157
158
159
160perl v5.8.8                       2006-01-07                  PERLDBMFILTER(1)
Impressum