If a filter has been installed with this method, it will be invoked
every time you read a value from a DBM database.
<P></P></DL>
<P>You can use any combination of the methods from none to all four.</P>
<P>All filter methods return the existing filter, if present, or <A HREF="../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A>
in not.</P>
<P>To delete a filter pass <A HREF="../../lib/Pod/perlfunc.html#item_undef"><CODE>undef</CODE></A> to it.</P>
<P>
<H2><A NAME="the filter">The Filter</A></H2>
<P>When each filter is called by Perl, a local copy of <CODE>$_</CODE> will contain
the key or value to be filtered. Filtering is achieved by modifying
the contents of <CODE>$_</CODE>. The return code from the filter is ignored.</P>
<P>
<H2><A NAME="an example the null termination problem.">An Example -- the NULL termination problem.</A></H2>
<P>DBM Filters are useful for a class of problems where you <EM>always</EM>
want to make the same transformation to all keys, all values or both.</P>
<P>For example, consider the following scenario. You have a DBM database
that you need to share with a third-party C application. The C application
assumes that <EM>all</EM> keys and values are NULL terminated. Unfortunately
when Perl writes to DBM databases it doesn't use NULL termination, so
your Perl application will have to manage NULL termination itself. When
you write to the database you will have to use something like this:</P>
<PRE>
$hash{"$key\0"} = "$value\0" ;</PRE>
<P>Similarly the NULL needs to be taken into account when you are considering
the length of existing keys/values.</P>
<P>It would be much better if you could ignore the NULL terminations issue
in the main application code and have a mechanism that automatically
added the terminating NULL 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.</P>
<PRE>
use strict ;
use warnings ;
use SDBM_File ;
use Fcntl ;</PRE>
<PRE>
my %hash ;
my $filename = "/tmp/filt" ;
unlink $filename ;</PRE>
<PRE>
my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640)
or die "Cannot open $filename: $!\n" ;</PRE>
<PRE>
# Install DBM Filters
$db->filter_fetch_key ( sub { s/\0$// } ) ;
$db->filter_store_key ( sub { $_ .= "\0" } ) ;
$db->filter_fetch_value(
sub { no warnings 'uninitialized' ;s/\0$// } ) ;
$db->filter_store_value( sub { $_ .= "\0" } ) ;</PRE>
<PRE>
$hash{"abc"} = "def" ;
my $a = $hash{"ABC"} ;
# ...
undef $db ;
untie %hash ;</PRE>
<P>The code above uses SDBM_File, but it will work with any of the DBM
modules.</P>
<P>Hopefully the contents of each of the filters should be
self-explanatory. Both ``fetch'' filters remove the terminating NULL,
and both ``store'' filters add a terminating NULL.</P>
<P>
<H2><A NAME="another example key is a c int.">Another Example -- Key is a C int.</A></H2>
<P>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:</P>
<PRE>
$hash{12345} = "soemthing" ;</PRE>
<P>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 <A HREF="../../lib/Pod/perlfunc.html#item_pack"><CODE>pack</CODE></A> when writing, and <A HREF="../../lib/Pod/perlfunc.html#item_unpack"><CODE>unpack</CODE></A>
when reading.</P>
<P>Here is a DBM Filter that does it:</P>
<PRE>
use strict ;
use warnings ;
use DB_File ;
my %hash ;
my $filename = "/tmp/filt" ;
unlink $filename ;</PRE>
<PRE>
my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH