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 v5.36;
80           use SDBM_File;
81           use Fcntl;
82
83           my %hash;
84           my $filename = "filt";
85           unlink $filename;
86
87           my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640)
88             or die "Cannot open $filename: $!\n";
89
90           # Install DBM Filters
91           $db->filter_fetch_key  ( sub { s/\0$//    } );
92           $db->filter_store_key  ( sub { $_ .= "\0" } );
93           $db->filter_fetch_value(
94               sub { no warnings 'uninitialized'; s/\0$// } );
95           $db->filter_store_value( sub { $_ .= "\0" } );
96
97           $hash{"abc"} = "def";
98           my $a = $hash{"ABC"};
99           # ...
100           undef $db;
101           untie %hash;
102
103       The code above uses SDBM_File, but it will work with any of the DBM
104       modules.
105
106       Hopefully the contents of each of the filters should be self-
107       explanatory. Both "fetch" filters remove the terminating NULL, and both
108       "store" filters add a terminating NULL.
109
110   Another Example: Key is a C int.
111       Here is another real-life example. By default, whenever Perl writes to
112       a DBM database it always writes the key and value as strings. So when
113       you use this:
114
115           $hash{12345} = "something";
116
117       the key 12345 will get stored in the DBM database as the 5 byte string
118       "12345". If you actually want the key to be stored in the DBM database
119       as a C int, you will have to use "pack" when writing, and "unpack" when
120       reading.
121
122       Here is a DBM Filter that does it:
123
124           use v5.36;
125           use DB_File;
126           my %hash;
127           my $filename = "filt";
128           unlink $filename;
129
130
131           my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666,
132               $DB_HASH or die "Cannot open $filename: $!\n";
133
134           $db->filter_fetch_key  ( sub { $_ = unpack("i", $_) } );
135           $db->filter_store_key  ( sub { $_ = pack ("i", $_) } );
136           $hash{123} = "def";
137           # ...
138           undef $db;
139           untie %hash;
140
141       The code above uses DB_File, but again it will work with any of the DBM
142       modules.
143
144       This time only two filters have been used; we only need to manipulate
145       the contents of the key, so it wasn't necessary to install any value
146       filters.
147

SEE ALSO

149       DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File.
150

AUTHOR

152       Paul Marquess
153
154
155
156perl v5.38.2                      2023-11-30                  PERLDBMFILTER(1)
Impressum