📄 perldbmfilter.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...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" '''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 (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" 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 "2007-12-18" "perl v5.10.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 \s-1DBM\s0modules that ship with Perl, namely DB_File, GDBM_File, NDBM_File,ODBM_File and SDBM_File..PPEach of the methods work identically, and are used to install (oruninstall) a single \s-1DBM\s0 Filter. The only difference between them is theplace that the filter is installed..PPTo summarise:.IP "\fBfilter_store_key\fR" 5.IX Item "filter_store_key"If a filter has been installed with this method, it will be invokedevery 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 invokedevery 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 invokedevery 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 invokedevery time you read a value from a \s-1DBM\s0 database..PPYou can use any combination of the methods from none to all four..PPAll filter methods return the existing filter, if present, or \f(CW\*(C`undef\*(C'\fRin not..PPTo delete a filter pass \f(CW\*(C`undef\*(C'\fR to it..Sh "The Filter".IX Subsection "The Filter"When each filter is called by Perl, a local copy of \f(CW$_\fR will containthe key or value to be filtered. Filtering is achieved by modifyingthe contents of \f(CW$_\fR. The return code from the filter is ignored..Sh "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\fRwant to make the same transformation to all keys, all values or both..PPFor example, consider the following scenario. You have a \s-1DBM\s0 databasethat you need to share with a third-party C application. The C applicationassumes that \fIall\fR keys and values are \s-1NULL\s0 terminated. Unfortunatelywhen Perl writes to \s-1DBM\s0 databases it doesn't use \s-1NULL\s0 termination, soyour Perl application will have to manage \s-1NULL\s0 termination itself. Whenyou write to the database you will have to use something like this:.PP.Vb 1\& $hash{"$key\e0"} = "$value\e0";.Ve.PPSimilarly the \s-1NULL\s0 needs to be taken into account when you are consideringthe length of existing keys/values..PPIt would be much better if you could ignore the \s-1NULL\s0 terminations issuein the main application code and have a mechanism that automaticallyadded the terminating \s-1NULL\s0 to all keys and values whenever you write tothe database and have them removed when you read from the database. As I'msure you have already guessed, this is a problem that \s-1DBM\s0 Filters canfix 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.PPThe code above uses SDBM_File, but it will work with any of the \s-1DBM\s0modules..PPHopefully the contents of each of the filters should beself-explanatory. Both \*(L"fetch\*(R" filters remove the terminating \s-1NULL\s0,and both \*(L"store\*(R" filters add a terminating \s-1NULL\s0..Sh "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 toa \s-1DBM\s0 database it always writes the key and value as strings. So whenyou use this:.PP.Vb 1\& $hash{12345} = "something";.Ve.PPthe 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 databaseas a C int, you will have to use \f(CW\*(C`pack\*(C'\fR when writing, and \f(CW\*(C`unpack\*(C'\fRwhen reading..PPHere 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.PPThe code above uses DB_File, but again it will work with any of the\&\s-1DBM\s0 modules..PPThis time only two filters have been used \*(-- we only need to manipulatethe contents of the key, so it wasn't necessary to install any valuefilters..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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -