PERLDBMFILTER(1) Perl Programmers Reference Guide PERLDBMFILTER(1)
NAME
perldbmfilter - Perl DBM Filters
SYNOPSIS
$db = tie %hash, 'DBM', ...
$oldfilter = $db->filterstorekey ( sub { ... } ) ;
$oldfilter = $db->filterstorevalue( sub { ... } ) ;
$oldfilter = $db->filterfetchkey ( sub { ... } ) ;
$oldfilter = $db->filterfetchvalue( sub { ... } ) ;
DESCRIPTION
The four "filter*" methods shown above are available in all the DBM
modules that ship with Perl, namely DBFile, GDBMFile, NDBMFile,
ODBMFile and SDBMFile.
Each of the methods work identically, and are used to install (or unin-
stall) a single DBM Filter. The only difference between them is the
place that the filter is installed.
To summarise:
filterstorekey
If a filter has been installed with this method, it will be
invoked every time you write a key to a DBM database.
filterstorevalue
If a filter has been installed with this method, it will be
invoked every time you write a value to a DBM database.
filterfetchkey
If a filter has been installed with this method, it will be
invoked every time you read a key from a DBM database.
filterfetchvalue
If a filter has been installed with this method, it will be
invoked every time you read a value from a DBM database.
You can use any combination of the methods from none to all four.
All filter methods return the existing filter, if present, or "undef"
in not.
To delete a filter pass "undef" to it.
The Filter
When each filter is called by Perl, a local copy of $ will contain the
key or value to be filtered. Filtering is achieved by modifying the
contents of $. The return code from the filter is ignored.
An Example -- the NUL termination problem.
DBM Filters are useful for a class of problems where you always want to
make the same transformation to all keys, all values or both.
For example, consider the following scenario. You have a DBM database
that you need to share with a third-party C application. The C applica-
tion assumes that all keys and values are NUL terminated. Unfortu-
nately when Perl writes to DBM databases it doesn't use NUL termina-
tion, so your Perl application will have to manage NUL termination
itself. When you write to the database you will have to use something
like this:
$hash{"$key\0"} = "$value\0" ;
Similarly the NUL needs to be taken into account when you are consid-
ering the length of existing keys/values.
It would be much better if you could ignore the NUL terminations issue
in the main application code and have a mechanism that automatically
added the terminating NUL to all keys and values whenever you write to
the database and have them removed when you read from the database. As
I'm sure you have already guessed, this is a problem that DBM Filters
can fix very easily.
use strict ;
use warnings ;
use SDBMFile ;
use Fcntl ;
my %hash ;
my $filename = "filt" ;
unlink $filename ;
my $db = tie(%hash, 'SDBMFile', $filename, ORDWROCREAT, 0640)
or die "Cannot open $filename: $!\n" ;
# Install DBM Filters
$db->filterfetchkey ( sub { s/\0$/ } ) ;
$db->filterstorekey ( sub { $ .= "\0" } ) ;
$db->filterfetchvalue(
sub { no warnings 'uninitialized' ;s/\0$/ } ) ;
$db->filterstorevalue( sub { $ .= "\0" } ) ;
$hash{"abc"} = "def" ;
my $a = $hash{"ABC"} ;
# ...
undef $db ;
untie %hash ;
The code above uses SDBMFile, but it will work with any of the DBM
modules.
Hopefully the contents of each of the filters should be self-explana-
tory. Both "fetch" filters remove the terminating NUL, and both
"store" filters add a terminating NUL.
Another Example -- Key is a C int.
Here is another real-life example. By default, whenever Perl writes to
a DBM database it always writes the key and value as strings. So when
you use this:
$hash{12345} = "something" ;
the key 12345 will get stored in the DBM database as the 5 byte string
"12345". If you actually want the key to be stored in the DBM database
as a C int, you will have to use "pack" when writing, and "unpack" when
reading.
Here is a DBM Filter that does it:
use strict ;
use warnings ;
use DBFile ;
my %hash ;
my $filename = "filt" ;
unlink $filename ;
my $db = tie %hash, 'DBFile', $filename, OCREATORDWR, 0666, $DBHASH
or die "Cannot open $filename: $!\n" ;
$db->filterfetchkey ( sub { $ = unpack("i", $) } ) ;
$db->filterstorekey ( sub { $ = pack ("i", $) } ) ;
$hash{123} = "def" ;
# ...
undef $db ;
untie %hash ;
The code above uses DBFile, but again it will work with any of the DBM
modules.
This time only two filters have been used -- we only need to manipulate
the contents of the key, so it wasn't necessary to install any value
filters.
SEE ALSO
DBFile, GDBMFile, NDBMFile, ODBMFile and SDBMFile.
AUTHOR
Paul Marquess
perl v5.8.6 2004-11-05 PERLDBMFILTER(1)
|