📄 db_file.pm
字号:
my $filename = "text" ; unlink $filename ; my @h ; tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_RECNO or die "Cannot open file 'text': $!\n" ; # Add a few key/value pairs to the file $h[0] = "orange" ; $h[1] = "blue" ; $h[2] = "yellow" ; push @h, "green", "black" ; my $elements = scalar @h ; print "The array contains $elements entries\n" ; my $last = pop @h ; print "popped $last\n" ; unshift @h, "white" ; my $first = shift @h ; print "shifted $first\n" ; # Check for existence of a key print "Element 1 Exists with value $h[1]\n" if $h[1] ; # use a negative index print "The last element is $h[-1]\n" ; print "The 2nd last element is $h[-2]\n" ; untie @h ;Here is the output from the script: The array contains 5 entries popped black shifted white Element 1 Exists with value blue The last element is green The 2nd last element is yellow=head2 Extra RECNO MethodsIf you are using a version of Perl earlier than 5.004_57, the tiedarray interface is quite limited. In the example script aboveC<push>, C<pop>, C<shift>, C<unshift>or determining the array length will not work with a tied array.To make the interface more useful for older versions of Perl, a numberof methods are supplied with B<DB_File> to simulate the missing arrayoperations. All these methods are accessed via the object returned fromthe tie call.Here are the methods:=over 5=item B<$X-E<gt>push(list) ;>Pushes the elements of C<list> to the end of the array.=item B<$value = $X-E<gt>pop ;>Removes and returns the last element of the array.=item B<$X-E<gt>shift>Removes and returns the first element of the array.=item B<$X-E<gt>unshift(list) ;>Pushes the elements of C<list> to the start of the array.=item B<$X-E<gt>length>Returns the number of elements in the array.=item B<$X-E<gt>splice(offset, length, elements);>Returns a splice of the array.=back=head2 Another ExampleHere is a more complete example that makes use of some of the methodsdescribed above. It also makes use of the API interface directly (see L<THE API INTERFACE>). use warnings ; use strict ; my (@h, $H, $file, $i) ; use DB_File ; use Fcntl ; $file = "text" ; unlink $file ; $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0666, $DB_RECNO or die "Cannot open file $file: $!\n" ; # first create a text file to play with $h[0] = "zero" ; $h[1] = "one" ; $h[2] = "two" ; $h[3] = "three" ; $h[4] = "four" ; # Print the records in order. # # The length method is needed here because evaluating a tied # array in a scalar context does not return the number of # elements in the array. print "\nORIGINAL\n" ; foreach $i (0 .. $H->length - 1) { print "$i: $h[$i]\n" ; } # use the push & pop methods $a = $H->pop ; $H->push("last") ; print "\nThe last record was [$a]\n" ; # and the shift & unshift methods $a = $H->shift ; $H->unshift("first") ; print "The first record was [$a]\n" ; # Use the API to add a new record after record 2. $i = 2 ; $H->put($i, "Newbie", R_IAFTER) ; # and a new record before record 1. $i = 1 ; $H->put($i, "New One", R_IBEFORE) ; # delete record 3 $H->del(3) ; # now print the records in reverse order print "\nREVERSE\n" ; for ($i = $H->length - 1 ; $i >= 0 ; -- $i) { print "$i: $h[$i]\n" } # same again, but use the API functions instead print "\nREVERSE again\n" ; my ($s, $k, $v) = (0, 0, 0) ; for ($s = $H->seq($k, $v, R_LAST) ; $s == 0 ; $s = $H->seq($k, $v, R_PREV)) { print "$k: $v\n" } undef $H ; untie @h ;and this is what it outputs: ORIGINAL 0: zero 1: one 2: two 3: three 4: four The last record was [four] The first record was [zero] REVERSE 5: last 4: three 3: Newbie 2: one 1: New One 0: first REVERSE again 5: last 4: three 3: Newbie 2: one 1: New One 0: firstNotes:=over 5=item 1.Rather than iterating through the array, C<@h> like this: foreach $i (@h)it is necessary to use either this: foreach $i (0 .. $H->length - 1) or this: for ($a = $H->get($k, $v, R_FIRST) ; $a == 0 ; $a = $H->get($k, $v, R_NEXT) )=item 2.Notice that both times the C<put> method was used the record index wasspecified using a variable, C<$i>, rather than the literal valueitself. This is because C<put> will return the record number of theinserted line via that parameter.=back=head1 THE API INTERFACEAs well as accessing Berkeley DB using a tied hash or array, it is alsopossible to make direct use of most of the API functions defined in theBerkeley DB documentation.To do this you need to store a copy of the object returned from the tie. $db = tie %hash, "DB_File", "filename" ;Once you have done that, you can access the Berkeley DB API functionsas B<DB_File> methods directly like this: $db->put($key, $value, R_NOOVERWRITE) ;B<Important:> If you have saved a copy of the object returned fromC<tie>, the underlying database file will I<not> be closed until boththe tied variable is untied and all copies of the saved object aredestroyed. use DB_File ; $db = tie %hash, "DB_File", "filename" or die "Cannot tie filename: $!" ; ... undef $db ; untie %hash ;See L<The untie() Gotcha> for more details.All the functions defined in L<dbopen> are available except forclose() and dbopen() itself. The B<DB_File> method interface to thesupported functions have been implemented to mirror the way Berkeley DBworks whenever possible. In particular note that:=over 5=item *The methods return a status value. All return 0 on success.All return -1 to signify an error and set C<$!> to the exacterror code. The return code 1 generally (but not always) means that thekey specified did not exist in the database.Other return codes are defined. See below and in the Berkeley DBdocumentation for details. The Berkeley DB documentation should be usedas the definitive source.=item *Whenever a Berkeley DB function returns data via one of its parameters,the equivalent B<DB_File> method does exactly the same.=item *If you are careful, it is possible to mix API calls with the tiedhash/array interface in the same piece of code. Although only a few ofthe methods used to implement the tied interface currently make use ofthe cursor, you should always assume that the cursor has been changedany time the tied hash/array interface is used. As an example, thiscode will probably not do what you expect: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; # this line will modify the cursor $count = scalar keys %x ; # Get the second key/value pair. # oops, it didn't, it got the last key/value pair! $X->seq($key, $value, R_NEXT) ;The code above can be rearranged to get around the problem, like this: $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE or die "Cannot tie $filename: $!" ; # this line will modify the cursor $count = scalar keys %x ; # Get the first key/value pair and set the cursor $X->seq($key, $value, R_FIRST) ; # Get the second key/value pair. # worked this time. $X->seq($key, $value, R_NEXT) ;=backAll the constants defined in L<dbopen> for use in the flags parametersin the methods defined below are also available. Refer to the BerkeleyDB documentation for the precise meaning of the flags values.Below is a list of the methods available.=over 5=item B<$status = $X-E<gt>get($key, $value [, $flags]) ;>Given a key (C<$key>) this method reads the value associated with itfrom the database. The value read from the database is returned in theC<$value> parameter.If the key does not exist the method returns 1.No flags are currently defined for this method.=item B<$status = $X-E<gt>put($key, $value [, $flags]) ;>Stores the key/value pair in the database.If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameterwill have the record number of the inserted key/value pair set.Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE andR_SETCURSOR.=item B<$status = $X-E<gt>del($key [, $flags]) ;>Removes all key/value pairs with key C<$key> from the database.A return code of 1 means that the requested key was not in thedatabase.R_CURSOR is the only valid flag at present.=item B<$status = $X-E<gt>fd ;>Returns the file descriptor for the underlying database.See L<Locking: The Trouble with fd> for an explanation for why you shouldnot use C<fd> to lock your database.=item B<$status = $X-E<gt>seq($key, $value, $flags) ;>This interface allows sequential retrieval from the database. SeeL<dbopen> for full details.Both the C<$key> and C<$value> parameters will be set to the key/valuepair read from the database.The flags parameter is mandatory. The valid flag values are R_CURSOR,R_FIRST, R_LAST, R_NEXT and R_PREV.=item B<$status = $X-E<gt>sync([$flags]) ;>Flushes any cached buffers to disk.R_RECNOSYNC is the only valid flag at present.=back=head1 DBM FILTERSA DBM Filter is a piece of code that is be used when you I<always>want to make the same transformation to all keys and/or values in aDBM database.There are four methods associated with DBM Filters. All work identically,and each is used to install (or uninstall) a single DBM Filter. Eachexpects a single parameter, namely a reference to a sub. The onlydifference between them is the place that the filter is installed.To summarise:=over 5=item B<filter_store_key>If a filter has been installed with this method, it will be invokedevery time you write a key to a DBM database.=item B<filter_store_value>If a filter has been installed with this method, it will be invokedevery time you write a value to a DBM database.=item B<filter_fetch_key>If a filter has been installed with this method, it will be invokedevery time you read a key from a DBM database.=item B<filter_fetch_value>If a filter has been installed with this method, it will be invokedevery time you read a value from a DBM database.=backYou can use any combination of the methods, from none, to all four.All filter methods return the existing filter, if present, or C<undef>in not.To delete a filter pass C<undef> to it.=head2 The FilterWhen each filter is called by Perl, a local copy of C<$_> will containthe key or value to be filtered. Filtering is achieved by modifyingthe contents of C<$_>. The return code from the filter is ignored.=head2 An Example -- the NULL termination problem.Consider the following scenario. You have a DBM databasethat you need to share with a third-party C application. The C applicationassumes that I<all> keys and values are NULL terminated. Unfortunatelywhen Perl writes to DBM databases it doesn't use NULL termination, soyour Perl application will have to manage NULL termination itself. Whenyou 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 consideringthe length of existing keys/values.It would be much better if you could ignore the NULL terminations issuein the main application code and have a mechanism that automaticallyadded the terminating NULL 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 DBM Filters canfix very easily. use warnings ; use strict ; 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" ; # Install DBM Filters $db->filter_fetch_key ( sub { s/\0$// } ) ; $db->filter_store_key ( sub { $_ .= "\0" } ) ; $db->filter_fetch_value( sub { s/\0$// } ) ; $db->filter_store_value( sub { $_ .= "\0" } ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -