Current Path : /usr/opt/perl530/man/man3/ |
FreeBSD hs32.drive.ne.jp 9.1-RELEASE FreeBSD 9.1-RELEASE #1: Wed Jan 14 12:18:08 JST 2015 root@hs32.drive.ne.jp:/sys/amd64/compile/hs32 amd64 |
Current File : //usr/opt/perl530/man/man3/DBM_Filter.3 |
.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" 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 >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 .\" .\" 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 "DBM_Filter 3" .TH DBM_Filter 3 "2019-10-24" "perl v5.30.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" DBM_Filter \-\- Filter DBM keys/values .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use DBM_Filter ; \& use SDBM_File; # or DB_File, GDBM_File, NDBM_File, or ODBM_File \& \& $db = tie %hash, ... \& \& $db\->Filter_Push(Fetch => sub {...}, \& Store => sub {...}); \& \& $db\->Filter_Push(\*(Aqmy_filter1\*(Aq); \& $db\->Filter_Push(\*(Aqmy_filter2\*(Aq, params...); \& \& $db\->Filter_Key_Push(...) ; \& $db\->Filter_Value_Push(...) ; \& \& $db\->Filter_Pop(); \& $db\->Filtered(); \& \& package DBM_Filter::my_filter1; \& \& sub Store { ... } \& sub Fetch { ... } \& \& 1; \& \& package DBM_Filter::my_filter2; \& \& sub Filter \& { \& my @opts = @_; \& ... \& return ( \& sub Store { ... }, \& sub Fetch { ... } ); \& } \& \& 1; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides an interface that allows filters to be applied to tied Hashes associated with \s-1DBM\s0 files. It builds on the \s-1DBM\s0 Filter hooks that are present in all the *DB*_File modules included with the standard Perl source distribution from version 5.6.1 onwards. In addition to the *DB*_File modules distributed with Perl, the BerkeleyDB module, available on \s-1CPAN,\s0 supports the \s-1DBM\s0 Filter hooks. See perldbmfilter for more details on the \s-1DBM\s0 Filter hooks. .SH "What is a DBM Filter?" .IX Header "What is a DBM Filter?" A \s-1DBM\s0 Filter allows the keys and/or values in a tied hash to be modified by some user-defined code just before it is written to the \s-1DBM\s0 file and just after it is read back from the \s-1DBM\s0 file. For example, this snippet of code .PP .Vb 1 \& $some_hash{"abc"} = 42; .Ve .PP could potentially trigger two filters, one for the writing of the key \&\*(L"abc\*(R" and another for writing the value 42. Similarly, this snippet .PP .Vb 1 \& my ($key, $value) = each %some_hash .Ve .PP will trigger two filters, one for the reading of the key and one for the reading of the value. .PP Like the existing \s-1DBM\s0 Filter functionality, this module arranges for the \&\f(CW$_\fR variable to be populated with the key or value that a filter will check. This usually means that most \s-1DBM\s0 filters tend to be very short. .SS "So what's new?" .IX Subsection "So what's new?" The main enhancements over the standard \s-1DBM\s0 Filter hooks are: .IP "\(bu" 4 A cleaner interface. .IP "\(bu" 4 The ability to easily apply multiple filters to a single \s-1DBM\s0 file. .IP "\(bu" 4 The ability to create \*(L"canned\*(R" filters. These allow commonly used filters to be packaged into a stand-alone module. .SH "METHODS" .IX Header "METHODS" This module will arrange for the following methods to be available via the object returned from the \f(CW\*(C`tie\*(C'\fR call. .ie n .SS "$db\->\fBFilter_Push()\fP / $db\->\fBFilter_Key_Push()\fP / $db\->\fBFilter_Value_Push()\fP" .el .SS "\f(CW$db\fP\->\fBFilter_Push()\fP / \f(CW$db\fP\->\fBFilter_Key_Push()\fP / \f(CW$db\fP\->\fBFilter_Value_Push()\fP" .IX Subsection "$db->Filter_Push() / $db->Filter_Key_Push() / $db->Filter_Value_Push()" Add a filter to filter stack for the database, \f(CW$db\fR. The three formats vary only in whether they apply to the \s-1DBM\s0 key, the \s-1DBM\s0 value or both. .IP "Filter_Push" 5 .IX Item "Filter_Push" The filter is applied to \fIboth\fR keys and values. .IP "Filter_Key_Push" 5 .IX Item "Filter_Key_Push" The filter is applied to the key \fIonly\fR. .IP "Filter_Value_Push" 5 .IX Item "Filter_Value_Push" The filter is applied to the value \fIonly\fR. .ie n .SS "$db\->\fBFilter_Pop()\fP" .el .SS "\f(CW$db\fP\->\fBFilter_Pop()\fP" .IX Subsection "$db->Filter_Pop()" Removes the last filter that was applied to the \s-1DBM\s0 file associated with \&\f(CW$db\fR, if present. .ie n .SS "$db\->\fBFiltered()\fP" .el .SS "\f(CW$db\fP\->\fBFiltered()\fP" .IX Subsection "$db->Filtered()" Returns \s-1TRUE\s0 if there are any filters applied to the \s-1DBM\s0 associated with \f(CW$db\fR. Otherwise returns \s-1FALSE.\s0 .SH "Writing a Filter" .IX Header "Writing a Filter" Filters can be created in two main ways .SS "Immediate Filters" .IX Subsection "Immediate Filters" An immediate filter allows you to specify the filter code to be used at the point where the filter is applied to a dbm. In this mode the Filter_*_Push methods expects to receive exactly two parameters. .PP .Vb 3 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ... \& $db\->Filter_Push( Store => sub { }, \& Fetch => sub { }); .Ve .PP The code reference associated with \f(CW\*(C`Store\*(C'\fR will be called before any key/value is written to the database and the code reference associated with \f(CW\*(C`Fetch\*(C'\fR will be called after any key/value is read from the database. .PP For example, here is a sample filter that adds a trailing \s-1NULL\s0 character to all strings before they are written to the \s-1DBM\s0 file, and removes the trailing \s-1NULL\s0 when they are read from the \s-1DBM\s0 file .PP .Vb 3 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ... \& $db\->Filter_Push( Store => sub { $_ .= "\ex00" ; }, \& Fetch => sub { s/\ex00$// ; }); .Ve .PP Points to note: .IP "1." 5 Both the Store and Fetch filters manipulate \f(CW$_\fR. .SS "Canned Filters" .IX Subsection "Canned Filters" Immediate filters are useful for one-off situations. For more generic problems it can be useful to package the filter up in its own module. .PP The usage is for a canned filter is: .PP .Vb 1 \& $db\->Filter_Push("name", params) .Ve .PP where .ie n .IP """name""" 5 .el .IP "``name''" 5 .IX Item "name" is the name of the module to load. If the string specified does not contain the package separator characters \*(L"::\*(R", it is assumed to refer to the full module name \*(L"DBM_Filter::name\*(R". This means that the full names for canned filters, \*(L"null\*(R" and \*(L"utf8\*(R", included with this module are: .Sp .Vb 2 \& DBM_Filter::null \& DBM_Filter::utf8 .Ve .IP "params" 5 .IX Item "params" any optional parameters that need to be sent to the filter. See the encode filter for an example of a module that uses parameters. .PP The module that implements the canned filter can take one of two forms. Here is a template for the first .PP .Vb 1 \& package DBM_Filter::null ; \& \& use strict; \& use warnings; \& \& sub Store \& { \& # store code here \& } \& \& sub Fetch \& { \& # fetch code here \& } \& \& 1; .Ve .PP Notes: .IP "1." 5 The package name uses the \f(CW\*(C`DBM_Filter::\*(C'\fR prefix. .IP "2." 5 The module \fImust\fR have both a Store and a Fetch method. If only one is present, or neither are present, a fatal error will be thrown. .PP The second form allows the filter to hold state information using a closure, thus: .PP .Vb 1 \& package DBM_Filter::encoding ; \& \& use strict; \& use warnings; \& \& sub Filter \& { \& my @params = @_ ; \& \& ... \& return { \& Store => sub { $_ = $encoding\->encode($_) }, \& Fetch => sub { $_ = $encoding\->decode($_) } \& } ; \& } \& \& 1; .Ve .PP In this instance the \*(L"Store\*(R" and \*(L"Fetch\*(R" methods are encapsulated inside a \&\*(L"Filter\*(R" method. .SH "Filters Included" .IX Header "Filters Included" A number of canned filers are provided with this module. They cover a number of the main areas that filters are needed when interfacing with \&\s-1DBM\s0 files. They also act as templates for your own filters. .PP The filter included are: .IP "\(bu" 5 utf8 .Sp This module will ensure that all data written to the \s-1DBM\s0 will be encoded in \s-1UTF\-8.\s0 .Sp This module needs the Encode module. .IP "\(bu" 5 encode .Sp Allows you to choose the character encoding will be store in the \s-1DBM\s0 file. .IP "\(bu" 5 compress .Sp This filter will compress all data before it is written to the database and uncompressed it on reading. .Sp This module needs Compress::Zlib. .IP "\(bu" 5 int32 .Sp This module is used when interoperating with a C/\*(C+ application that uses a C int as either the key and/or value in the \s-1DBM\s0 file. .IP "\(bu" 5 null .Sp This module ensures that all data written to the \s-1DBM\s0 file is null terminated. This is useful when you have a perl script that needs to interoperate with a \s-1DBM\s0 file that a C program also uses. A fairly common issue is for the C application to include the terminating null in a string when it writes to the \s-1DBM\s0 file. This filter will ensure that all data written to the \s-1DBM\s0 file can be read by the C application. .SH "NOTES" .IX Header "NOTES" .SS "Maintain Round Trip Integrity" .IX Subsection "Maintain Round Trip Integrity" When writing a \s-1DBM\s0 filter it is \fIvery\fR important to ensure that it is possible to retrieve all data that you have written when the \s-1DBM\s0 filter is in place. In practice, this means that whatever transformation is applied to the data in the Store method, the \fIexact\fR inverse operation should be applied in the Fetch method. .PP If you don't provide an exact inverse transformation, you will find that code like this will not behave as you expect. .PP .Vb 4 \& while (my ($k, $v) = each %hash) \& { \& ... \& } .Ve .PP Depending on the transformation, you will find that one or more of the following will happen .IP "1." 5 The loop will never terminate. .IP "2." 5 Too few records will be retrieved. .IP "3." 5 Too many will be retrieved. .IP "4." 5 The loop will do the right thing for a while, but it will unexpectedly fail. .SS "Don't mix filtered & non-filtered data in the same database file." .IX Subsection "Don't mix filtered & non-filtered data in the same database file." This is just a restatement of the previous section. Unless you are completely certain you know what you are doing, avoid mixing filtered & non-filtered data. .SH "EXAMPLE" .IX Header "EXAMPLE" Say you need to interoperate with a legacy C application that stores keys as C ints and the values and null terminated \s-1UTF\-8\s0 strings. Here is how you would set that up .PP .Vb 1 \& my $db = tie %hash, \*(AqSDBM_File\*(Aq, ... \& \& $db\->Filter_Key_Push(\*(Aqint32\*(Aq) ; \& \& $db\->Filter_Value_Push(\*(Aqutf8\*(Aq); \& $db\->Filter_Value_Push(\*(Aqnull\*(Aq); .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" <DB_File>, GDBM_File, NDBM_File, ODBM_File, SDBM_File, perldbmfilter .SH "AUTHOR" .IX Header "AUTHOR" Paul Marquess <pmqs@cpan.org>