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