PERLDBMFILTER(1)

πŸ’– perldbmfilter β€” Perl DBM Filters πŸ’–

ok so bestie, welcome to perldbmfilter, aka the page that lives in the Perl Programmers Reference Guide and is here to make your DBM databases do tricks. πŸŽ€ no cap, this one's a banger.


✨ 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 { ... } );

🌸 DESCRIPTION 🌸

ok so those four filter_* methods up there? they're available in every single DBM module that ships with Perl, and that's DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File. all of them. the whole squad. 🫢

each of the methods works identically β€” they install (or uninstall) a single DBM Filter. the only difference between them is where the filter gets installed. that's it. that's the whole plot twist.

to summarise (love a summary moment):

  • filter_store_key β€” if a filter has been installed with this method, it will be invoked every time you write a key</u> to a DBM database.
  • 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.
  • 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.
  • 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.

you can use any combination of the methods, from none to all four. go feral. ✨

all filter methods return the existing filter, if present, or undef if not.

and to delete a filter? you pass undef to it. bye bestie πŸ‘‹


🧚 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 $_. and the return code from the filter? ignored. literally doesn't matter. don't stress about it.


πŸ¦‹ An Example: the NULL 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 this scenario. you have a DBM database that you need to share with a third-party C application. the C application assumes that 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. and as i'm sure you have already guessed, this is a problem that DBM Filters can fix very easily. πŸ’…

use v5.36;
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 $x = $hash{"ABC"};
# ...
undef $db;
untie %hash;

the code above uses SDBM_File, but it will work with any of the DBM modules. no gatekeeping here.

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. slay.


⭐ 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. ikr.

here is a DBM Filter that does it:

use v5.36;
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. lowkey efficient of us.


πŸ‘‘ SEE ALSO πŸ‘‘

DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File.


πŸŽ€ AUTHOR πŸŽ€

Paul Marquess


ok that's the whole page bestie, you're basically a DBM filter main character now πŸ’…βœ¨

this lil masterpiece is from perl v5.42.3, dated 2026-08-03, and it goes by PERLDBMFILTER(1) if you ever need to cite it in a group chat. mwah πŸ«ΆπŸ’–
πŸ“¦ where this page comes from