1 # DB_File.pm -- Perl 5 interface to Berkeley DB
3 # written by Paul Marquess (Paul.Marquess@btinternet.com)
4 # last modified 16th January 2000
7 # Copyright (c) 1995-2000 Paul Marquess. All rights reserved.
8 # This program is free software; you can redistribute it and/or
9 # modify it under the same terms as Perl itself.
12 package DB_File::HASHINFO ;
19 @DB_File::HASHINFO::ISA = qw(Tie::Hash);
34 bless { VALID => { map {$_, 1}
35 qw( bsize ffactor nelem cachesize hash lorder)
47 return $self->{GOT}{$key} if exists $self->{VALID}{$key} ;
50 croak "${pkg}::FETCH - Unknown element '$key'" ;
60 if ( exists $self->{VALID}{$key} )
62 $self->{GOT}{$key} = $value ;
67 croak "${pkg}::STORE - Unknown element '$key'" ;
75 if ( exists $self->{VALID}{$key} )
77 delete $self->{GOT}{$key} ;
82 croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ;
90 exists $self->{VALID}{$key} ;
98 croak ref($self) . " does not define the method ${method}" ;
101 sub FIRSTKEY { my $self = shift ; $self->NotHere("FIRSTKEY") }
102 sub NEXTKEY { my $self = shift ; $self->NotHere("NEXTKEY") }
103 sub CLEAR { my $self = shift ; $self->NotHere("CLEAR") }
105 package DB_File::RECNOINFO ;
109 @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ;
115 bless { VALID => { map {$_, 1}
116 qw( bval cachesize psize flags lorder reclen bfname )
122 package DB_File::BTREEINFO ;
126 @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ;
132 bless { VALID => { map {$_, 1}
133 qw( flags cachesize maxkeypage minkeypage psize
134 compare prefix lorder )
144 use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO
145 $db_version $use_XSLoader
152 #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
153 $DB_BTREE = new DB_File::BTREEINFO ;
154 $DB_HASH = new DB_File::HASHINFO ;
155 $DB_RECNO = new DB_File::RECNOINFO ;
162 eval { require XSLoader } ;
167 @ISA = qw(DynaLoader);
171 push @ISA, qw(Tie::Hash Exporter);
173 $DB_BTREE $DB_HASH $DB_RECNO
208 ($constname = $AUTOLOAD) =~ s/.*:://;
209 my $val = constant($constname, @_ ? $_[0] : 0);
211 if ($! =~ /Invalid/ || $!{EINVAL}) {
212 $AutoLoader::AUTOLOAD = $AUTOLOAD;
213 goto &AutoLoader::AUTOLOAD;
216 my($pack,$file,$line) = caller;
217 croak "Your vendor has not defined DB macro $constname, used at $file line $line.
221 eval "sub $AUTOLOAD { $val }";
227 # Make all Fcntl O_XXX constants available for importing
229 my @O = grep /^O_/, @Fcntl::EXPORT;
230 Fcntl->import(@O); # first we import what we want to export
235 { XSLoader::load("DB_File", $VERSION)}
237 { bootstrap DB_File $VERSION }
239 # Preloaded methods go here. Autoload methods go after __END__, and are
240 # processed by the autosplit program.
242 sub tie_hash_or_array
245 my $tieHASH = ( (caller(1))[3] =~ /TIEHASH/ ) ;
247 $arg[4] = tied %{ $arg[4] }
248 if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ;
250 # make recno in Berkeley DB version 2 work like recno in version 1.
251 if ($db_version > 1 and defined $arg[4] and $arg[4] =~ /RECNO/ and
252 $arg[1] and ! -e $arg[1]) {
253 open(FH, ">$arg[1]") or return undef ;
255 chmod $arg[3] ? $arg[3] : 0666 , $arg[1] ;
258 DoTie_($tieHASH, @arg) ;
263 tie_hash_or_array(@_) ;
268 tie_hash_or_array(@_) ;
276 my $status = $self->seq($key, $value, R_FIRST());
279 while ($status == 0) {
281 $status = $self->seq($key, $value, R_NEXT());
283 foreach $key (reverse @keys) {
284 my $s = $self->del($key);
294 my $current_length = $self->length() ;
296 if ($length < $current_length) {
298 for ($key = $current_length - 1 ; $key >= $length ; -- $key)
301 elsif ($length > $current_length) {
302 $self->put($length-1, "") ;
308 croak "Usage: \$db->find_dup(key,value)\n"
312 my ($origkey, $value_wanted) = @_ ;
313 my ($key, $value) = ($origkey, 0);
316 for ($status = $db->seq($key, $value, R_CURSOR() ) ;
318 $status = $db->seq($key, $value, R_NEXT() ) ) {
320 return 0 if $key eq $origkey and $value eq $value_wanted ;
328 croak "Usage: \$db->del_dup(key,value)\n"
332 my ($key, $value) = @_ ;
333 my ($status) = $db->find_dup($key, $value) ;
334 return $status if $status != 0 ;
336 $status = $db->del($key, R_CURSOR() ) ;
342 croak "Usage: \$db->get_dup(key [,flag])\n"
343 unless @_ == 2 or @_ == 3 ;
350 my $wantarray = wantarray ;
356 # iterate through the database until either EOF ($status == 0)
357 # or a different key is encountered ($key ne $origkey).
358 for ($status = $db->seq($key, $value, R_CURSOR()) ;
359 $status == 0 and $key eq $origkey ;
360 $status = $db->seq($key, $value, R_NEXT()) ) {
362 # save the value or count number of matches
365 { ++ $values{$value} }
367 { push (@values, $value) }
374 return ($wantarray ? ($flag ? %values : @values) : $counter) ;
383 DB_File - Perl5 access to Berkeley DB version 1.x
389 [$X =] tie %hash, 'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
390 [$X =] tie %hash, 'DB_File', $filename, $flags, $mode, $DB_BTREE ;
391 [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
393 $status = $X->del($key [, $flags]) ;
394 $status = $X->put($key, $value [, $flags]) ;
395 $status = $X->get($key, $value [, $flags]) ;
396 $status = $X->seq($key, $value, $flags) ;
397 $status = $X->sync([$flags]) ;
401 $count = $X->get_dup($key) ;
402 @list = $X->get_dup($key) ;
403 %list = $X->get_dup($key, 1) ;
404 $status = $X->find_dup($key, $value) ;
405 $status = $X->del_dup($key, $value) ;
415 $old_filter = $db->filter_store_key ( sub { ... } ) ;
416 $old_filter = $db->filter_store_value( sub { ... } ) ;
417 $old_filter = $db->filter_fetch_key ( sub { ... } ) ;
418 $old_filter = $db->filter_fetch_value( sub { ... } ) ;
425 B<DB_File> is a module which allows Perl programs to make use of the
426 facilities provided by Berkeley DB version 1.x (if you have a newer
427 version of DB, see L<Using DB_File with Berkeley DB version 2 or 3>).
428 It is assumed that you have a copy of the Berkeley DB manual pages at
429 hand when reading this documentation. The interface defined here
430 mirrors the Berkeley DB interface closely.
432 Berkeley DB is a C library which provides a consistent interface to a
433 number of database formats. B<DB_File> provides an interface to all
434 three of the database types currently supported by Berkeley DB.
442 This database type allows arbitrary key/value pairs to be stored in data
443 files. This is equivalent to the functionality provided by other
444 hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
445 the files created using DB_HASH are not compatible with any of the
446 other packages mentioned.
448 A default hashing algorithm, which will be adequate for most
449 applications, is built into Berkeley DB. If you do need to use your own
450 hashing algorithm it is possible to write your own in Perl and have
451 B<DB_File> use it instead.
455 The btree format allows arbitrary key/value pairs to be stored in a
456 sorted, balanced binary tree.
458 As with the DB_HASH format, it is possible to provide a user defined
459 Perl routine to perform the comparison of keys. By default, though, the
460 keys are stored in lexical order.
464 DB_RECNO allows both fixed-length and variable-length flat text files
465 to be manipulated using the same key/value pair interface as in DB_HASH
466 and DB_BTREE. In this case the key will consist of a record (line)
471 =head2 Using DB_File with Berkeley DB version 2 or 3
473 Although B<DB_File> is intended to be used with Berkeley DB version 1,
474 it can also be used with version 2.or 3 In this case the interface is
475 limited to the functionality provided by Berkeley DB 1.x. Anywhere the
476 version 2 or 3 interface differs, B<DB_File> arranges for it to work
477 like version 1. This feature allows B<DB_File> scripts that were built
478 with version 1 to be migrated to version 2 or 3 without any changes.
480 If you want to make use of the new features available in Berkeley DB
481 2.x or greater, use the Perl module B<BerkeleyDB> instead.
483 B<Note:> The database file format has changed in both Berkeley DB
484 version 2 and 3. If you cannot recreate your databases, you must dump
485 any existing databases with the C<db_dump185> utility that comes with
487 Once you have rebuilt DB_File to use Berkeley DB version 2 or 3, your
488 databases can be recreated using C<db_load>. Refer to the Berkeley DB
489 documentation for further details.
491 Please read L<"COPYRIGHT"> before using version 2.x or 3.x of Berkeley
494 =head2 Interface to Berkeley DB
496 B<DB_File> allows access to Berkeley DB files using the tie() mechanism
497 in Perl 5 (for full details, see L<perlfunc/tie()>). This facility
498 allows B<DB_File> to access Berkeley DB files using either an
499 associative array (for DB_HASH & DB_BTREE file types) or an ordinary
500 array (for the DB_RECNO file type).
502 In addition to the tie() interface, it is also possible to access most
503 of the functions provided in the Berkeley DB API directly.
504 See L<THE API INTERFACE>.
506 =head2 Opening a Berkeley DB Database File
508 Berkeley DB uses the function dbopen() to open or create a database.
509 Here is the C prototype for dbopen():
512 dbopen (const char * file, int flags, int mode,
513 DBTYPE type, const void * openinfo)
515 The parameter C<type> is an enumeration which specifies which of the 3
516 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
517 Depending on which of these is actually chosen, the final parameter,
518 I<openinfo> points to a data structure which allows tailoring of the
519 specific interface method.
521 This interface is handled slightly differently in B<DB_File>. Here is
522 an equivalent call using B<DB_File>:
524 tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ;
526 The C<filename>, C<flags> and C<mode> parameters are the direct
527 equivalent of their dbopen() counterparts. The final parameter $DB_HASH
528 performs the function of both the C<type> and C<openinfo> parameters in
531 In the example above $DB_HASH is actually a pre-defined reference to a
532 hash object. B<DB_File> has three of these pre-defined references.
533 Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
535 The keys allowed in each of these pre-defined references is limited to
536 the names used in the equivalent C structure. So, for example, the
537 $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>,
538 C<ffactor>, C<hash>, C<lorder> and C<nelem>.
540 To change one of these elements, just assign to it like this:
542 $DB_HASH->{'cachesize'} = 10000 ;
544 The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are
545 usually adequate for most applications. If you do need to create extra
546 instances of these objects, constructors are available for each file
549 Here are examples of the constructors and the valid options available
550 for DB_HASH, DB_BTREE and DB_RECNO respectively.
552 $a = new DB_File::HASHINFO ;
560 $b = new DB_File::BTREEINFO ;
570 $c = new DB_File::RECNOINFO ;
579 The values stored in the hashes above are mostly the direct equivalent
580 of their C counterpart. Like their C counterparts, all are set to a
581 default values - that means you don't have to set I<all> of the
582 values when you only want to change one. Here is an example:
584 $a = new DB_File::HASHINFO ;
585 $a->{'cachesize'} = 12345 ;
586 tie %y, 'DB_File', "filename", $flags, 0777, $a ;
588 A few of the options need extra discussion here. When used, the C
589 equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers
590 to C functions. In B<DB_File> these keys are used to store references
591 to Perl subs. Below are templates for each of the subs:
597 # return the hash value for $data
603 my ($key, $key2) = @_ ;
605 # return 0 if $key1 eq $key2
606 # -1 if $key1 lt $key2
607 # 1 if $key1 gt $key2
608 return (-1 , 0 or 1) ;
613 my ($key, $key2) = @_ ;
615 # return number of bytes of $key2 which are
616 # necessary to determine that it is greater than $key1
620 See L<Changing the BTREE sort order> for an example of using the
623 If you are using the DB_RECNO interface and you intend making use of
624 C<bval>, you should check out L<The 'bval' Option>.
626 =head2 Default Parameters
628 It is possible to omit some or all of the final 4 parameters in the
629 call to C<tie> and let them take default values. As DB_HASH is the most
630 common file format used, the call:
632 tie %A, "DB_File", "filename" ;
636 tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;
638 It is also possible to omit the filename parameter as well, so the
645 tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;
647 See L<In Memory Databases> for a discussion on the use of C<undef>
648 in place of a filename.
650 =head2 In Memory Databases
652 Berkeley DB allows the creation of in-memory databases by using NULL
653 (that is, a C<(char *)0> in C) in place of the filename. B<DB_File>
654 uses C<undef> instead of NULL to provide this functionality.
658 The DB_HASH file format is probably the most commonly used of the three
659 file formats that B<DB_File> supports. It is also very straightforward
662 =head2 A Simple Example
664 This example shows how to create a database, add key/value pairs to the
665 database, delete keys/value pairs and finally how to enumerate the
666 contents of the database.
670 use vars qw( %h $k $v ) ;
673 tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
674 or die "Cannot open file 'fruit': $!\n";
676 # Add a few key/value pairs to the file
677 $h{"apple"} = "red" ;
678 $h{"orange"} = "orange" ;
679 $h{"banana"} = "yellow" ;
680 $h{"tomato"} = "red" ;
682 # Check for existence of a key
683 print "Banana Exists\n\n" if $h{"banana"} ;
685 # Delete a key/value pair.
688 # print the contents of the file
689 while (($k, $v) = each %h)
690 { print "$k -> $v\n" }
702 Note that the like ordinary associative arrays, the order of the keys
703 retrieved is in an apparently random order.
707 The DB_BTREE format is useful when you want to store data in a given
708 order. By default the keys will be stored in lexical order, but as you
709 will see from the example shown in the next section, it is very easy to
710 define your own sorting function.
712 =head2 Changing the BTREE sort order
714 This script shows how to override the default sorting algorithm that
715 BTREE uses. Instead of using the normal lexical ordering, a case
716 insensitive compare function will be used.
725 my ($key1, $key2) = @_ ;
726 "\L$key1" cmp "\L$key2" ;
729 # specify the Perl sub that will do the comparison
730 $DB_BTREE->{'compare'} = \&Compare ;
733 tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE
734 or die "Cannot open file 'tree': $!\n" ;
736 # Add a key/value pair to the file
737 $h{'Wall'} = 'Larry' ;
738 $h{'Smith'} = 'John' ;
739 $h{'mouse'} = 'mickey' ;
740 $h{'duck'} = 'donald' ;
745 # Cycle through the keys printing them in order.
746 # Note it is not necessary to sort the keys as
747 # the btree will have kept them in order automatically.
753 Here is the output from the code above.
759 There are a few point to bear in mind if you want to change the
760 ordering in a BTREE database:
766 The new compare function must be specified when you create the database.
770 You cannot change the ordering once the database has been created. Thus
771 you must use the same compare function every time you access the
776 =head2 Handling Duplicate Keys
778 The BTREE file type optionally allows a single key to be associated
779 with an arbitrary number of values. This option is enabled by setting
780 the flags element of C<$DB_BTREE> to R_DUP when creating the database.
782 There are some difficulties in using the tied hash interface if you
783 want to manipulate a BTREE database with duplicate keys. Consider this
789 use vars qw($filename %h ) ;
794 # Enable duplicate records
795 $DB_BTREE->{'flags'} = R_DUP ;
797 tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
798 or die "Cannot open $filename: $!\n";
800 # Add some key/value pairs to the file
801 $h{'Wall'} = 'Larry' ;
802 $h{'Wall'} = 'Brick' ; # Note the duplicate key
803 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
804 $h{'Smith'} = 'John' ;
805 $h{'mouse'} = 'mickey' ;
807 # iterate through the associative array
808 # and print each key/value pair.
809 foreach (sort keys %h)
810 { print "$_ -> $h{$_}\n" }
822 As you can see 3 records have been successfully created with key C<Wall>
823 - the only thing is, when they are retrieved from the database they
824 I<seem> to have the same value, namely C<Larry>. The problem is caused
825 by the way that the associative array interface works. Basically, when
826 the associative array interface is used to fetch the value associated
827 with a given key, it will only ever retrieve the first value.
829 Although it may not be immediately obvious from the code above, the
830 associative array interface can be used to write values with duplicate
831 keys, but it cannot be used to read them back from the database.
833 The way to get around this problem is to use the Berkeley DB API method
834 called C<seq>. This method allows sequential access to key/value
835 pairs. See L<THE API INTERFACE> for details of both the C<seq> method
836 and the API in general.
838 Here is the script above rewritten using the C<seq> API method.
843 use vars qw($filename $x %h $status $key $value) ;
848 # Enable duplicate records
849 $DB_BTREE->{'flags'} = R_DUP ;
851 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
852 or die "Cannot open $filename: $!\n";
854 # Add some key/value pairs to the file
855 $h{'Wall'} = 'Larry' ;
856 $h{'Wall'} = 'Brick' ; # Note the duplicate key
857 $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
858 $h{'Smith'} = 'John' ;
859 $h{'mouse'} = 'mickey' ;
861 # iterate through the btree using seq
862 # and print each key/value pair.
864 for ($status = $x->seq($key, $value, R_FIRST) ;
866 $status = $x->seq($key, $value, R_NEXT) )
867 { print "$key -> $value\n" }
880 This time we have got all the key/value pairs, including the multiple
881 values associated with the key C<Wall>.
883 To make life easier when dealing with duplicate keys, B<DB_File> comes with
884 a few utility methods.
886 =head2 The get_dup() Method
888 The C<get_dup> method assists in
889 reading duplicate values from BTREE databases. The method can take the
892 $count = $x->get_dup($key) ;
893 @list = $x->get_dup($key) ;
894 %list = $x->get_dup($key, 1) ;
896 In a scalar context the method returns the number of values associated
897 with the key, C<$key>.
899 In list context, it returns all the values which match C<$key>. Note
900 that the values will be returned in an apparently random order.
902 In list context, if the second parameter is present and evaluates
903 TRUE, the method returns an associative array. The keys of the
904 associative array correspond to the values that matched in the BTREE
905 and the values of the array are a count of the number of times that
906 particular value occurred in the BTREE.
908 So assuming the database created above, we can use C<get_dup> like
914 use vars qw($filename $x %h ) ;
918 # Enable duplicate records
919 $DB_BTREE->{'flags'} = R_DUP ;
921 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
922 or die "Cannot open $filename: $!\n";
924 my $cnt = $x->get_dup("Wall") ;
925 print "Wall occurred $cnt times\n" ;
927 my %hash = $x->get_dup("Wall", 1) ;
928 print "Larry is there\n" if $hash{'Larry'} ;
929 print "There are $hash{'Brick'} Brick Walls\n" ;
931 my @list = sort $x->get_dup("Wall") ;
932 print "Wall => [@list]\n" ;
934 @list = $x->get_dup("Smith") ;
935 print "Smith => [@list]\n" ;
937 @list = $x->get_dup("Dog") ;
938 print "Dog => [@list]\n" ;
943 Wall occurred 3 times
945 There are 2 Brick Walls
946 Wall => [Brick Brick Larry]
950 =head2 The find_dup() Method
952 $status = $X->find_dup($key, $value) ;
954 This method checks for the existence of a specific key/value pair. If the
955 pair exists, the cursor is left pointing to the pair and the method
956 returns 0. Otherwise the method returns a non-zero value.
958 Assuming the database from the previous example:
963 use vars qw($filename $x %h $found) ;
965 my $filename = "tree" ;
967 # Enable duplicate records
968 $DB_BTREE->{'flags'} = R_DUP ;
970 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
971 or die "Cannot open $filename: $!\n";
973 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
974 print "Larry Wall is $found there\n" ;
976 $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
977 print "Harry Wall is $found there\n" ;
985 Harry Wall is not there
988 =head2 The del_dup() Method
990 $status = $X->del_dup($key, $value) ;
992 This method deletes a specific key/value pair. It returns
993 0 if they exist and have been deleted successfully.
994 Otherwise the method returns a non-zero value.
996 Again assuming the existence of the C<tree> database
1001 use vars qw($filename $x %h $found) ;
1003 my $filename = "tree" ;
1005 # Enable duplicate records
1006 $DB_BTREE->{'flags'} = R_DUP ;
1008 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1009 or die "Cannot open $filename: $!\n";
1011 $x->del_dup("Wall", "Larry") ;
1013 $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
1014 print "Larry Wall is $found there\n" ;
1021 Larry Wall is not there
1023 =head2 Matching Partial Keys
1025 The BTREE interface has a feature which allows partial keys to be
1026 matched. This functionality is I<only> available when the C<seq> method
1027 is used along with the R_CURSOR flag.
1029 $x->seq($key, $value, R_CURSOR) ;
1031 Here is the relevant quote from the dbopen man page where it defines
1032 the use of the R_CURSOR flag with seq:
1034 Note, for the DB_BTREE access method, the returned key is not
1035 necessarily an exact match for the specified key. The returned key
1036 is the smallest key greater than or equal to the specified key,
1037 permitting partial key matches and range searches.
1039 In the example script below, the C<match> sub uses this feature to find
1040 and print the first matching key/value pair given a partial key.
1046 use vars qw($filename $x %h $st $key $value) ;
1052 my $orig_key = $key ;
1053 $x->seq($key, $value, R_CURSOR) ;
1054 print "$orig_key\t-> $key\t-> $value\n" ;
1057 $filename = "tree" ;
1060 $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
1061 or die "Cannot open $filename: $!\n";
1063 # Add some key/value pairs to the file
1064 $h{'mouse'} = 'mickey' ;
1065 $h{'Wall'} = 'Larry' ;
1066 $h{'Walls'} = 'Brick' ;
1067 $h{'Smith'} = 'John' ;
1071 print "IN ORDER\n" ;
1072 for ($st = $x->seq($key, $value, R_FIRST) ;
1074 $st = $x->seq($key, $value, R_NEXT) )
1076 { print "$key -> $value\n" }
1078 print "\nPARTIAL MATCH\n" ;
1098 a -> mouse -> mickey
1102 DB_RECNO provides an interface to flat text files. Both variable and
1103 fixed length records are supported.
1105 In order to make RECNO more compatible with Perl, the array offset for
1106 all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
1108 As with normal Perl arrays, a RECNO array can be accessed using
1109 negative indexes. The index -1 refers to the last element of the array,
1110 -2 the second last, and so on. Attempting to access an element before
1111 the start of the array will raise a fatal run-time error.
1113 =head2 The 'bval' Option
1115 The operation of the bval option warrants some discussion. Here is the
1116 definition of bval from the Berkeley DB 1.85 recno manual page:
1118 The delimiting byte to be used to mark the end of a
1119 record for variable-length records, and the pad charac-
1120 ter for fixed-length records. If no value is speci-
1121 fied, newlines (``\n'') are used to mark the end of
1122 variable-length records and fixed-length records are
1125 The second sentence is wrong. In actual fact bval will only default to
1126 C<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULL
1127 openinfo parameter is used at all, the value that happens to be in bval
1128 will be used. That means you always have to specify bval when making
1129 use of any of the options in the openinfo parameter. This documentation
1130 error will be fixed in the next release of Berkeley DB.
1132 That clarifies the situation with regards Berkeley DB itself. What
1133 about B<DB_File>? Well, the behavior defined in the quote above is
1134 quite useful, so B<DB_File> conforms to it.
1136 That means that you can specify other options (e.g. cachesize) and
1137 still have bval default to C<"\n"> for variable length records, and
1138 space for fixed length records.
1140 =head2 A Simple Example
1142 Here is a simple example that uses RECNO (if you are using a version
1143 of Perl earlier than 5.004_57 this example won't work -- see
1144 L<Extra RECNO Methods> for a workaround).
1149 my $filename = "text" ;
1153 tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_RECNO
1154 or die "Cannot open file 'text': $!\n" ;
1156 # Add a few key/value pairs to the file
1161 push @h, "green", "black" ;
1163 my $elements = scalar @h ;
1164 print "The array contains $elements entries\n" ;
1167 print "popped $last\n" ;
1169 unshift @h, "white" ;
1170 my $first = shift @h ;
1171 print "shifted $first\n" ;
1173 # Check for existence of a key
1174 print "Element 1 Exists with value $h[1]\n" if $h[1] ;
1176 # use a negative index
1177 print "The last element is $h[-1]\n" ;
1178 print "The 2nd last element is $h[-2]\n" ;
1182 Here is the output from the script:
1184 The array contains 5 entries
1187 Element 1 Exists with value blue
1188 The last element is green
1189 The 2nd last element is yellow
1191 =head2 Extra RECNO Methods
1193 If you are using a version of Perl earlier than 5.004_57, the tied
1194 array interface is quite limited. In the example script above
1195 C<push>, C<pop>, C<shift>, C<unshift>
1196 or determining the array length will not work with a tied array.
1198 To make the interface more useful for older versions of Perl, a number
1199 of methods are supplied with B<DB_File> to simulate the missing array
1200 operations. All these methods are accessed via the object returned from
1203 Here are the methods:
1207 =item B<$X-E<gt>push(list) ;>
1209 Pushes the elements of C<list> to the end of the array.
1211 =item B<$value = $X-E<gt>pop ;>
1213 Removes and returns the last element of the array.
1215 =item B<$X-E<gt>shift>
1217 Removes and returns the first element of the array.
1219 =item B<$X-E<gt>unshift(list) ;>
1221 Pushes the elements of C<list> to the start of the array.
1223 =item B<$X-E<gt>length>
1225 Returns the number of elements in the array.
1229 =head2 Another Example
1231 Here is a more complete example that makes use of some of the methods
1232 described above. It also makes use of the API interface directly (see
1233 L<THE API INTERFACE>).
1236 use vars qw(@h $H $file $i) ;
1244 $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO
1245 or die "Cannot open file $file: $!\n" ;
1247 # first create a text file to play with
1255 # Print the records in order.
1257 # The length method is needed here because evaluating a tied
1258 # array in a scalar context does not return the number of
1259 # elements in the array.
1261 print "\nORIGINAL\n" ;
1262 foreach $i (0 .. $H->length - 1) {
1263 print "$i: $h[$i]\n" ;
1266 # use the push & pop methods
1269 print "\nThe last record was [$a]\n" ;
1271 # and the shift & unshift methods
1273 $H->unshift("first") ;
1274 print "The first record was [$a]\n" ;
1276 # Use the API to add a new record after record 2.
1278 $H->put($i, "Newbie", R_IAFTER) ;
1280 # and a new record before record 1.
1282 $H->put($i, "New One", R_IBEFORE) ;
1287 # now print the records in reverse order
1288 print "\nREVERSE\n" ;
1289 for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
1290 { print "$i: $h[$i]\n" }
1292 # same again, but use the API functions instead
1293 print "\nREVERSE again\n" ;
1294 my ($s, $k, $v) = (0, 0, 0) ;
1295 for ($s = $H->seq($k, $v, R_LAST) ;
1297 $s = $H->seq($k, $v, R_PREV))
1298 { print "$k: $v\n" }
1303 and this is what it outputs:
1312 The last record was [four]
1313 The first record was [zero]
1337 Rather than iterating through the array, C<@h> like this:
1341 it is necessary to use either this:
1343 foreach $i (0 .. $H->length - 1)
1347 for ($a = $H->get($k, $v, R_FIRST) ;
1349 $a = $H->get($k, $v, R_NEXT) )
1353 Notice that both times the C<put> method was used the record index was
1354 specified using a variable, C<$i>, rather than the literal value
1355 itself. This is because C<put> will return the record number of the
1356 inserted line via that parameter.
1360 =head1 THE API INTERFACE
1362 As well as accessing Berkeley DB using a tied hash or array, it is also
1363 possible to make direct use of most of the API functions defined in the
1364 Berkeley DB documentation.
1366 To do this you need to store a copy of the object returned from the tie.
1368 $db = tie %hash, "DB_File", "filename" ;
1370 Once you have done that, you can access the Berkeley DB API functions
1371 as B<DB_File> methods directly like this:
1373 $db->put($key, $value, R_NOOVERWRITE) ;
1375 B<Important:> If you have saved a copy of the object returned from
1376 C<tie>, the underlying database file will I<not> be closed until both
1377 the tied variable is untied and all copies of the saved object are
1381 $db = tie %hash, "DB_File", "filename"
1382 or die "Cannot tie filename: $!" ;
1387 See L<The untie() Gotcha> for more details.
1389 All the functions defined in L<dbopen> are available except for
1390 close() and dbopen() itself. The B<DB_File> method interface to the
1391 supported functions have been implemented to mirror the way Berkeley DB
1392 works whenever possible. In particular note that:
1398 The methods return a status value. All return 0 on success.
1399 All return -1 to signify an error and set C<$!> to the exact
1400 error code. The return code 1 generally (but not always) means that the
1401 key specified did not exist in the database.
1403 Other return codes are defined. See below and in the Berkeley DB
1404 documentation for details. The Berkeley DB documentation should be used
1405 as the definitive source.
1409 Whenever a Berkeley DB function returns data via one of its parameters,
1410 the equivalent B<DB_File> method does exactly the same.
1414 If you are careful, it is possible to mix API calls with the tied
1415 hash/array interface in the same piece of code. Although only a few of
1416 the methods used to implement the tied interface currently make use of
1417 the cursor, you should always assume that the cursor has been changed
1418 any time the tied hash/array interface is used. As an example, this
1419 code will probably not do what you expect:
1421 $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1422 or die "Cannot tie $filename: $!" ;
1424 # Get the first key/value pair and set the cursor
1425 $X->seq($key, $value, R_FIRST) ;
1427 # this line will modify the cursor
1428 $count = scalar keys %x ;
1430 # Get the second key/value pair.
1431 # oops, it didn't, it got the last key/value pair!
1432 $X->seq($key, $value, R_NEXT) ;
1434 The code above can be rearranged to get around the problem, like this:
1436 $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1437 or die "Cannot tie $filename: $!" ;
1439 # this line will modify the cursor
1440 $count = scalar keys %x ;
1442 # Get the first key/value pair and set the cursor
1443 $X->seq($key, $value, R_FIRST) ;
1445 # Get the second key/value pair.
1447 $X->seq($key, $value, R_NEXT) ;
1451 All the constants defined in L<dbopen> for use in the flags parameters
1452 in the methods defined below are also available. Refer to the Berkeley
1453 DB documentation for the precise meaning of the flags values.
1455 Below is a list of the methods available.
1459 =item B<$status = $X-E<gt>get($key, $value [, $flags]) ;>
1461 Given a key (C<$key>) this method reads the value associated with it
1462 from the database. The value read from the database is returned in the
1463 C<$value> parameter.
1465 If the key does not exist the method returns 1.
1467 No flags are currently defined for this method.
1469 =item B<$status = $X-E<gt>put($key, $value [, $flags]) ;>
1471 Stores the key/value pair in the database.
1473 If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter
1474 will have the record number of the inserted key/value pair set.
1476 Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
1479 =item B<$status = $X-E<gt>del($key [, $flags]) ;>
1481 Removes all key/value pairs with key C<$key> from the database.
1483 A return code of 1 means that the requested key was not in the
1486 R_CURSOR is the only valid flag at present.
1488 =item B<$status = $X-E<gt>fd ;>
1490 Returns the file descriptor for the underlying database.
1492 See L<Locking: The Trouble with fd> for an explanation for why you should
1493 not use C<fd> to lock your database.
1495 =item B<$status = $X-E<gt>seq($key, $value, $flags) ;>
1497 This interface allows sequential retrieval from the database. See
1498 L<dbopen> for full details.
1500 Both the C<$key> and C<$value> parameters will be set to the key/value
1501 pair read from the database.
1503 The flags parameter is mandatory. The valid flag values are R_CURSOR,
1504 R_FIRST, R_LAST, R_NEXT and R_PREV.
1506 =item B<$status = $X-E<gt>sync([$flags]) ;>
1508 Flushes any cached buffers to disk.
1510 R_RECNOSYNC is the only valid flag at present.
1516 A DBM Filter is a piece of code that is be used when you I<always>
1517 want to make the same transformation to all keys and/or values in a
1520 There are four methods associated with DBM Filters. All work identically,
1521 and each is used to install (or uninstall) a single DBM Filter. Each
1522 expects a single parameter, namely a reference to a sub. The only
1523 difference between them is the place that the filter is installed.
1529 =item B<filter_store_key>
1531 If a filter has been installed with this method, it will be invoked
1532 every time you write a key to a DBM database.
1534 =item B<filter_store_value>
1536 If a filter has been installed with this method, it will be invoked
1537 every time you write a value to a DBM database.
1540 =item B<filter_fetch_key>
1542 If a filter has been installed with this method, it will be invoked
1543 every time you read a key from a DBM database.
1545 =item B<filter_fetch_value>
1547 If a filter has been installed with this method, it will be invoked
1548 every time you read a value from a DBM database.
1552 You can use any combination of the methods, from none, to all four.
1554 All filter methods return the existing filter, if present, or C<undef>
1557 To delete a filter pass C<undef> to it.
1561 When each filter is called by Perl, a local copy of C<$_> will contain
1562 the key or value to be filtered. Filtering is achieved by modifying
1563 the contents of C<$_>. The return code from the filter is ignored.
1565 =head2 An Example -- the NULL termination problem.
1567 Consider the following scenario. You have a DBM database
1568 that you need to share with a third-party C application. The C application
1569 assumes that I<all> keys and values are NULL terminated. Unfortunately
1570 when Perl writes to DBM databases it doesn't use NULL termination, so
1571 your Perl application will have to manage NULL termination itself. When
1572 you write to the database you will have to use something like this:
1574 $hash{"$key\0"} = "$value\0" ;
1576 Similarly the NULL needs to be taken into account when you are considering
1577 the length of existing keys/values.
1579 It would be much better if you could ignore the NULL terminations issue
1580 in the main application code and have a mechanism that automatically
1581 added the terminating NULL to all keys and values whenever you write to
1582 the database and have them removed when you read from the database. As I'm
1583 sure you have already guessed, this is a problem that DBM Filters can
1590 my $filename = "/tmp/filt" ;
1593 my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
1594 or die "Cannot open $filename: $!\n" ;
1596 # Install DBM Filters
1597 $db->filter_fetch_key ( sub { s/\0$// } ) ;
1598 $db->filter_store_key ( sub { $_ .= "\0" } ) ;
1599 $db->filter_fetch_value( sub { s/\0$// } ) ;
1600 $db->filter_store_value( sub { $_ .= "\0" } ) ;
1602 $hash{"abc"} = "def" ;
1603 my $a = $hash{"ABC"} ;
1608 Hopefully the contents of each of the filters should be
1609 self-explanatory. Both "fetch" filters remove the terminating NULL,
1610 and both "store" filters add a terminating NULL.
1613 =head2 Another Example -- Key is a C int.
1615 Here is another real-life example. By default, whenever Perl writes to
1616 a DBM database it always writes the key and value as strings. So when
1619 $hash{12345} = "soemthing" ;
1621 the key 12345 will get stored in the DBM database as the 5 byte string
1622 "12345". If you actually want the key to be stored in the DBM database
1623 as a C int, you will have to use C<pack> when writing, and C<unpack>
1626 Here is a DBM Filter that does it:
1631 my $filename = "/tmp/filt" ;
1635 my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
1636 or die "Cannot open $filename: $!\n" ;
1638 $db->filter_fetch_key ( sub { $_ = unpack("i", $_) } ) ;
1639 $db->filter_store_key ( sub { $_ = pack ("i", $_) } ) ;
1640 $hash{123} = "def" ;
1645 This time only two filters have been used -- we only need to manipulate
1646 the contents of the key, so it wasn't necessary to install any value
1649 =head1 HINTS AND TIPS
1652 =head2 Locking: The Trouble with fd
1654 Until version 1.72 of this module, the recommended technique for locking
1655 B<DB_File> databases was to flock the filehandle returned from the "fd"
1656 function. Unfortunately this technique has been shown to be fundamentally
1657 flawed (Kudos to David Harris for tracking this down). Use it at your own
1660 The locking technique went like this.
1662 $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644)
1663 || die "dbcreat /tmp/foo.db $!";
1665 open(DB_FH, "+<&=$fd") || die "dup $!";
1666 flock (DB_FH, LOCK_EX) || die "flock: $!";
1668 $db{"Tom"} = "Jerry" ;
1670 flock(DB_FH, LOCK_UN);
1675 In simple terms, this is what happens:
1681 Use "tie" to open the database.
1685 Lock the database with fd & flock.
1689 Read & Write to the database.
1693 Unlock and close the database.
1697 Here is the crux of the problem. A side-effect of opening the B<DB_File>
1698 database in step 2 is that an initial block from the database will get
1699 read from disk and cached in memory.
1701 To see why this is a problem, consider what can happen when two processes,
1702 say "A" and "B", both want to update the same B<DB_File> database
1703 using the locking steps outlined above. Assume process "A" has already
1704 opened the database and has a write lock, but it hasn't actually updated
1705 the database yet (it has finished step 2, but not started step 3 yet). Now
1706 process "B" tries to open the same database - step 1 will succeed,
1707 but it will block on step 2 until process "A" releases the lock. The
1708 important thing to notice here is that at this point in time both
1709 processes will have cached identical initial blocks from the database.
1711 Now process "A" updates the database and happens to change some of the
1712 data held in the initial buffer. Process "A" terminates, flushing
1713 all cached data to disk and releasing the database lock. At this point
1714 the database on disk will correctly reflect the changes made by process
1717 With the lock released, process "B" can now continue. It also updates the
1718 database and unfortunately it too modifies the data that was in its
1719 initial buffer. Once that data gets flushed to disk it will overwrite
1720 some/all of the changes process "A" made to the database.
1722 The result of this scenario is at best a database that doesn't contain
1723 what you expect. At worst the database will corrupt.
1725 The above won't happen every time competing process update the same
1726 B<DB_File> database, but it does illustrate why the technique should
1729 =head2 Safe ways to lock a database
1731 Starting with version 2.x, Berkeley DB has internal support for locking.
1732 The companion module to this one, B<BerkeleyDB>, provides an interface
1733 to this locking functionality. If you are serious about locking
1734 Berkeley DB databases, I strongly recommend using B<BerkeleyDB>.
1736 If using B<BerkeleyDB> isn't an option, there are a number of modules
1737 available on CPAN that can be used to implement locking. Each one
1738 implements locking differently and has different goals in mind. It is
1739 therefore worth knowing the difference, so that you can pick the right
1740 one for your application. Here are the three locking wrappers:
1744 =item B<Tie::DB_Lock>
1746 A B<DB_File> wrapper which creates copies of the database file for
1747 read access, so that you have a kind of a multiversioning concurrent read
1748 system. However, updates are still serial. Use for databases where reads
1749 may be lengthy and consistency problems may occur.
1751 =item B<Tie::DB_LockFile>
1753 A B<DB_File> wrapper that has the ability to lock and unlock the database
1754 while it is being used. Avoids the tie-before-flock problem by simply
1755 re-tie-ing the database when you get or drop a lock. Because of the
1756 flexibility in dropping and re-acquiring the lock in the middle of a
1757 session, this can be massaged into a system that will work with long
1758 updates and/or reads if the application follows the hints in the POD
1761 =item B<DB_File::Lock>
1763 An extremely lightweight B<DB_File> wrapper that simply flocks a lockfile
1764 before tie-ing the database and drops the lock after the untie. Allows
1765 one to use the same lockfile for multiple databases to avoid deadlock
1766 problems, if desired. Use for databases where updates are reads are
1767 quick and simple flock locking semantics are enough.
1771 =head2 Sharing Databases With C Applications
1773 There is no technical reason why a Berkeley DB database cannot be
1774 shared by both a Perl and a C application.
1776 The vast majority of problems that are reported in this area boil down
1777 to the fact that C strings are NULL terminated, whilst Perl strings are
1778 not. See L<DBM FILTERS> for a generic way to work around this problem.
1780 Here is a real example. Netscape 2.0 keeps a record of the locations you
1781 visit along with the time you last visited them in a DB_HASH database.
1782 This is usually stored in the file F<~/.netscape/history.db>. The key
1783 field in the database is the location string and the value field is the
1784 time the location was last visited stored as a 4 byte binary value.
1786 If you haven't already guessed, the location string is stored with a
1787 terminating NULL. This means you need to be careful when accessing the
1790 Here is a snippet of code that is loosely based on Tom Christiansen's
1791 I<ggh> script (available from your nearest CPAN archive in
1792 F<authors/id/TOMC/scripts/nshist.gz>).
1798 use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ;
1799 $dotdir = $ENV{HOME} || $ENV{LOGNAME};
1801 $HISTORY = "$dotdir/.netscape/history.db";
1803 tie %hist_db, 'DB_File', $HISTORY
1804 or die "Cannot open $HISTORY: $!\n" ;;
1806 # Dump the complete database
1807 while ( ($href, $binary_time) = each %hist_db ) {
1809 # remove the terminating NULL
1810 $href =~ s/\x00$// ;
1812 # convert the binary time into a user friendly string
1813 $date = localtime unpack("V", $binary_time);
1814 print "$date $href\n" ;
1817 # check for the existence of a specific key
1818 # remember to add the NULL
1819 if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) {
1820 $date = localtime unpack("V", $binary_time) ;
1821 print "Last visited mox.perl.com on $date\n" ;
1824 print "Never visited mox.perl.com\n"
1829 =head2 The untie() Gotcha
1831 If you make use of the Berkeley DB API, it is I<very> strongly
1832 recommended that you read L<perltie/The untie Gotcha>.
1834 Even if you don't currently make use of the API interface, it is still
1837 Here is an example which illustrates the problem from a B<DB_File>
1846 $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_TRUNC
1847 or die "Cannot tie first time: $!" ;
1853 tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
1854 or die "Cannot tie second time: $!" ;
1858 When run, the script will produce this error message:
1860 Cannot tie second time: Invalid argument at bad.file line 14.
1862 Although the error message above refers to the second tie() statement
1863 in the script, the source of the problem is really with the untie()
1864 statement that precedes it.
1866 Having read L<perltie> you will probably have already guessed that the
1867 error is caused by the extra copy of the tied object stored in C<$X>.
1868 If you haven't, then the problem boils down to the fact that the
1869 B<DB_File> destructor, DESTROY, will not be called until I<all>
1870 references to the tied object are destroyed. Both the tied variable,
1871 C<%x>, and C<$X> above hold a reference to the object. The call to
1872 untie() will destroy the first, but C<$X> still holds a valid
1873 reference, so the destructor will not get called and the database file
1874 F<tst.fil> will remain open. The fact that Berkeley DB then reports the
1875 attempt to open a database that is already open via the catch-all
1876 "Invalid argument" doesn't help.
1878 If you run the script with the C<-w> flag the error message becomes:
1880 untie attempted while 1 inner references still exist at bad.file line 12.
1881 Cannot tie second time: Invalid argument at bad.file line 14.
1883 which pinpoints the real problem. Finally the script can now be
1884 modified to fix the original problem by destroying the API object
1893 $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
1897 =head1 COMMON QUESTIONS
1899 =head2 Why is there Perl source in my database?
1901 If you look at the contents of a database file created by DB_File,
1902 there can sometimes be part of a Perl script included in it.
1904 This happens because Berkeley DB uses dynamic memory to allocate
1905 buffers which will subsequently be written to the database file. Being
1906 dynamic, the memory could have been used for anything before DB
1907 malloced it. As Berkeley DB doesn't clear the memory once it has been
1908 allocated, the unused portions will contain random junk. In the case
1909 where a Perl script gets written to the database, the random junk will
1910 correspond to an area of dynamic memory that happened to be used during
1911 the compilation of the script.
1913 Unless you don't like the possibility of there being part of your Perl
1914 scripts embedded in a database file, this is nothing to worry about.
1916 =head2 How do I store complex data structures with DB_File?
1918 Although B<DB_File> cannot do this directly, there is a module which
1919 can layer transparently over B<DB_File> to accomplish this feat.
1921 Check out the MLDBM module, available on CPAN in the directory
1922 F<modules/by-module/MLDBM>.
1924 =head2 What does "Invalid Argument" mean?
1926 You will get this error message when one of the parameters in the
1927 C<tie> call is wrong. Unfortunately there are quite a few parameters to
1928 get wrong, so it can be difficult to figure out which one it is.
1930 Here are a couple of possibilities:
1936 Attempting to reopen a database without closing it.
1940 Using the O_WRONLY flag.
1944 =head2 What does "Bareword 'DB_File' not allowed" mean?
1946 You will encounter this particular error message when you have the
1947 C<strict 'subs'> pragma (or the full strict pragma) in your script.
1948 Consider this script:
1953 tie %x, DB_File, "filename" ;
1955 Running it produces the error in question:
1957 Bareword "DB_File" not allowed while "strict subs" in use
1959 To get around the error, place the word C<DB_File> in either single or
1960 double quotes, like this:
1962 tie %x, "DB_File", "filename" ;
1964 Although it might seem like a real pain, it is really worth the effort
1965 of having a C<use strict> in all your scripts.
1969 Articles that are either about B<DB_File> or make use of it.
1975 I<Full-Text Searching in Perl>, Tim Kientzle (tkientzle@ddj.com),
1976 Dr. Dobb's Journal, Issue 295, January 1999, pp 34-41
1982 Moved to the Changes file.
1986 Some older versions of Berkeley DB had problems with fixed length
1987 records using the RECNO file format. This problem has been fixed since
1988 version 1.85 of Berkeley DB.
1990 I am sure there are bugs in the code. If you do find any, or can
1991 suggest any enhancements, I would welcome your comments.
1995 B<DB_File> comes with the standard Perl source distribution. Look in
1996 the directory F<ext/DB_File>. Given the amount of time between releases
1997 of Perl the version that ships with Perl is quite likely to be out of
1998 date, so the most recent version can always be found on CPAN (see
1999 L<perlmod/CPAN> for details), in the directory
2000 F<modules/by-module/DB_File>.
2002 This version of B<DB_File> will work with either version 1.x, 2.x or
2003 3.x of Berkeley DB, but is limited to the functionality provided by
2006 The official web site for Berkeley DB is F<http://www.sleepycat.com>.
2007 All versions of Berkeley DB are available there.
2009 Alternatively, Berkeley DB version 1 is available at your nearest CPAN
2010 archive in F<src/misc/db.1.85.tar.gz>.
2012 If you are running IRIX, then get Berkeley DB version 1 from
2013 F<http://reality.sgi.com/ariel>. It has the patches necessary to
2014 compile properly on IRIX 5.3.
2018 Copyright (c) 1995-1999 Paul Marquess. All rights reserved. This program
2019 is free software; you can redistribute it and/or modify it under the
2020 same terms as Perl itself.
2022 Although B<DB_File> is covered by the Perl license, the library it
2023 makes use of, namely Berkeley DB, is not. Berkeley DB has its own
2024 copyright and its own license. Please take the time to read it.
2026 Here are are few words taken from the Berkeley DB FAQ (at
2027 F<http://www.sleepycat.com>) regarding the license:
2029 Do I have to license DB to use it in Perl scripts?
2031 No. The Berkeley DB license requires that software that uses
2032 Berkeley DB be freely redistributable. In the case of Perl, that
2033 software is Perl, and not your scripts. Any Perl scripts that you
2034 write are your property, including scripts that make use of
2035 Berkeley DB. Neither the Perl license nor the Berkeley DB license
2036 place any restriction on what you may do with them.
2038 If you are in any doubt about the license situation, contact either the
2039 Berkeley DB authors or the author of DB_File. See L<"AUTHOR"> for details.
2044 L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)>,
2049 The DB_File interface was written by Paul Marquess
2050 E<lt>Paul.Marquess@btinternet.comE<gt>.
2051 Questions about the DB system itself may be addressed to
2052 E<lt>db@sleepycat.com<gt>.