.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "PERLDBMFILTER 1" .TH PERLDBMFILTER 1 2024-05-07 "perl v5.42.0" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME perldbmfilter \- Perl DBM Filters .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& $db = tie %hash, \*(AqDBM\*(Aq, ... \& \& $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 { ... } ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" The four \f(CW\*(C`filter_*\*(C'\fR 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. .PP 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. .PP To summarise: .IP \fBfilter_store_key\fR 5 .IX Item "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. .IP \fBfilter_store_value\fR 5 .IX Item "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. .IP \fBfilter_fetch_key\fR 5 .IX Item "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. .IP \fBfilter_fetch_value\fR 5 .IX Item "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. .PP You can use any combination of the methods from none to all four. .PP All filter methods return the existing filter, if present, or \f(CW\*(C`undef\*(C'\fR if not. .PP To delete a filter pass \f(CW\*(C`undef\*(C'\fR to it. .SS "The Filter" .IX Subsection "The Filter" When each filter is called by Perl, a local copy of \f(CW$_\fR will contain the key or value to be filtered. Filtering is achieved by modifying the contents of \f(CW$_\fR. The return code from the filter is ignored. .SS "An Example: the NULL termination problem" .IX Subsection "An Example: the NULL termination problem" DBM Filters are useful for a class of problems where you \fIalways\fR want to make the same transformation to all keys, all values or both. .PP 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 \fIall\fR keys and values are NULL terminated. Unfortunately when Perl writes to DBM databases it doesn\*(Aqt 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: .PP .Vb 1 \& $hash{"$key\e0"} = "$value\e0"; .Ve .PP Similarly the NULL needs to be taken into account when you are considering the length of existing keys/values. .PP 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\*(Aqm sure you have already guessed, this is a problem that DBM Filters can fix very easily. .PP .Vb 3 \& use v5.36; \& use SDBM_File; \& use Fcntl; \& \& my %hash; \& my $filename = "filt"; \& unlink $filename; \& \& my $db = tie(%hash, \*(AqSDBM_File\*(Aq, $filename, O_RDWR|O_CREAT, 0640) \& or die "Cannot open $filename: $!\en"; \& \& # Install DBM Filters \& $db\->filter_fetch_key ( sub { s/\e0$// } ); \& $db\->filter_store_key ( sub { $_ .= "\e0" } ); \& $db\->filter_fetch_value( \& sub { no warnings \*(Aquninitialized\*(Aq; s/\e0$// } ); \& $db\->filter_store_value( sub { $_ .= "\e0" } ); \& \& $hash{"abc"} = "def"; \& my $x = $hash{"ABC"}; \& # ... \& undef $db; \& untie %hash; .Ve .PP The code above uses SDBM_File, but it will work with any of the DBM modules. .PP 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. .SS "Another Example: Key is a C int" .IX Subsection "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: .PP .Vb 1 \& $hash{12345} = "something"; .Ve .PP 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 \f(CW\*(C`pack\*(C'\fR when writing, and \f(CW\*(C`unpack\*(C'\fR when reading. .PP Here is a DBM Filter that does it: .PP .Vb 5 \& use v5.36; \& use DB_File; \& my %hash; \& my $filename = "filt"; \& unlink $filename; \& \& \& my $db = tie %hash, \*(AqDB_File\*(Aq, $filename, O_CREAT|O_RDWR, 0666, \& $DB_HASH or die "Cannot open $filename: $!\en"; \& \& $db\->filter_fetch_key ( sub { $_ = unpack("i", $_) } ); \& $db\->filter_store_key ( sub { $_ = pack ("i", $_) } ); \& $hash{123} = "def"; \& # ... \& undef $db; \& untie %hash; .Ve .PP The code above uses DB_File, but again it will work with any of the DBM modules. .PP This time only two filters have been used; we only need to manipulate the contents of the key, so it wasn\*(Aqt necessary to install any value filters. .SH "SEE ALSO" .IX Header "SEE ALSO" DB_File, GDBM_File, NDBM_File, ODBM_File and SDBM_File. .SH AUTHOR .IX Header "AUTHOR" Paul Marquess