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

SEE ALSO

151       DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File.
152

AUTHOR

154       Paul Marquess
155
156
157
158perl v5.34.1                      2022-03-15                  PERLDBMFILTER(1)
Impressum