DB_File 1.09 patch
[p5sagit/p5-mst-13.2.git] / ext / DB_File / DB_File.pm
1 # DB_File.pm -- Perl 5 interface to Berkeley DB 
2 #
3 # written by Paul Marquess (pmarquess@bfsec.bt.co.uk)
4 # last modified 18th Dec 1996
5 # version 1.09
6 #
7 #     Copyright (c) 1995, 1996 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.
10
11
12 package DB_File::HASHINFO ;
13
14 require 5.003 ;
15
16 use strict;
17 use Carp;
18 require Tie::Hash;
19 @DB_File::HASHINFO::ISA = qw(Tie::Hash);
20
21 sub new
22 {
23     my $pkg = shift ;
24     my %x ;
25     tie %x, $pkg ;
26     bless \%x, $pkg ;
27 }
28
29
30 sub TIEHASH
31 {
32     my $pkg = shift ;
33
34     bless { VALID => { map {$_, 1} 
35                        qw( bsize ffactor nelem cachesize hash lorder)
36                      }, 
37             GOT   => {}
38           }, $pkg ;
39 }
40
41
42 sub FETCH 
43 {  
44     my $self  = shift ;
45     my $key   = shift ;
46
47     return $self->{GOT}{$key} if exists $self->{VALID}{$key}  ;
48
49     my $pkg = ref $self ;
50     croak "${pkg}::FETCH - Unknown element '$key'" ;
51 }
52
53
54 sub STORE 
55 {
56     my $self  = shift ;
57     my $key   = shift ;
58     my $value = shift ;
59
60     if ( exists $self->{VALID}{$key} )
61     {
62         $self->{GOT}{$key} = $value ;
63         return ;
64     }
65     
66     my $pkg = ref $self ;
67     croak "${pkg}::STORE - Unknown element '$key'" ;
68 }
69
70 sub DELETE 
71 {
72     my $self = shift ;
73     my $key  = shift ;
74
75     if ( exists $self->{VALID}{$key} )
76     {
77         delete $self->{GOT}{$key} ;
78         return ;
79     }
80     
81     my $pkg = ref $self ;
82     croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ;
83 }
84
85 sub EXISTS
86 {
87     my $self = shift ;
88     my $key  = shift ;
89
90     exists $self->{VALID}{$key} ;
91 }
92
93 sub NotHere
94 {
95     my $self = shift ;
96     my $method = shift ;
97
98     croak ref($self) . " does not define the method ${method}" ;
99 }
100
101 sub DESTROY  { undef %{$_[0]} }
102 sub FIRSTKEY { my $self = shift ; $self->NotHere("FIRSTKEY") }
103 sub NEXTKEY  { my $self = shift ; $self->NotHere("NEXTKEY") }
104 sub CLEAR    { my $self = shift ; $self->NotHere("CLEAR") }
105
106 package DB_File::RECNOINFO ;
107
108 use strict ;
109
110 @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ;
111
112 sub TIEHASH
113 {
114     my $pkg = shift ;
115
116     bless { VALID => { map {$_, 1} 
117                        qw( bval cachesize psize flags lorder reclen bfname )
118                      },
119             GOT   => {},
120           }, $pkg ;
121 }
122
123 package DB_File::BTREEINFO ;
124
125 use strict ;
126
127 @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ;
128
129 sub TIEHASH
130 {
131     my $pkg = shift ;
132
133     bless { VALID => { map {$_, 1} 
134                        qw( flags cachesize maxkeypage minkeypage psize 
135                            compare prefix lorder )
136                      },
137             GOT   => {},
138           }, $pkg ;
139 }
140
141
142 package DB_File ;
143
144 use strict;
145 use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO) ;
146 use Carp;
147
148
149 $VERSION = "1.09" ;
150
151 #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
152 $DB_BTREE = new DB_File::BTREEINFO ;
153 $DB_HASH  = new DB_File::HASHINFO ;
154 $DB_RECNO = new DB_File::RECNOINFO ;
155
156 require Tie::Hash;
157 require Exporter;
158 use AutoLoader;
159 require DynaLoader;
160 @ISA = qw(Tie::Hash Exporter DynaLoader);
161 @EXPORT = qw(
162         $DB_BTREE $DB_HASH $DB_RECNO 
163
164         BTREEMAGIC
165         BTREEVERSION
166         DB_LOCK
167         DB_SHMEM
168         DB_TXN
169         HASHMAGIC
170         HASHVERSION
171         MAX_PAGE_NUMBER
172         MAX_PAGE_OFFSET
173         MAX_REC_NUMBER
174         RET_ERROR
175         RET_SPECIAL
176         RET_SUCCESS
177         R_CURSOR
178         R_DUP
179         R_FIRST
180         R_FIXEDLEN
181         R_IAFTER
182         R_IBEFORE
183         R_LAST
184         R_NEXT
185         R_NOKEY
186         R_NOOVERWRITE
187         R_PREV
188         R_RECNOSYNC
189         R_SETCURSOR
190         R_SNAPSHOT
191         __R_UNUSED
192
193 );
194
195 sub AUTOLOAD {
196     my($constname);
197     ($constname = $AUTOLOAD) =~ s/.*:://;
198     my $val = constant($constname, @_ ? $_[0] : 0);
199     if ($! != 0) {
200         if ($! =~ /Invalid/) {
201             $AutoLoader::AUTOLOAD = $AUTOLOAD;
202             goto &AutoLoader::AUTOLOAD;
203         }
204         else {
205             my($pack,$file,$line) = caller;
206             croak "Your vendor has not defined DB macro $constname, used at $file line $line.
207 ";
208         }
209     }
210     eval "sub $AUTOLOAD { $val }";
211     goto &$AUTOLOAD;
212 }
213
214
215 # import borrowed from IO::File
216 #   exports Fcntl constants if available.
217 sub import {
218     my $pkg = shift;
219     my $callpkg = caller;
220     Exporter::export $pkg, $callpkg, @_;
221     eval {
222         require Fcntl;
223         Exporter::export 'Fcntl', $callpkg, '/^O_/';
224     };
225 }
226
227 bootstrap DB_File $VERSION;
228
229 # Preloaded methods go here.  Autoload methods go after __END__, and are
230 # processed by the autosplit program.
231
232 sub TIEHASH
233 {
234     my (@arg) = @_ ;
235
236     $arg[4] = tied %{ $arg[4] } 
237         if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ;
238
239     DoTie_(@arg) ;
240 }
241
242 *TIEARRAY = \&TIEHASH ;
243
244 sub get_dup
245 {
246     croak "Usage: \$db->get_dup(key [,flag])\n"
247         unless @_ == 2 or @_ == 3 ;
248  
249     my $db        = shift ;
250     my $key       = shift ;
251     my $flag      = shift ;
252     my $value     = 0 ;
253     my $origkey   = $key ;
254     my $wantarray = wantarray ;
255     my %values    = () ;
256     my @values    = () ;
257     my $counter   = 0 ;
258     my $status    = 0 ;
259  
260     # iterate through the database until either EOF ($status == 0)
261     # or a different key is encountered ($key ne $origkey).
262     for ($status = $db->seq($key, $value, R_CURSOR()) ;
263          $status == 0 and $key eq $origkey ;
264          $status = $db->seq($key, $value, R_NEXT()) ) {
265  
266         # save the value or count number of matches
267         if ($wantarray) {
268             if ($flag)
269                 { ++ $values{$value} }
270             else
271                 { push (@values, $value) }
272         }
273         else
274             { ++ $counter }
275      
276     }
277  
278     return ($wantarray ? ($flag ? %values : @values) : $counter) ;
279 }
280
281
282 1;
283 __END__
284
285 =cut
286
287 =head1 NAME
288
289 DB_File - Perl5 access to Berkeley DB
290
291 =head1 SYNOPSIS
292
293  use DB_File ;
294  
295  [$X =] tie %hash,  'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
296  [$X =] tie %hash,  'DB_File', $filename, $flags, $mode, $DB_BTREE ;
297  [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
298
299  $status = $X->del($key [, $flags]) ;
300  $status = $X->put($key, $value [, $flags]) ;
301  $status = $X->get($key, $value [, $flags]) ;
302  $status = $X->seq($key, $value, $flags) ;
303  $status = $X->sync([$flags]) ;
304  $status = $X->fd ;
305
306  # BTREE only
307  $count = $X->get_dup($key) ;
308  @list  = $X->get_dup($key) ;
309  %list  = $X->get_dup($key, 1) ;
310
311  # RECNO only
312  $a = $X->length;
313  $a = $X->pop ;
314  $X->push(list);
315  $a = $X->shift;
316  $X->unshift(list);
317
318  untie %hash ;
319  untie @array ;
320
321 =head1 DESCRIPTION
322
323 B<DB_File> is a module which allows Perl programs to make use of the
324 facilities provided by Berkeley DB.  If you intend to use this
325 module you should really have a copy of the Berkeley DB manual pages at
326 hand. The interface defined here mirrors the Berkeley DB interface
327 closely.
328
329 Berkeley DB is a C library which provides a consistent interface to a
330 number of database formats.  B<DB_File> provides an interface to all
331 three of the database types currently supported by Berkeley DB.
332
333 The file types are:
334
335 =over 5
336
337 =item B<DB_HASH>
338
339 This database type allows arbitrary key/value pairs to be stored in data
340 files. This is equivalent to the functionality provided by other
341 hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
342 the files created using DB_HASH are not compatible with any of the
343 other packages mentioned.
344
345 A default hashing algorithm, which will be adequate for most
346 applications, is built into Berkeley DB. If you do need to use your own
347 hashing algorithm it is possible to write your own in Perl and have
348 B<DB_File> use it instead.
349
350 =item B<DB_BTREE>
351
352 The btree format allows arbitrary key/value pairs to be stored in a
353 sorted, balanced binary tree.
354
355 As with the DB_HASH format, it is possible to provide a user defined
356 Perl routine to perform the comparison of keys. By default, though, the
357 keys are stored in lexical order.
358
359 =item B<DB_RECNO>
360
361 DB_RECNO allows both fixed-length and variable-length flat text files
362 to be manipulated using the same key/value pair interface as in DB_HASH
363 and DB_BTREE.  In this case the key will consist of a record (line)
364 number.
365
366 =back
367
368 =head2 How does DB_File interface to Berkeley DB?
369
370 B<DB_File> allows access to Berkeley DB files using the tie() mechanism
371 in Perl 5 (for full details, see L<perlfunc/tie()>). This facility
372 allows B<DB_File> to access Berkeley DB files using either an
373 associative array (for DB_HASH & DB_BTREE file types) or an ordinary
374 array (for the DB_RECNO file type).
375
376 In addition to the tie() interface, it is also possible to access most
377 of the functions provided in the Berkeley DB API directly.
378 See L<THE API INTERFACE>.
379
380 =head2 Opening a Berkeley DB Database File
381
382 Berkeley DB uses the function dbopen() to open or create a database.
383 Here is the C prototype for dbopen():
384
385       DB*
386       dbopen (const char * file, int flags, int mode, 
387               DBTYPE type, const void * openinfo)
388
389 The parameter C<type> is an enumeration which specifies which of the 3
390 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
391 Depending on which of these is actually chosen, the final parameter,
392 I<openinfo> points to a data structure which allows tailoring of the
393 specific interface method.
394
395 This interface is handled slightly differently in B<DB_File>. Here is
396 an equivalent call using B<DB_File>:
397
398         tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ;
399
400 The C<filename>, C<flags> and C<mode> parameters are the direct
401 equivalent of their dbopen() counterparts. The final parameter $DB_HASH
402 performs the function of both the C<type> and C<openinfo> parameters in
403 dbopen().
404
405 In the example above $DB_HASH is actually a pre-defined reference to a
406 hash object. B<DB_File> has three of these pre-defined references.
407 Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
408
409 The keys allowed in each of these pre-defined references is limited to
410 the names used in the equivalent C structure. So, for example, the
411 $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>,
412 C<ffactor>, C<hash>, C<lorder> and C<nelem>. 
413
414 To change one of these elements, just assign to it like this:
415
416         $DB_HASH->{'cachesize'} = 10000 ;
417
418 The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are
419 usually adequate for most applications.  If you do need to create extra
420 instances of these objects, constructors are available for each file
421 type.
422
423 Here are examples of the constructors and the valid options available
424 for DB_HASH, DB_BTREE and DB_RECNO respectively.
425
426      $a = new DB_File::HASHINFO ;
427      $a->{'bsize'} ;
428      $a->{'cachesize'} ;
429      $a->{'ffactor'};
430      $a->{'hash'} ;
431      $a->{'lorder'} ;
432      $a->{'nelem'} ;
433
434      $b = new DB_File::BTREEINFO ;
435      $b->{'flags'} ;
436      $b->{'cachesize'} ;
437      $b->{'maxkeypage'} ;
438      $b->{'minkeypage'} ;
439      $b->{'psize'} ;
440      $b->{'compare'} ;
441      $b->{'prefix'} ;
442      $b->{'lorder'} ;
443
444      $c = new DB_File::RECNOINFO ;
445      $c->{'bval'} ;
446      $c->{'cachesize'} ;
447      $c->{'psize'} ;
448      $c->{'flags'} ;
449      $c->{'lorder'} ;
450      $c->{'reclen'} ;
451      $c->{'bfname'} ;
452
453 The values stored in the hashes above are mostly the direct equivalent
454 of their C counterpart. Like their C counterparts, all are set to a
455 default values - that means you don't have to set I<all> of the
456 values when you only want to change one. Here is an example:
457
458      $a = new DB_File::HASHINFO ;
459      $a->{'cachesize'} =  12345 ;
460      tie %y, 'DB_File', "filename", $flags, 0777, $a ;
461
462 A few of the options need extra discussion here. When used, the C
463 equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers
464 to C functions. In B<DB_File> these keys are used to store references
465 to Perl subs. Below are templates for each of the subs:
466
467     sub hash
468     {
469         my ($data) = @_ ;
470         ...
471         # return the hash value for $data
472         return $hash ;
473     }
474
475     sub compare
476     {
477         my ($key, $key2) = @_ ;
478         ...
479         # return  0 if $key1 eq $key2
480         #        -1 if $key1 lt $key2
481         #         1 if $key1 gt $key2
482         return (-1 , 0 or 1) ;
483     }
484
485     sub prefix
486     {
487         my ($key, $key2) = @_ ;
488         ...
489         # return number of bytes of $key2 which are 
490         # necessary to determine that it is greater than $key1
491         return $bytes ;
492     }
493
494 See L<Changing the BTREE sort order> for an example of using the
495 C<compare> template.
496
497 If you are using the DB_RECNO interface and you intend making use of
498 C<bval>, you should check out L<The bval option>.
499
500 =head2 Default Parameters
501
502 It is possible to omit some or all of the final 4 parameters in the
503 call to C<tie> and let them take default values. As DB_HASH is the most
504 common file format used, the call:
505
506     tie %A, "DB_File", "filename" ;
507
508 is equivalent to:
509
510     tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;
511
512 It is also possible to omit the filename parameter as well, so the
513 call:
514
515     tie %A, "DB_File" ;
516
517 is equivalent to:
518
519     tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;
520
521 See L<In Memory Databases> for a discussion on the use of C<undef>
522 in place of a filename.
523
524 =head2 In Memory Databases
525
526 Berkeley DB allows the creation of in-memory databases by using NULL
527 (that is, a C<(char *)0> in C) in place of the filename.  B<DB_File>
528 uses C<undef> instead of NULL to provide this functionality.
529
530 =head1 DB_HASH
531
532 The DB_HASH file format is probably the most commonly used of the three
533 file formats that B<DB_File> supports. It is also very straightforward
534 to use.
535
536 =head2 A Simple Example.
537
538 This example shows how to create a database, add key/value pairs to the
539 database, delete keys/value pairs and finally how to enumerate the
540 contents of the database.
541
542     use strict ;
543     use DB_File ;
544     use vars qw( %h $k $v ) ;
545
546     tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH 
547         or die "Cannot open file 'fruit': $!\n";
548
549     # Add a few key/value pairs to the file
550     $h{"apple"} = "red" ;
551     $h{"orange"} = "orange" ;
552     $h{"banana"} = "yellow" ;
553     $h{"tomato"} = "red" ;
554
555     # Check for existence of a key
556     print "Banana Exists\n\n" if $h{"banana"} ;
557
558     # Delete a key/value pair.
559     delete $h{"apple"} ;
560
561     # print the contents of the file
562     while (($k, $v) = each %h)
563       { print "$k -> $v\n" }
564
565     untie %h ;
566
567 here is the output:
568
569     Banana Exists
570  
571     orange -> orange
572     tomato -> red
573     banana -> yellow
574
575 Note that the like ordinary associative arrays, the order of the keys
576 retrieved is in an apparently random order.
577
578 =head1 DB_BTREE
579
580 The DB_BTREE format is useful when you want to store data in a given
581 order. By default the keys will be stored in lexical order, but as you
582 will see from the example shown in the next section, it is very easy to
583 define your own sorting function.
584
585 =head2 Changing the BTREE sort order
586
587 This script shows how to override the default sorting algorithm that
588 BTREE uses. Instead of using the normal lexical ordering, a case
589 insensitive compare function will be used.
590
591     use strict ;
592     use DB_File ;
593
594     my %h ;
595
596     sub Compare
597     {
598         my ($key1, $key2) = @_ ;
599         "\L$key1" cmp "\L$key2" ;
600     }
601
602     # specify the Perl sub that will do the comparison
603     $DB_BTREE->{'compare'} = \&Compare ;
604
605     tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0640, $DB_BTREE 
606         or die "Cannot open file 'tree': $!\n" ;
607
608     # Add a key/value pair to the file
609     $h{'Wall'} = 'Larry' ;
610     $h{'Smith'} = 'John' ;
611     $h{'mouse'} = 'mickey' ;
612     $h{'duck'}  = 'donald' ;
613
614     # Delete
615     delete $h{"duck"} ;
616
617     # Cycle through the keys printing them in order.
618     # Note it is not necessary to sort the keys as
619     # the btree will have kept them in order automatically.
620     foreach (keys %h)
621       { print "$_\n" }
622
623     untie %h ;
624
625 Here is the output from the code above.
626
627     mouse
628     Smith
629     Wall
630
631 There are a few point to bear in mind if you want to change the
632 ordering in a BTREE database:
633
634 =over 5
635
636 =item 1.
637
638 The new compare function must be specified when you create the database.
639
640 =item 2.
641
642 You cannot change the ordering once the database has been created. Thus
643 you must use the same compare function every time you access the
644 database.
645
646 =back 
647
648 =head2 Handling duplicate keys 
649
650 The BTREE file type optionally allows a single key to be associated
651 with an arbitrary number of values. This option is enabled by setting
652 the flags element of C<$DB_BTREE> to R_DUP when creating the database.
653
654 There are some difficulties in using the tied hash interface if you
655 want to manipulate a BTREE database with duplicate keys. Consider this
656 code:
657
658     use strict ;
659     use DB_File ;
660
661     use vars qw($filename %h ) ;
662
663     $filename = "tree" ;
664     unlink $filename ;
665  
666     # Enable duplicate records
667     $DB_BTREE->{'flags'} = R_DUP ;
668  
669     tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE 
670         or die "Cannot open $filename: $!\n";
671  
672     # Add some key/value pairs to the file
673     $h{'Wall'} = 'Larry' ;
674     $h{'Wall'} = 'Brick' ; # Note the duplicate key
675     $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
676     $h{'Smith'} = 'John' ;
677     $h{'mouse'} = 'mickey' ;
678
679     # iterate through the associative array
680     # and print each key/value pair.
681     foreach (keys %h)
682       { print "$_  -> $h{$_}\n" }
683
684     untie %h ;
685
686 Here is the output:
687
688     Smith   -> John
689     Wall    -> Larry
690     Wall    -> Larry
691     Wall    -> Larry
692     mouse   -> mickey
693
694 As you can see 3 records have been successfully created with key C<Wall>
695 - the only thing is, when they are retrieved from the database they
696 I<seem> to have the same value, namely C<Larry>. The problem is caused
697 by the way that the associative array interface works. Basically, when
698 the associative array interface is used to fetch the value associated
699 with a given key, it will only ever retrieve the first value.
700
701 Although it may not be immediately obvious from the code above, the
702 associative array interface can be used to write values with duplicate
703 keys, but it cannot be used to read them back from the database.
704
705 The way to get around this problem is to use the Berkeley DB API method
706 called C<seq>.  This method allows sequential access to key/value
707 pairs. See L<THE API INTERFACE> for details of both the C<seq> method
708 and the API in general.
709
710 Here is the script above rewritten using the C<seq> API method.
711
712     use strict ;
713     use DB_File ;
714  
715     use vars qw($filename $x %h $status $key $value) ;
716
717     $filename = "tree" ;
718     unlink $filename ;
719  
720     # Enable duplicate records
721     $DB_BTREE->{'flags'} = R_DUP ;
722  
723     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE 
724         or die "Cannot open $filename: $!\n";
725  
726     # Add some key/value pairs to the file
727     $h{'Wall'} = 'Larry' ;
728     $h{'Wall'} = 'Brick' ; # Note the duplicate key
729     $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
730     $h{'Smith'} = 'John' ;
731     $h{'mouse'} = 'mickey' ;
732  
733     # iterate through the btree using seq
734     # and print each key/value pair.
735     $key = $value = 0 ;
736     for ($status = $x->seq($key, $value, R_FIRST) ;
737          $status == 0 ;
738          $status = $x->seq($key, $value, R_NEXT) )
739       {  print "$key -> $value\n" }
740  
741     undef $x ;
742     untie %h ;
743
744 that prints:
745
746     Smith   -> John
747     Wall    -> Brick
748     Wall    -> Brick
749     Wall    -> Larry
750     mouse   -> mickey
751
752 This time we have got all the key/value pairs, including the multiple
753 values associated with the key C<Wall>.
754
755 =head2 The get_dup method.
756
757 B<DB_File> comes with a utility method, called C<get_dup>, to assist in
758 reading duplicate values from BTREE databases. The method can take the
759 following forms:
760
761     $count = $x->get_dup($key) ;
762     @list  = $x->get_dup($key) ;
763     %list  = $x->get_dup($key, 1) ;
764
765 In a scalar context the method returns the number of values associated
766 with the key, C<$key>.
767
768 In list context, it returns all the values which match C<$key>. Note
769 that the values will be returned in an apparently random order.
770
771 In list context, if the second parameter is present and evaluates TRUE,
772 the method returns an associative array. The keys of the associative
773 array correspond to the the values that matched in the BTREE and the
774 values of the array are a count of the number of times that particular
775 value occurred in the BTREE.
776
777 So assuming the database created above, we can use C<get_dup> like
778 this:
779
780     my $cnt  = $x->get_dup("Wall") ;
781     print "Wall occurred $cnt times\n" ;
782
783     my %hash = $x->get_dup("Wall", 1) ;
784     print "Larry is there\n" if $hash{'Larry'} ;
785     print "There are $hash{'Brick'} Brick Walls\n" ;
786
787     my @list = $x->get_dup("Wall") ;
788     print "Wall =>      [@list]\n" ;
789
790     @list = $x->get_dup("Smith") ;
791     print "Smith =>     [@list]\n" ;
792  
793     @list = $x->get_dup("Dog") ;
794     print "Dog =>       [@list]\n" ;
795
796
797 and it will print:
798
799     Wall occurred 3 times
800     Larry is there
801     There are 2 Brick Walls
802     Wall =>     [Brick Brick Larry]
803     Smith =>    [John]
804     Dog =>      []
805
806 =head2 Matching Partial Keys 
807
808 The BTREE interface has a feature which allows partial keys to be
809 matched. This functionality is I<only> available when the C<seq> method
810 is used along with the R_CURSOR flag.
811
812     $x->seq($key, $value, R_CURSOR) ;
813
814 Here is the relevant quote from the dbopen man page where it defines
815 the use of the R_CURSOR flag with seq:
816
817     Note, for the DB_BTREE access method, the returned key is not
818     necessarily an exact match for the specified key. The returned key
819     is the smallest key greater than or equal to the specified key,
820     permitting partial key matches and range searches.
821
822 In the example script below, the C<match> sub uses this feature to find
823 and print the first matching key/value pair given a partial key.
824
825     use strict ;
826     use DB_File ;
827     use Fcntl ;
828
829     use vars qw($filename $x %h $st $key $value) ;
830
831     sub match
832     {
833         my $key = shift ;
834         my $value = 0;
835         my $orig_key = $key ;
836         $x->seq($key, $value, R_CURSOR) ;
837         print "$orig_key\t-> $key\t-> $value\n" ;
838     }
839
840     $filename = "tree" ;
841     unlink $filename ;
842
843     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0640, $DB_BTREE
844         or die "Cannot open $filename: $!\n";
845  
846     # Add some key/value pairs to the file
847     $h{'mouse'} = 'mickey' ;
848     $h{'Wall'} = 'Larry' ;
849     $h{'Walls'} = 'Brick' ; 
850     $h{'Smith'} = 'John' ;
851  
852
853     $key = $value = 0 ;
854     print "IN ORDER\n" ;
855     for ($st = $x->seq($key, $value, R_FIRST) ;
856          $st == 0 ;
857          $st = $x->seq($key, $value, R_NEXT) )
858         
859       {  print "$key -> $value\n" }
860  
861     print "\nPARTIAL MATCH\n" ;
862
863     match "Wa" ;
864     match "A" ;
865     match "a" ;
866
867     undef $x ;
868     untie %h ;
869
870 Here is the output:
871
872     IN ORDER
873     Smith -> John
874     Wall  -> Larry
875     Walls -> Brick
876     mouse -> mickey
877
878     PARTIAL MATCH
879     Wa -> Wall  -> Larry
880     A  -> Smith -> John
881     a  -> mouse -> mickey
882
883 =head1 DB_RECNO
884
885 DB_RECNO provides an interface to flat text files. Both variable and
886 fixed length records are supported.
887
888 In order to make RECNO more compatible with Perl the array offset for
889 all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
890
891 As with normal Perl arrays, a RECNO array can be accessed using
892 negative indexes. The index -1 refers to the last element of the array,
893 -2 the second last, and so on. Attempting to access an element before
894 the start of the array will raise a fatal run-time error.
895
896 =head2 The bval option
897
898 The operation of the bval option warrants some discussion. Here is the
899 definition of bval from the Berkeley DB 1.85 recno manual page:
900
901     The delimiting byte to be used to mark  the  end  of  a
902     record for variable-length records, and the pad charac-
903     ter for fixed-length records.  If no  value  is  speci-
904     fied,  newlines  (``\n'')  are  used to mark the end of
905     variable-length records and  fixed-length  records  are
906     padded with spaces.
907
908 The second sentence is wrong. In actual fact bval will only default to
909 C<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULL
910 openinfo parameter is used at all, the value that happens to be in bval
911 will be used. That means you always have to specify bval when making
912 use of any of the options in the openinfo parameter. This documentation
913 error will be fixed in the next release of Berkeley DB.
914
915 That clarifies the situation with regards Berkeley DB itself. What
916 about B<DB_File>? Well, the behavior defined in the quote above is
917 quite useful, so B<DB_File> conforms it.
918
919 That means that you can specify other options (e.g. cachesize) and
920 still have bval default to C<"\n"> for variable length records, and
921 space for fixed length records.
922
923 =head2 A Simple Example
924
925 Here is a simple example that uses RECNO.
926
927     use strict ;
928     use DB_File ;
929
930     my @h ;
931     tie @h, "DB_File", "text", O_RDWR|O_CREAT, 0640, $DB_RECNO 
932         or die "Cannot open file 'text': $!\n" ;
933
934     # Add a few key/value pairs to the file
935     $h[0] = "orange" ;
936     $h[1] = "blue" ;
937     $h[2] = "yellow" ;
938
939     # Check for existence of a key
940     print "Element 1 Exists with value $h[1]\n" if $h[1] ;
941
942     # use a negative index
943     print "The last element is $h[-1]\n" ;
944     print "The 2nd last element is $h[-2]\n" ;
945
946     untie @h ;
947
948 Here is the output from the script:
949
950
951     Element 1 Exists with value blue
952     The last element is yellow
953     The 2nd last element is blue
954
955 =head2 Extra Methods
956
957 As you can see from the example above, the tied array interface is
958 quite limited. To make the interface more useful, a number of methods
959 are supplied with B<DB_File> to simulate the standard array operations
960 that are not currently implemented in Perl's tied array interface. All
961 these methods are accessed via the object returned from the tie call.
962
963 Here are the methods:
964
965 =over 5
966
967 =item B<$X-E<gt>push(list) ;>
968
969 Pushes the elements of C<list> to the end of the array.
970
971 =item B<$value = $X-E<gt>pop ;>
972
973 Removes and returns the last element of the array.
974
975 =item B<$X-E<gt>shift>
976
977 Removes and returns the first element of the array.
978
979 =item B<$X-E<gt>unshift(list) ;>
980
981 Pushes the elements of C<list> to the start of the array.
982
983 =item B<$X-E<gt>length>
984
985 Returns the number of elements in the array.
986
987 =back
988
989 =head2 Another Example
990
991 Here is a more complete example that makes use of some of the methods
992 described above. It also makes use of the API interface directly (see 
993 L<THE API INTERFACE>).
994
995     use strict ;
996     use vars qw(@h $H $file $i) ;
997     use DB_File ;
998     use Fcntl ;
999     
1000     $file = "text" ;
1001
1002     unlink $file ;
1003
1004     $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0640, $DB_RECNO 
1005         or die "Cannot open file $file: $!\n" ;
1006     
1007     # first create a text file to play with
1008     $h[0] = "zero" ;
1009     $h[1] = "one" ;
1010     $h[2] = "two" ;
1011     $h[3] = "three" ;
1012     $h[4] = "four" ;
1013
1014     
1015     # Print the records in order.
1016     #
1017     # The length method is needed here because evaluating a tied
1018     # array in a scalar context does not return the number of
1019     # elements in the array.  
1020
1021     print "\nORIGINAL\n" ;
1022     foreach $i (0 .. $H->length - 1) {
1023         print "$i: $h[$i]\n" ;
1024     }
1025
1026     # use the push & pop methods
1027     $a = $H->pop ;
1028     $H->push("last") ;
1029     print "\nThe last record was [$a]\n" ;
1030
1031     # and the shift & unshift methods
1032     $a = $H->shift ;
1033     $H->unshift("first") ;
1034     print "The first record was [$a]\n" ;
1035
1036     # Use the API to add a new record after record 2.
1037     $i = 2 ;
1038     $H->put($i, "Newbie", R_IAFTER) ;
1039
1040     # and a new record before record 1.
1041     $i = 1 ;
1042     $H->put($i, "New One", R_IBEFORE) ;
1043
1044     # delete record 3
1045     $H->del(3) ;
1046
1047     # now print the records in reverse order
1048     print "\nREVERSE\n" ;
1049     for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
1050       { print "$i: $h[$i]\n" }
1051
1052     # same again, but use the API functions instead
1053     print "\nREVERSE again\n" ;
1054     my ($s, $k, $v)  = (0, 0, 0) ;
1055     for ($s = $H->seq($k, $v, R_LAST) ; 
1056              $s == 0 ; 
1057              $s = $H->seq($k, $v, R_PREV))
1058       { print "$k: $v\n" }
1059
1060     undef $H ;
1061     untie @h ;
1062
1063 and this is what it outputs:
1064
1065     ORIGINAL
1066     0: zero
1067     1: one
1068     2: two
1069     3: three
1070     4: four
1071
1072     The last record was [four]
1073     The first record was [zero]
1074
1075     REVERSE
1076     5: last
1077     4: three
1078     3: Newbie
1079     2: one
1080     1: New One
1081     0: first
1082
1083     REVERSE again
1084     5: last
1085     4: three
1086     3: Newbie
1087     2: one
1088     1: New One
1089     0: first
1090
1091 Notes:
1092
1093 =over 5
1094
1095 =item 1.
1096
1097 Rather than iterating through the array, C<@h> like this:
1098
1099     foreach $i (@h)
1100
1101 it is necessary to use either this:
1102
1103     foreach $i (0 .. $H->length - 1) 
1104
1105 or this:
1106
1107     for ($a = $H->get($k, $v, R_FIRST) ;
1108          $a == 0 ;
1109          $a = $H->get($k, $v, R_NEXT) )
1110
1111 =item 2.
1112
1113 Notice that both times the C<put> method was used the record index was
1114 specified using a variable, C<$i>, rather than the literal value
1115 itself. This is because C<put> will return the record number of the
1116 inserted line via that parameter.
1117
1118 =back
1119
1120 =head1 THE API INTERFACE
1121
1122 As well as accessing Berkeley DB using a tied hash or array, it is also
1123 possible to make direct use of most of the API functions defined in the
1124 Berkeley DB documentation.
1125
1126 To do this you need to store a copy of the object returned from the tie.
1127
1128         $db = tie %hash, "DB_File", "filename" ;
1129
1130 Once you have done that, you can access the Berkeley DB API functions
1131 as B<DB_File> methods directly like this:
1132
1133         $db->put($key, $value, R_NOOVERWRITE) ;
1134
1135 B<Important:> If you have saved a copy of the object returned from
1136 C<tie>, the underlying database file will I<not> be closed until both
1137 the tied variable is untied and all copies of the saved object are
1138 destroyed. 
1139
1140     use DB_File ;
1141     $db = tie %hash, "DB_File", "filename" 
1142         or die "Cannot tie filename: $!" ;
1143     ...
1144     undef $db ;
1145     untie %hash ;
1146
1147 All the functions defined in L<dbopen> are available except for
1148 close() and dbopen() itself. The B<DB_File> method interface to the
1149 supported functions have been implemented to mirror the way Berkeley DB
1150 works whenever possible. In particular note that:
1151
1152 =over 5
1153
1154 =item *
1155
1156 The methods return a status value. All return 0 on success.
1157 All return -1 to signify an error and set C<$!> to the exact
1158 error code. The return code 1 generally (but not always) means that the
1159 key specified did not exist in the database.
1160
1161 Other return codes are defined. See below and in the Berkeley DB
1162 documentation for details. The Berkeley DB documentation should be used
1163 as the definitive source.
1164
1165 =item *
1166
1167 Whenever a Berkeley DB function returns data via one of its parameters,
1168 the equivalent B<DB_File> method does exactly the same.
1169
1170 =item *
1171
1172 If you are careful, it is possible to mix API calls with the tied
1173 hash/array interface in the same piece of code. Although only a few of
1174 the methods used to implement the tied interface currently make use of
1175 the cursor, you should always assume that the cursor has been changed
1176 any time the tied hash/array interface is used. As an example, this
1177 code will probably not do what you expect:
1178
1179     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1180         or die "Cannot tie $filename: $!" ;
1181
1182     # Get the first key/value pair and set  the cursor
1183     $X->seq($key, $value, R_FIRST) ;
1184
1185     # this line will modify the cursor
1186     $count = scalar keys %x ; 
1187
1188     # Get the second key/value pair.
1189     # oops, it didn't, it got the last key/value pair!
1190     $X->seq($key, $value, R_NEXT) ;
1191
1192 The code above can be rearranged to get around the problem, like this:
1193
1194     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1195         or die "Cannot tie $filename: $!" ;
1196
1197     # this line will modify the cursor
1198     $count = scalar keys %x ; 
1199
1200     # Get the first key/value pair and set  the cursor
1201     $X->seq($key, $value, R_FIRST) ;
1202
1203     # Get the second key/value pair.
1204     # worked this time.
1205     $X->seq($key, $value, R_NEXT) ;
1206
1207 =back
1208
1209 All the constants defined in L<dbopen> for use in the flags parameters
1210 in the methods defined below are also available. Refer to the Berkeley
1211 DB documentation for the precise meaning of the flags values.
1212
1213 Below is a list of the methods available.
1214
1215 =over 5
1216
1217 =item B<$status = $X-E<gt>get($key, $value [, $flags]) ;>
1218
1219 Given a key (C<$key>) this method reads the value associated with it
1220 from the database. The value read from the database is returned in the
1221 C<$value> parameter.
1222
1223 If the key does not exist the method returns 1.
1224
1225 No flags are currently defined for this method.
1226
1227 =item B<$status = $X-E<gt>put($key, $value [, $flags]) ;>
1228
1229 Stores the key/value pair in the database.
1230
1231 If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter
1232 will have the record number of the inserted key/value pair set.
1233
1234 Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
1235 R_SETCURSOR.
1236
1237 =item B<$status = $X-E<gt>del($key [, $flags]) ;>
1238
1239 Removes all key/value pairs with key C<$key> from the database.
1240
1241 A return code of 1 means that the requested key was not in the
1242 database.
1243
1244 R_CURSOR is the only valid flag at present.
1245
1246 =item B<$status = $X-E<gt>fd ;>
1247
1248 Returns the file descriptor for the underlying database.
1249
1250 See L<Locking Databases> for an example of how to make use of the
1251 C<fd> method to lock your database.
1252
1253 =item B<$status = $X-E<gt>seq($key, $value, $flags) ;>
1254
1255 This interface allows sequential retrieval from the database. See
1256 L<dbopen> for full details.
1257
1258 Both the C<$key> and C<$value> parameters will be set to the key/value
1259 pair read from the database.
1260
1261 The flags parameter is mandatory. The valid flag values are R_CURSOR,
1262 R_FIRST, R_LAST, R_NEXT and R_PREV.
1263
1264 =item B<$status = $X-E<gt>sync([$flags]) ;>
1265
1266 Flushes any cached buffers to disk.
1267
1268 R_RECNOSYNC is the only valid flag at present.
1269
1270 =back
1271
1272 =head1 HINTS AND TIPS 
1273
1274
1275 =head2 Locking Databases
1276
1277 Concurrent access of a read-write database by several parties requires
1278 them all to use some kind of locking.  Here's an example of Tom's that
1279 uses the I<fd> method to get the file descriptor, and then a careful
1280 open() to give something Perl will flock() for you.  Run this repeatedly
1281 in the background to watch the locks granted in proper order.
1282
1283     use DB_File;
1284
1285     use strict;
1286
1287     sub LOCK_SH { 1 }
1288     sub LOCK_EX { 2 }
1289     sub LOCK_NB { 4 }
1290     sub LOCK_UN { 8 }
1291
1292     my($oldval, $fd, $db, %db, $value, $key);
1293
1294     $key = shift || 'default';
1295     $value = shift || 'magic';
1296
1297     $value .= " $$";
1298
1299     $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644) 
1300             || die "dbcreat /tmp/foo.db $!";
1301     $fd = $db->fd;
1302     print "$$: db fd is $fd\n";
1303     open(DB_FH, "+<&=$fd") || die "dup $!";
1304
1305
1306     unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {
1307         print "$$: CONTENTION; can't read during write update!
1308                     Waiting for read lock ($!) ....";
1309         unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" }
1310     } 
1311     print "$$: Read lock granted\n";
1312
1313     $oldval = $db{$key};
1314     print "$$: Old value was $oldval\n";
1315     flock(DB_FH, LOCK_UN);
1316
1317     unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
1318         print "$$: CONTENTION; must have exclusive lock!
1319                     Waiting for write lock ($!) ....";
1320         unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" }
1321     } 
1322
1323     print "$$: Write lock granted\n";
1324     $db{$key} = $value;
1325     $db->sync;  # to flush
1326     sleep 10;
1327
1328     flock(DB_FH, LOCK_UN);
1329     undef $db;
1330     untie %db;
1331     close(DB_FH);
1332     print "$$: Updated db to $key=$value\n";
1333
1334 =head2 Sharing databases with C applications
1335
1336 There is no technical reason why a Berkeley DB database cannot be
1337 shared by both a Perl and a C application.
1338
1339 The vast majority of problems that are reported in this area boil down
1340 to the fact that C strings are NULL terminated, whilst Perl strings are
1341 not. 
1342
1343 Here is a real example. Netscape 2.0 keeps a record of the locations you
1344 visit along with the time you last visited them in a DB_HASH database.
1345 This is usually stored in the file F<~/.netscape/history.db>. The key
1346 field in the database is the location string and the value field is the
1347 time the location was last visited stored as a 4 byte binary value.
1348
1349 If you haven't already guessed, the location string is stored with a
1350 terminating NULL. This means you need to be careful when accessing the
1351 database.
1352
1353 Here is a snippet of code that is loosely based on Tom Christiansen's
1354 I<ggh> script (available from your nearest CPAN archive in
1355 F<authors/id/TOMC/scripts/nshist.gz>).
1356
1357     use strict ;
1358     use DB_File ;
1359     use Fcntl ;
1360
1361     use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ;
1362     $dotdir = $ENV{HOME} || $ENV{LOGNAME};
1363
1364     $HISTORY = "$dotdir/.netscape/history.db";
1365
1366     tie %hist_db, 'DB_File', $HISTORY
1367         or die "Cannot open $HISTORY: $!\n" ;;
1368
1369     # Dump the complete database
1370     while ( ($href, $binary_time) = each %hist_db ) {
1371
1372         # remove the terminating NULL
1373         $href =~ s/\x00$// ;
1374
1375         # convert the binary time into a user friendly string
1376         $date = localtime unpack("V", $binary_time);
1377         print "$date $href\n" ;
1378     }
1379
1380     # check for the existence of a specific key
1381     # remember to add the NULL
1382     if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) {
1383         $date = localtime unpack("V", $binary_time) ;
1384         print "Last visited mox.perl.com on $date\n" ;
1385     }
1386     else {
1387         print "Never visited mox.perl.com\n"
1388     }
1389
1390     untie %hist_db ;
1391
1392
1393 =head1 COMMON QUESTIONS
1394
1395 =head2 Why is there Perl source in my database?
1396
1397 If you look at the contents of a database file created by DB_File,
1398 there can sometimes be part of a Perl script included in it.
1399
1400 This happens because Berkeley DB uses dynamic memory to allocate
1401 buffers which will subsequently be written to the database file. Being
1402 dynamic, the memory could have been used for anything before DB
1403 malloced it. As Berkeley DB doesn't clear the memory once it has been
1404 allocated, the unused portions will contain random junk. In the case
1405 where a Perl script gets written to the database, the random junk will
1406 correspond to an area of dynamic memory that happened to be used during
1407 the compilation of the script.
1408
1409 Unless you don't like the possibility of there being part of your Perl
1410 scripts embedded in a database file, this is nothing to worry about.
1411
1412 =head2 How do I store complex data structures with DB_File?
1413
1414 Although B<DB_File> cannot do this directly, there is a module which
1415 can layer transparently over B<DB_File> to accomplish this feat.
1416
1417 Check out the MLDBM module, available on CPAN in the directory
1418 F<modules/by-module/MLDBM>.
1419
1420 =head2 What does "Invalid Argument" mean?
1421
1422 You will get this error message when one of the parameters in the
1423 C<tie> call is wrong. Unfortunately there are quite a few parameters to
1424 get wrong, so it can be difficult to figure out which one it is.
1425
1426 Here are a couple of possibilities:
1427
1428 =over 5
1429
1430 =item 1.
1431
1432 Attempting to reopen a database without closing it. 
1433
1434 =item 2.
1435
1436 Using the O_WRONLY flag.
1437
1438 =back
1439
1440 =head2 What does "Bareword 'DB_File' not allowed" mean? 
1441
1442 You will encounter this particular error message when you have the
1443 C<strict 'subs'> pragma (or the full strict pragma) in your script.
1444 Consider this script:
1445
1446     use strict ;
1447     use DB_File ;
1448     use vars qw(%x) ;
1449     tie %x, DB_File, "filename" ;
1450
1451 Running it produces the error in question:
1452
1453     Bareword "DB_File" not allowed while "strict subs" in use 
1454
1455 To get around the error, place the word C<DB_File> in either single or
1456 double quotes, like this:
1457
1458     tie %x, "DB_File", "filename" ;
1459
1460 Although it might seem like a real pain, it is really worth the effort
1461 of having a C<use strict> in all your scripts.
1462
1463 =head1 HISTORY
1464
1465 =over
1466
1467 =item 0.1
1468
1469 First Release.
1470
1471 =item 0.2
1472
1473 When B<DB_File> is opening a database file it no longer terminates the
1474 process if I<dbopen> returned an error. This allows file protection
1475 errors to be caught at run time. Thanks to Judith Grass
1476 E<lt>grass@cybercash.comE<gt> for spotting the bug.
1477
1478 =item 0.3
1479
1480 Added prototype support for multiple btree compare callbacks.
1481
1482 =item 1.0
1483
1484 B<DB_File> has been in use for over a year. To reflect that, the
1485 version number has been incremented to 1.0.
1486
1487 Added complete support for multiple concurrent callbacks.
1488
1489 Using the I<push> method on an empty list didn't work properly. This
1490 has been fixed.
1491
1492 =item 1.01
1493
1494 Fixed a core dump problem with SunOS.
1495
1496 The return value from TIEHASH wasn't set to NULL when dbopen returned
1497 an error.
1498
1499 =item 1.02
1500
1501 Merged OS/2 specific code into DB_File.xs
1502
1503 Removed some redundant code in DB_File.xs.
1504
1505 Documentation update.
1506
1507 Allow negative subscripts with RECNO interface.
1508
1509 Changed the default flags from O_RDWR to O_CREAT|O_RDWR.
1510
1511 The example code which showed how to lock a database needed a call to
1512 C<sync> added. Without it the resultant database file was empty.
1513
1514 Added get_dup method.
1515
1516 =item 1.03
1517
1518 Documentation update.
1519
1520 B<DB_File> now imports the constants (O_RDWR, O_CREAT etc.) from Fcntl
1521 automatically.
1522
1523 The standard hash function C<exists> is now supported.
1524
1525 Modified the behavior of get_dup. When it returns an associative
1526 array, the value is the count of the number of matching BTREE values.
1527
1528 =item 1.04
1529
1530 Minor documentation changes.
1531
1532 Fixed a bug in hash_cb. Patches supplied by Dave Hammen,
1533 E<lt>hammen@gothamcity.jsc.nasa.govE<gt>.
1534
1535 Fixed a bug with the constructors for DB_File::HASHINFO,
1536 DB_File::BTREEINFO and DB_File::RECNOINFO. Also tidied up the
1537 constructors to make them C<-w> clean.
1538
1539 Reworked part of the test harness to be more locale friendly.
1540
1541 =item 1.05
1542
1543 Made all scripts in the documentation C<strict> and C<-w> clean.
1544
1545 Added logic to F<DB_File.xs> to allow the module to be built after Perl
1546 is installed.
1547
1548 =item 1.06
1549
1550 Minor namespace cleanup: Localized C<PrintBtree>.
1551
1552 =item 1.07
1553
1554 Fixed bug with RECNO, where bval wasn't defaulting to "\n".
1555
1556 =item 1.08
1557
1558 Documented operation of bval.
1559
1560 =item 1.09
1561
1562 Minor bug fix in DB_File::HASHINFO, DB_File::RECNOINFO and
1563 DB_File::BTREEINFO.
1564
1565 Changed default mode to 0666.
1566
1567 =back
1568
1569 =head1 BUGS
1570
1571 Some older versions of Berkeley DB had problems with fixed length
1572 records using the RECNO file format. The newest version at the time of
1573 writing was 1.85 - this seems to have fixed the problems with RECNO.
1574
1575 I am sure there are bugs in the code. If you do find any, or can
1576 suggest any enhancements, I would welcome your comments.
1577
1578 =head1 AVAILABILITY
1579
1580 B<DB_File> comes with the standard Perl source distribution. Look in
1581 the directory F<ext/DB_File>.
1582
1583 Berkeley DB is available at your nearest CPAN archive (see
1584 L<perlmod/"CPAN"> for a list) in F<src/misc/db.1.85.tar.gz>, or via the
1585 host F<ftp.cs.berkeley.edu> in F</ucb/4bsd/db.tar.gz>.  Alternatively,
1586 check out the Berkeley DB home page at F<http://www.bostic.com/db>. It
1587 is I<not> under the GPL.
1588
1589 If you are running IRIX, then get Berkeley DB from
1590 F<http://reality.sgi.com/ariel>. It has the patches necessary to
1591 compile properly on IRIX 5.3.
1592
1593 =head1 SEE ALSO
1594
1595 L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)> 
1596
1597 =head1 AUTHOR
1598
1599 The DB_File interface was written by Paul Marquess
1600 E<lt>pmarquess@bfsec.bt.co.ukE<gt>.
1601 Questions about the DB system itself may be addressed to Keith Bostic
1602 E<lt>bostic@cs.berkeley.eduE<gt>.
1603
1604 =cut