.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 turned on, 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "PERLDBMFILTER 1" .TH PERLDBMFILTER 1 "2014-12-27" "perl v5.20.2" "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 \s-1DBM\s0 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 \s-1DBM\s0 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 \s-1DBM\s0 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 \s-1DBM\s0 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 \s-1DBM\s0 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 \s-1DBM\s0 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 \s-1NULL\s0 termination problem." .IX Subsection "An Example: the NULL termination problem." \&\s-1DBM\s0 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 \s-1DBM\s0 database that you need to share with a third-party C application. The C application assumes that \fIall\fR keys and values are \s-1NULL\s0 terminated. Unfortunately when Perl writes to \s-1DBM\s0 databases it doesn't use \s-1NULL\s0 termination, so your Perl application will have to manage \s-1NULL\s0 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 \s-1NULL\s0 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 \s-1NULL\s0 terminations issue in the main application code and have a mechanism that automatically added the terminating \s-1NULL\s0 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 \s-1DBM\s0 Filters can fix very easily. .PP .Vb 4 \& use strict; \& use warnings; \& 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 $a = $hash{"ABC"}; \& # ... \& undef $db; \& untie %hash; .Ve .PP The code above uses SDBM_File, but it will work with any of the \s-1DBM\s0 modules. .PP Hopefully the contents of each of the filters should be self-explanatory. Both \*(L"fetch\*(R" filters remove the terminating \s-1NULL,\s0 and both \*(L"store\*(R" filters add a terminating \s-1NULL.\s0 .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 \s-1DBM\s0 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 \s-1DBM\s0 database as the 5 byte string \&\*(L"12345\*(R". If you actually want the key to be stored in the \s-1DBM\s0 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 \s-1DBM\s0 Filter that does it: .PP .Vb 6 \& use strict; \& use warnings; \& 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 \&\s-1DBM\s0 modules. .PP 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. .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