/usr/share/perl5/pod
=head1 NAME perldbmfilter - Perl DBM Filters =head1 SYNOPSIS $db = tie %hash, 'DBM', ... $old_filter = $db->filter_store_key ( sub { ... } ); $old_filter = $db->filter_store_value( sub { ... } ); $old_filter = $db->filter_fetch_key ( sub { ... } ); $old_filter = $db->filter_fetch_value( sub { ... } ); =head1 DESCRIPTION The four C<filter_*> methods shown above are available in all the DBM modules that ship with Perl, namely DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File. Each of the methods works identically, and is used to install (or uninstall) a single DBM Filter. The only difference between them is the place that the filter is installed. To summarise: =over 5 =item B<filter_store_key> If a filter has been installed with this method, it will be invoked every time you write a key to a DBM database. =item B<filter_store_value> If a filter has been installed with this method, it will be invoked every time you write a value to a DBM database. =item B<filter_fetch_key> If a filter has been installed with this method, it will be invoked every time you read a key from a DBM database. =item B<filter_fetch_value> If a filter has been installed with this method, it will be invoked every time you read a value from a DBM database. =back You can use any combination of the methods from none to all four. All filter methods return the existing filter, if present, or C<undef> if not. To delete a filter pass C<undef> to it. =head2 The Filter When each filter is called by Perl, a local copy of C<$_> will contain the key or value to be filtered. Filtering is achieved by modifying the contents of C<$_>. The return code from the filter is ignored. =head2 An Example: the NULL termination problem. DBM Filters are useful for a class of problems where you I<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 application assumes that I<all> 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: $hash{"$key\0"} = "$value\0"; Similarly the NULL needs to be taken into account when you are considering the length of existing keys/values. 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. use strict; use warnings; use SDBM_File; use Fcntl; my %hash; my $filename = "filt"; unlink $filename; my $db = tie(%hash, 'SDBM_File', $filename, O_RDWR|O_CREAT, 0640) or die "Cannot open $filename: $!\n"; # 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" } ); $hash{"abc"} = "def"; my $a = $hash{"ABC"}; # ... undef $db; untie %hash; The code above uses SDBM_File, but it will work with any of the DBM modules. 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. =head2 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 C<pack> when writing, and C<unpack> when reading. Here is a DBM Filter that does it: use strict; use warnings; use DB_File; my %hash; my $filename = "filt"; unlink $filename; my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH or die "Cannot open $filename: $!\n"; $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ); $db->filter_store_key ( sub { $_ = pack ("i", $_) } ); $hash{123} = "def"; # ... undef $db; untie %hash; The code above uses DB_File, 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. =head1 SEE ALSO L<DB_File>, L<GDBM_File>, L<NDBM_File>, L<ODBM_File> and L<SDBM_File>. =head1 AUTHOR Paul Marquess
.
Edit
..
Edit
perl.pod
Edit
perl5004delta.pod
Edit
perl5005delta.pod
Edit
perl5100delta.pod
Edit
perl5101delta.pod
Edit
perl5120delta.pod
Edit
perl5121delta.pod
Edit
perl5122delta.pod
Edit
perl5123delta.pod
Edit
perl5124delta.pod
Edit
perl5125delta.pod
Edit
perl5140delta.pod
Edit
perl5141delta.pod
Edit
perl5142delta.pod
Edit
perl5143delta.pod
Edit
perl5144delta.pod
Edit
perl5160delta.pod
Edit
perl5161delta.pod
Edit
perl5162delta.pod
Edit
perl5163delta.pod
Edit
perl5180delta.pod
Edit
perl5181delta.pod
Edit
perl5182delta.pod
Edit
perl5184delta.pod
Edit
perl5200delta.pod
Edit
perl5201delta.pod
Edit
perl5202delta.pod
Edit
perl5203delta.pod
Edit
perl5220delta.pod
Edit
perl5221delta.pod
Edit
perl5222delta.pod
Edit
perl5223delta.pod
Edit
perl5224delta.pod
Edit
perl5240delta.pod
Edit
perl5241delta.pod
Edit
perl5242delta.pod
Edit
perl5243delta.pod
Edit
perl5244delta.pod
Edit
perl5260delta.pod
Edit
perl5261delta.pod
Edit
perl5262delta.pod
Edit
perl5263delta.pod
Edit
perl5280delta.pod
Edit
perl5281delta.pod
Edit
perl5282delta.pod
Edit
perl5283delta.pod
Edit
perl5300delta.pod
Edit
perl5301delta.pod
Edit
perl5302delta.pod
Edit
perl5303delta.pod
Edit
perl5320delta.pod
Edit
perl5321delta.pod
Edit
perl561delta.pod
Edit
perl56delta.pod
Edit
perl581delta.pod
Edit
perl582delta.pod
Edit
perl583delta.pod
Edit
perl584delta.pod
Edit
perl585delta.pod
Edit
perl586delta.pod
Edit
perl587delta.pod
Edit
perl588delta.pod
Edit
perl589delta.pod
Edit
perl58delta.pod
Edit
perlaix.pod
Edit
perlamiga.pod
Edit
perlandroid.pod
Edit
perlapi.pod
Edit
perlapio.pod
Edit
perlartistic.pod
Edit
perlbook.pod
Edit
perlboot.pod
Edit
perlbot.pod
Edit
perlbs2000.pod
Edit
perlcall.pod
Edit
perlcheat.pod
Edit
perlclib.pod
Edit
perlcn.pod
Edit
perlcommunity.pod
Edit
perlcygwin.pod
Edit
perldata.pod
Edit
perldbmfilter.pod
Edit
perldebguts.pod
Edit
perldebtut.pod
Edit
perldebug.pod
Edit
perldelta.pod
Edit
perldeprecation.pod
Edit
perldiag.pod
Edit
perldos.pod
Edit
perldsc.pod
Edit
perldtrace.pod
Edit
perlebcdic.pod
Edit
perlembed.pod
Edit
perlexperiment.pod
Edit
perlfork.pod
Edit
perlform.pod
Edit
perlfreebsd.pod
Edit
perlfunc.pod
Edit
perlgit.pod
Edit
perlgov.pod
Edit
perlgpl.pod
Edit
perlguts.pod
Edit
perlhack.pod
Edit
perlhacktips.pod
Edit
perlhacktut.pod
Edit
perlhaiku.pod
Edit
perlhist.pod
Edit
perlhpux.pod
Edit
perlhurd.pod
Edit
perlintern.pod
Edit
perlinterp.pod
Edit
perlintro.pod
Edit
perliol.pod
Edit
perlipc.pod
Edit
perlirix.pod
Edit
perljp.pod
Edit
perlko.pod
Edit
perllexwarn.pod
Edit
perllinux.pod
Edit
perllocale.pod
Edit
perllol.pod
Edit
perlmacos.pod
Edit
perlmacosx.pod
Edit
perlmod.pod
Edit
perlmodinstall.pod
Edit
perlmodlib.pod
Edit
perlmodstyle.pod
Edit
perlmroapi.pod
Edit
perlnetware.pod
Edit
perlnewmod.pod
Edit
perlnumber.pod
Edit
perlobj.pod
Edit
perlootut.pod
Edit
perlop.pod
Edit
perlopenbsd.pod
Edit
perlopentut.pod
Edit
perlos2.pod
Edit
perlos390.pod
Edit
perlos400.pod
Edit
perlpacktut.pod
Edit
perlperf.pod
Edit
perlplan9.pod
Edit
perlpod.pod
Edit
perlpodspec.pod
Edit
perlpolicy.pod
Edit
perlport.pod
Edit
perlpragma.pod
Edit
perlqnx.pod
Edit
perlre.pod
Edit
perlreapi.pod
Edit
perlrebackslash.pod
Edit
perlrecharclass.pod
Edit
perlref.pod
Edit
perlreftut.pod
Edit
perlreguts.pod
Edit
perlrepository.pod
Edit
perlrequick.pod
Edit
perlreref.pod
Edit
perlretut.pod
Edit
perlriscos.pod
Edit
perlrun.pod
Edit
perlsec.pod
Edit
perlsecpolicy.pod
Edit
perlsolaris.pod
Edit
perlsource.pod
Edit
perlstyle.pod
Edit
perlsub.pod
Edit
perlsymbian.pod
Edit
perlsyn.pod
Edit
perlsynology.pod
Edit
perlthrtut.pod
Edit
perltie.pod
Edit
perltoc.pod
Edit
perltodo.pod
Edit
perltooc.pod
Edit
perltoot.pod
Edit
perltrap.pod
Edit
perltru64.pod
Edit
perltw.pod
Edit
perlunicode.pod
Edit
perlunicook.pod
Edit
perlunifaq.pod
Edit
perluniintro.pod
Edit
perluniprops.pod
Edit
perlunitut.pod
Edit
perlutil.pod
Edit
perlvar.pod
Edit
perlvms.pod
Edit
perlvos.pod
Edit
perlwin32.pod
Edit