289ac0a65c792313d7be0b17142768cbbb4cbaa0
[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 (Paul.Marquess@btinternet.com)
4 # last modified 30th July 2001
5 # version 1.78
6 #
7 #     Copyright (c) 1995-2001 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 warnings;
17 use strict;
18 use Carp;
19 require Tie::Hash;
20 @DB_File::HASHINFO::ISA = qw(Tie::Hash);
21
22 sub new
23 {
24     my $pkg = shift ;
25     my %x ;
26     tie %x, $pkg ;
27     bless \%x, $pkg ;
28 }
29
30
31 sub TIEHASH
32 {
33     my $pkg = shift ;
34
35     bless { VALID => { map {$_, 1} 
36                        qw( bsize ffactor nelem cachesize hash lorder)
37                      }, 
38             GOT   => {}
39           }, $pkg ;
40 }
41
42
43 sub FETCH 
44 {  
45     my $self  = shift ;
46     my $key   = shift ;
47
48     return $self->{GOT}{$key} if exists $self->{VALID}{$key}  ;
49
50     my $pkg = ref $self ;
51     croak "${pkg}::FETCH - Unknown element '$key'" ;
52 }
53
54
55 sub STORE 
56 {
57     my $self  = shift ;
58     my $key   = shift ;
59     my $value = shift ;
60
61     if ( exists $self->{VALID}{$key} )
62     {
63         $self->{GOT}{$key} = $value ;
64         return ;
65     }
66     
67     my $pkg = ref $self ;
68     croak "${pkg}::STORE - Unknown element '$key'" ;
69 }
70
71 sub DELETE 
72 {
73     my $self = shift ;
74     my $key  = shift ;
75
76     if ( exists $self->{VALID}{$key} )
77     {
78         delete $self->{GOT}{$key} ;
79         return ;
80     }
81     
82     my $pkg = ref $self ;
83     croak "DB_File::HASHINFO::DELETE - Unknown element '$key'" ;
84 }
85
86 sub EXISTS
87 {
88     my $self = shift ;
89     my $key  = shift ;
90
91     exists $self->{VALID}{$key} ;
92 }
93
94 sub NotHere
95 {
96     my $self = shift ;
97     my $method = shift ;
98
99     croak ref($self) . " does not define the method ${method}" ;
100 }
101
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 warnings;
109 use strict ;
110
111 @DB_File::RECNOINFO::ISA = qw(DB_File::HASHINFO) ;
112
113 sub TIEHASH
114 {
115     my $pkg = shift ;
116
117     bless { VALID => { map {$_, 1} 
118                        qw( bval cachesize psize flags lorder reclen bfname )
119                      },
120             GOT   => {},
121           }, $pkg ;
122 }
123
124 package DB_File::BTREEINFO ;
125
126 use warnings;
127 use strict ;
128
129 @DB_File::BTREEINFO::ISA = qw(DB_File::HASHINFO) ;
130
131 sub TIEHASH
132 {
133     my $pkg = shift ;
134
135     bless { VALID => { map {$_, 1} 
136                        qw( flags cachesize maxkeypage minkeypage psize 
137                            compare prefix lorder )
138                      },
139             GOT   => {},
140           }, $pkg ;
141 }
142
143
144 package DB_File ;
145
146 use warnings;
147 use strict;
148 use vars qw($VERSION @ISA @EXPORT $AUTOLOAD $DB_BTREE $DB_HASH $DB_RECNO 
149             $db_version $use_XSLoader
150            ) ;
151 use Carp;
152
153
154 $VERSION = "1.78" ;
155
156 #typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
157 $DB_BTREE = new DB_File::BTREEINFO ;
158 $DB_HASH  = new DB_File::HASHINFO ;
159 $DB_RECNO = new DB_File::RECNOINFO ;
160
161 require Tie::Hash;
162 require Exporter;
163 use AutoLoader;
164 BEGIN {
165     $use_XSLoader = 1 ;
166     eval { require XSLoader } ;
167
168     if ($@) {
169         $use_XSLoader = 0 ;
170         require DynaLoader;
171         @ISA = qw(DynaLoader);
172     }
173 }
174
175 push @ISA, qw(Tie::Hash Exporter);
176 @EXPORT = qw(
177         $DB_BTREE $DB_HASH $DB_RECNO 
178
179         BTREEMAGIC
180         BTREEVERSION
181         DB_LOCK
182         DB_SHMEM
183         DB_TXN
184         HASHMAGIC
185         HASHVERSION
186         MAX_PAGE_NUMBER
187         MAX_PAGE_OFFSET
188         MAX_REC_NUMBER
189         RET_ERROR
190         RET_SPECIAL
191         RET_SUCCESS
192         R_CURSOR
193         R_DUP
194         R_FIRST
195         R_FIXEDLEN
196         R_IAFTER
197         R_IBEFORE
198         R_LAST
199         R_NEXT
200         R_NOKEY
201         R_NOOVERWRITE
202         R_PREV
203         R_RECNOSYNC
204         R_SETCURSOR
205         R_SNAPSHOT
206         __R_UNUSED
207
208 );
209
210 sub AUTOLOAD {
211     my($constname);
212     ($constname = $AUTOLOAD) =~ s/.*:://;
213     my $val = constant($constname, @_ ? $_[0] : 0);
214     if ($! != 0) {
215         if ($! =~ /Invalid/ || $!{EINVAL}) {
216             $AutoLoader::AUTOLOAD = $AUTOLOAD;
217             goto &AutoLoader::AUTOLOAD;
218         }
219         else {
220             my($pack,$file,$line) = caller;
221             croak "Your vendor has not defined DB macro $constname, used at $file line $line.
222 ";
223         }
224     }
225     eval "sub $AUTOLOAD { $val }";
226     goto &$AUTOLOAD;
227 }
228
229
230 eval {
231     # Make all Fcntl O_XXX constants available for importing
232     require Fcntl;
233     my @O = grep /^O_/, @Fcntl::EXPORT;
234     Fcntl->import(@O);  # first we import what we want to export
235     push(@EXPORT, @O);
236 };
237
238 if ($use_XSLoader)
239   { XSLoader::load("DB_File", $VERSION)}
240 else
241   { bootstrap DB_File $VERSION }
242
243 # Preloaded methods go here.  Autoload methods go after __END__, and are
244 # processed by the autosplit program.
245
246 sub tie_hash_or_array
247 {
248     my (@arg) = @_ ;
249     my $tieHASH = ( (caller(1))[3] =~ /TIEHASH/ ) ;
250
251     $arg[4] = tied %{ $arg[4] } 
252         if @arg >= 5 && ref $arg[4] && $arg[4] =~ /=HASH/ && tied %{ $arg[4] } ;
253
254     # make recno in Berkeley DB version 2 work like recno in version 1.
255     if ($db_version > 1 and defined $arg[4] and $arg[4] =~ /RECNO/ and 
256         $arg[1] and ! -e $arg[1]) {
257         open(FH, ">$arg[1]") or return undef ;
258         close FH ;
259         chmod $arg[3] ? $arg[3] : 0666 , $arg[1] ;
260     }
261
262     DoTie_($tieHASH, @arg) ;
263 }
264
265 sub TIEHASH
266 {
267     tie_hash_or_array(@_) ;
268 }
269
270 sub TIEARRAY
271 {
272     tie_hash_or_array(@_) ;
273 }
274
275 sub CLEAR 
276 {
277     my $self = shift;
278     my $key = 0 ;
279     my $value = "" ;
280     my $status = $self->seq($key, $value, R_FIRST());
281     my @keys;
282  
283     while ($status == 0) {
284         push @keys, $key;
285         $status = $self->seq($key, $value, R_NEXT());
286     }
287     foreach $key (reverse @keys) {
288         my $s = $self->del($key); 
289     }
290 }
291
292 sub EXTEND { }
293
294 sub STORESIZE
295 {
296     my $self = shift;
297     my $length = shift ;
298     my $current_length = $self->length() ;
299
300     if ($length < $current_length) {
301         my $key ;
302         for ($key = $current_length - 1 ; $key >= $length ; -- $key)
303           { $self->del($key) }
304     }
305     elsif ($length > $current_length) {
306         $self->put($length-1, "") ;
307     }
308 }
309  
310
311 sub SPLICE
312 {
313     my $self = shift;
314     my $offset = shift;
315     if (not defined $offset) {
316         carp 'Use of uninitialized value in splice';
317         $offset = 0;
318     }
319
320     my $length = @_ ? shift : 0;
321     # Carping about definedness comes _after_ the OFFSET sanity check.
322     # This is so we get the same error messages as Perl's splice().
323     # 
324
325     my @list = @_;
326
327     my $size = $self->FETCHSIZE();
328     
329     # 'If OFFSET is negative then it start that far from the end of
330     # the array.'
331     # 
332     if ($offset < 0) {
333         my $new_offset = $size + $offset;
334         if ($new_offset < 0) {
335             die "Modification of non-creatable array value attempted, "
336               . "subscript $offset";
337         }
338         $offset = $new_offset;
339     }
340
341     if ($offset > $size) {
342         $offset = $size;
343     }
344
345     if (not defined $length) {
346         carp 'Use of uninitialized value in splice';
347         $length = 0;
348     }
349
350     # 'If LENGTH is omitted, removes everything from OFFSET onward.'
351     if (not defined $length) {
352         $length = $size - $offset;
353     }
354
355     # 'If LENGTH is negative, leave that many elements off the end of
356     # the array.'
357     # 
358     if ($length < 0) {
359         $length = $size - $offset + $length;
360
361         if ($length < 0) {
362             # The user must have specified a length bigger than the
363             # length of the array passed in.  But perl's splice()
364             # doesn't catch this, it just behaves as for length=0.
365             # 
366             $length = 0;
367         }
368     }
369
370     if ($length > $size - $offset) {
371         $length = $size - $offset;
372     }
373
374     # $num_elems holds the current number of elements in the database.
375     my $num_elems = $size;
376
377     # 'Removes the elements designated by OFFSET and LENGTH from an
378     # array,'...
379     # 
380     my @removed = ();
381     foreach (0 .. $length - 1) {
382         my $old;
383         my $status = $self->get($offset, $old);
384         if ($status != 0) {
385             my $msg = "error from Berkeley DB on get($offset, \$old)";
386             if ($status == 1) {
387                 $msg .= ' (no such element?)';
388             }
389             else {
390                 $msg .= ": error status $status";
391                 if (defined $! and $! ne '') {
392                     $msg .= ", message $!";
393                 }
394             }
395             die $msg;
396         }
397         push @removed, $old;
398
399         $status = $self->del($offset);
400         if ($status != 0) {
401             my $msg = "error from Berkeley DB on del($offset)";
402             if ($status == 1) {
403                 $msg .= ' (no such element?)';
404             }
405             else {
406                 $msg .= ": error status $status";
407                 if (defined $! and $! ne '') {
408                     $msg .= ", message $!";
409                 }
410             }
411             die $msg;
412         }
413
414         -- $num_elems;
415     }
416
417     # ...'and replaces them with the elements of LIST, if any.'
418     my $pos = $offset;
419     while (defined (my $elem = shift @list)) {
420         my $old_pos = $pos;
421         my $status;
422         if ($pos >= $num_elems) {
423             $status = $self->put($pos, $elem);
424         }
425         else {
426             $status = $self->put($pos, $elem, $self->R_IBEFORE);
427         }
428
429         if ($status != 0) {
430             my $msg = "error from Berkeley DB on put($pos, $elem, ...)";
431             if ($status == 1) {
432                 $msg .= ' (no such element?)';
433             }
434             else {
435                 $msg .= ", error status $status";
436                 if (defined $! and $! ne '') {
437                     $msg .= ", message $!";
438                 }
439             }
440             die $msg;
441         }
442
443         die "pos unexpectedly changed from $old_pos to $pos with R_IBEFORE"
444           if $old_pos != $pos;
445
446         ++ $pos;
447         ++ $num_elems;
448     }
449
450     if (wantarray) {
451         # 'In list context, returns the elements removed from the
452         # array.'
453         # 
454         return @removed;
455     }
456     elsif (defined wantarray and not wantarray) {
457         # 'In scalar context, returns the last element removed, or
458         # undef if no elements are removed.'
459         # 
460         if (@removed) {
461             my $last = pop @removed;
462             return "$last";
463         }
464         else {
465             return undef;
466         }
467     }
468     elsif (not defined wantarray) {
469         # Void context
470     }
471     else { die }
472 }
473 sub ::DB_File::splice { &SPLICE }
474
475 sub find_dup
476 {
477     croak "Usage: \$db->find_dup(key,value)\n"
478         unless @_ == 3 ;
479  
480     my $db        = shift ;
481     my ($origkey, $value_wanted) = @_ ;
482     my ($key, $value) = ($origkey, 0);
483     my ($status) = 0 ;
484
485     for ($status = $db->seq($key, $value, R_CURSOR() ) ;
486          $status == 0 ;
487          $status = $db->seq($key, $value, R_NEXT() ) ) {
488
489         return 0 if $key eq $origkey and $value eq $value_wanted ;
490     }
491
492     return $status ;
493 }
494
495 sub del_dup
496 {
497     croak "Usage: \$db->del_dup(key,value)\n"
498         unless @_ == 3 ;
499  
500     my $db        = shift ;
501     my ($key, $value) = @_ ;
502     my ($status) = $db->find_dup($key, $value) ;
503     return $status if $status != 0 ;
504
505     $status = $db->del($key, R_CURSOR() ) ;
506     return $status ;
507 }
508
509 sub get_dup
510 {
511     croak "Usage: \$db->get_dup(key [,flag])\n"
512         unless @_ == 2 or @_ == 3 ;
513  
514     my $db        = shift ;
515     my $key       = shift ;
516     my $flag      = shift ;
517     my $value     = 0 ;
518     my $origkey   = $key ;
519     my $wantarray = wantarray ;
520     my %values    = () ;
521     my @values    = () ;
522     my $counter   = 0 ;
523     my $status    = 0 ;
524  
525     # iterate through the database until either EOF ($status == 0)
526     # or a different key is encountered ($key ne $origkey).
527     for ($status = $db->seq($key, $value, R_CURSOR()) ;
528          $status == 0 and $key eq $origkey ;
529          $status = $db->seq($key, $value, R_NEXT()) ) {
530  
531         # save the value or count number of matches
532         if ($wantarray) {
533             if ($flag)
534                 { ++ $values{$value} }
535             else
536                 { push (@values, $value) }
537         }
538         else
539             { ++ $counter }
540      
541     }
542  
543     return ($wantarray ? ($flag ? %values : @values) : $counter) ;
544 }
545
546
547 1;
548 __END__
549
550 =head1 NAME
551
552 DB_File - Perl5 access to Berkeley DB version 1.x
553
554 =head1 SYNOPSIS
555
556  use DB_File;
557
558  [$X =] tie %hash,  'DB_File', [$filename, $flags, $mode, $DB_HASH] ;
559  [$X =] tie %hash,  'DB_File', $filename, $flags, $mode, $DB_BTREE ;
560  [$X =] tie @array, 'DB_File', $filename, $flags, $mode, $DB_RECNO ;
561
562  $status = $X->del($key [, $flags]) ;
563  $status = $X->put($key, $value [, $flags]) ;
564  $status = $X->get($key, $value [, $flags]) ;
565  $status = $X->seq($key, $value, $flags) ;
566  $status = $X->sync([$flags]) ;
567  $status = $X->fd ;
568
569  # BTREE only
570  $count = $X->get_dup($key) ;
571  @list  = $X->get_dup($key) ;
572  %list  = $X->get_dup($key, 1) ;
573  $status = $X->find_dup($key, $value) ;
574  $status = $X->del_dup($key, $value) ;
575
576  # RECNO only
577  $a = $X->length;
578  $a = $X->pop ;
579  $X->push(list);
580  $a = $X->shift;
581  $X->unshift(list);
582  @r = $X->splice(offset, length, elements);
583
584  # DBM Filters
585  $old_filter = $db->filter_store_key  ( sub { ... } ) ;
586  $old_filter = $db->filter_store_value( sub { ... } ) ;
587  $old_filter = $db->filter_fetch_key  ( sub { ... } ) ;
588  $old_filter = $db->filter_fetch_value( sub { ... } ) ;
589
590  untie %hash ;
591  untie @array ;
592
593 =head1 DESCRIPTION
594
595 B<DB_File> is a module which allows Perl programs to make use of the
596 facilities provided by Berkeley DB version 1.x (if you have a newer
597 version of DB, see L<Using DB_File with Berkeley DB version 2 or 3>).
598 It is assumed that you have a copy of the Berkeley DB manual pages at
599 hand when reading this documentation. The interface defined here
600 mirrors the Berkeley DB interface closely.
601
602 Berkeley DB is a C library which provides a consistent interface to a
603 number of database formats.  B<DB_File> provides an interface to all
604 three of the database types currently supported by Berkeley DB.
605
606 The file types are:
607
608 =over 5
609
610 =item B<DB_HASH>
611
612 This database type allows arbitrary key/value pairs to be stored in data
613 files. This is equivalent to the functionality provided by other
614 hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
615 the files created using DB_HASH are not compatible with any of the
616 other packages mentioned.
617
618 A default hashing algorithm, which will be adequate for most
619 applications, is built into Berkeley DB. If you do need to use your own
620 hashing algorithm it is possible to write your own in Perl and have
621 B<DB_File> use it instead.
622
623 =item B<DB_BTREE>
624
625 The btree format allows arbitrary key/value pairs to be stored in a
626 sorted, balanced binary tree.
627
628 As with the DB_HASH format, it is possible to provide a user defined
629 Perl routine to perform the comparison of keys. By default, though, the
630 keys are stored in lexical order.
631
632 =item B<DB_RECNO>
633
634 DB_RECNO allows both fixed-length and variable-length flat text files
635 to be manipulated using the same key/value pair interface as in DB_HASH
636 and DB_BTREE.  In this case the key will consist of a record (line)
637 number.
638
639 =back
640
641 =head2 Using DB_File with Berkeley DB version 2 or 3
642
643 Although B<DB_File> is intended to be used with Berkeley DB version 1,
644 it can also be used with version 2 or 3. In this case the interface is
645 limited to the functionality provided by Berkeley DB 1.x. Anywhere the
646 version 2 or 3 interface differs, B<DB_File> arranges for it to work
647 like version 1. This feature allows B<DB_File> scripts that were built
648 with version 1 to be migrated to version 2 or 3 without any changes.
649
650 If you want to make use of the new features available in Berkeley DB
651 2.x or greater, use the Perl module B<BerkeleyDB> instead.
652
653 B<Note:> The database file format has changed in both Berkeley DB
654 version 2 and 3. If you cannot recreate your databases, you must dump
655 any existing databases with either the C<db_dump> or the C<db_dump185>
656 utility that comes with Berkeley DB.
657 Once you have rebuilt DB_File to use Berkeley DB version 2 or 3, your
658 databases can be recreated using C<db_load>. Refer to the Berkeley DB
659 documentation for further details.
660
661 Please read L<"COPYRIGHT"> before using version 2.x or 3.x of Berkeley
662 DB with DB_File.
663
664 =head2 Interface to Berkeley DB
665
666 B<DB_File> allows access to Berkeley DB files using the tie() mechanism
667 in Perl 5 (for full details, see L<perlfunc/tie()>). This facility
668 allows B<DB_File> to access Berkeley DB files using either an
669 associative array (for DB_HASH & DB_BTREE file types) or an ordinary
670 array (for the DB_RECNO file type).
671
672 In addition to the tie() interface, it is also possible to access most
673 of the functions provided in the Berkeley DB API directly.
674 See L<THE API INTERFACE>.
675
676 =head2 Opening a Berkeley DB Database File
677
678 Berkeley DB uses the function dbopen() to open or create a database.
679 Here is the C prototype for dbopen():
680
681       DB*
682       dbopen (const char * file, int flags, int mode, 
683               DBTYPE type, const void * openinfo)
684
685 The parameter C<type> is an enumeration which specifies which of the 3
686 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
687 Depending on which of these is actually chosen, the final parameter,
688 I<openinfo> points to a data structure which allows tailoring of the
689 specific interface method.
690
691 This interface is handled slightly differently in B<DB_File>. Here is
692 an equivalent call using B<DB_File>:
693
694         tie %array, 'DB_File', $filename, $flags, $mode, $DB_HASH ;
695
696 The C<filename>, C<flags> and C<mode> parameters are the direct
697 equivalent of their dbopen() counterparts. The final parameter $DB_HASH
698 performs the function of both the C<type> and C<openinfo> parameters in
699 dbopen().
700
701 In the example above $DB_HASH is actually a pre-defined reference to a
702 hash object. B<DB_File> has three of these pre-defined references.
703 Apart from $DB_HASH, there is also $DB_BTREE and $DB_RECNO.
704
705 The keys allowed in each of these pre-defined references is limited to
706 the names used in the equivalent C structure. So, for example, the
707 $DB_HASH reference will only allow keys called C<bsize>, C<cachesize>,
708 C<ffactor>, C<hash>, C<lorder> and C<nelem>. 
709
710 To change one of these elements, just assign to it like this:
711
712         $DB_HASH->{'cachesize'} = 10000 ;
713
714 The three predefined variables $DB_HASH, $DB_BTREE and $DB_RECNO are
715 usually adequate for most applications.  If you do need to create extra
716 instances of these objects, constructors are available for each file
717 type.
718
719 Here are examples of the constructors and the valid options available
720 for DB_HASH, DB_BTREE and DB_RECNO respectively.
721
722      $a = new DB_File::HASHINFO ;
723      $a->{'bsize'} ;
724      $a->{'cachesize'} ;
725      $a->{'ffactor'};
726      $a->{'hash'} ;
727      $a->{'lorder'} ;
728      $a->{'nelem'} ;
729
730      $b = new DB_File::BTREEINFO ;
731      $b->{'flags'} ;
732      $b->{'cachesize'} ;
733      $b->{'maxkeypage'} ;
734      $b->{'minkeypage'} ;
735      $b->{'psize'} ;
736      $b->{'compare'} ;
737      $b->{'prefix'} ;
738      $b->{'lorder'} ;
739
740      $c = new DB_File::RECNOINFO ;
741      $c->{'bval'} ;
742      $c->{'cachesize'} ;
743      $c->{'psize'} ;
744      $c->{'flags'} ;
745      $c->{'lorder'} ;
746      $c->{'reclen'} ;
747      $c->{'bfname'} ;
748
749 The values stored in the hashes above are mostly the direct equivalent
750 of their C counterpart. Like their C counterparts, all are set to a
751 default values - that means you don't have to set I<all> of the
752 values when you only want to change one. Here is an example:
753
754      $a = new DB_File::HASHINFO ;
755      $a->{'cachesize'} =  12345 ;
756      tie %y, 'DB_File', "filename", $flags, 0777, $a ;
757
758 A few of the options need extra discussion here. When used, the C
759 equivalent of the keys C<hash>, C<compare> and C<prefix> store pointers
760 to C functions. In B<DB_File> these keys are used to store references
761 to Perl subs. Below are templates for each of the subs:
762
763     sub hash
764     {
765         my ($data) = @_ ;
766         ...
767         # return the hash value for $data
768         return $hash ;
769     }
770
771     sub compare
772     {
773         my ($key, $key2) = @_ ;
774         ...
775         # return  0 if $key1 eq $key2
776         #        -1 if $key1 lt $key2
777         #         1 if $key1 gt $key2
778         return (-1 , 0 or 1) ;
779     }
780
781     sub prefix
782     {
783         my ($key, $key2) = @_ ;
784         ...
785         # return number of bytes of $key2 which are 
786         # necessary to determine that it is greater than $key1
787         return $bytes ;
788     }
789
790 See L<Changing the BTREE sort order> for an example of using the
791 C<compare> template.
792
793 If you are using the DB_RECNO interface and you intend making use of
794 C<bval>, you should check out L<The 'bval' Option>.
795
796 =head2 Default Parameters
797
798 It is possible to omit some or all of the final 4 parameters in the
799 call to C<tie> and let them take default values. As DB_HASH is the most
800 common file format used, the call:
801
802     tie %A, "DB_File", "filename" ;
803
804 is equivalent to:
805
806     tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;
807
808 It is also possible to omit the filename parameter as well, so the
809 call:
810
811     tie %A, "DB_File" ;
812
813 is equivalent to:
814
815     tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;
816
817 See L<In Memory Databases> for a discussion on the use of C<undef>
818 in place of a filename.
819
820 =head2 In Memory Databases
821
822 Berkeley DB allows the creation of in-memory databases by using NULL
823 (that is, a C<(char *)0> in C) in place of the filename.  B<DB_File>
824 uses C<undef> instead of NULL to provide this functionality.
825
826 =head1 DB_HASH
827
828 The DB_HASH file format is probably the most commonly used of the three
829 file formats that B<DB_File> supports. It is also very straightforward
830 to use.
831
832 =head2 A Simple Example
833
834 This example shows how to create a database, add key/value pairs to the
835 database, delete keys/value pairs and finally how to enumerate the
836 contents of the database.
837
838     use warnings ;
839     use strict ;
840     use DB_File ;
841     use vars qw( %h $k $v ) ;
842
843     unlink "fruit" ;
844     tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH 
845         or die "Cannot open file 'fruit': $!\n";
846
847     # Add a few key/value pairs to the file
848     $h{"apple"} = "red" ;
849     $h{"orange"} = "orange" ;
850     $h{"banana"} = "yellow" ;
851     $h{"tomato"} = "red" ;
852
853     # Check for existence of a key
854     print "Banana Exists\n\n" if $h{"banana"} ;
855
856     # Delete a key/value pair.
857     delete $h{"apple"} ;
858
859     # print the contents of the file
860     while (($k, $v) = each %h)
861       { print "$k -> $v\n" }
862
863     untie %h ;
864
865 here is the output:
866
867     Banana Exists
868
869     orange -> orange
870     tomato -> red
871     banana -> yellow
872
873 Note that the like ordinary associative arrays, the order of the keys
874 retrieved is in an apparently random order.
875
876 =head1 DB_BTREE
877
878 The DB_BTREE format is useful when you want to store data in a given
879 order. By default the keys will be stored in lexical order, but as you
880 will see from the example shown in the next section, it is very easy to
881 define your own sorting function.
882
883 =head2 Changing the BTREE sort order
884
885 This script shows how to override the default sorting algorithm that
886 BTREE uses. Instead of using the normal lexical ordering, a case
887 insensitive compare function will be used.
888
889     use warnings ;
890     use strict ;
891     use DB_File ;
892
893     my %h ;
894
895     sub Compare
896     {
897         my ($key1, $key2) = @_ ;
898         "\L$key1" cmp "\L$key2" ;
899     }
900
901     # specify the Perl sub that will do the comparison
902     $DB_BTREE->{'compare'} = \&Compare ;
903
904     unlink "tree" ;
905     tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0666, $DB_BTREE 
906         or die "Cannot open file 'tree': $!\n" ;
907
908     # Add a key/value pair to the file
909     $h{'Wall'} = 'Larry' ;
910     $h{'Smith'} = 'John' ;
911     $h{'mouse'} = 'mickey' ;
912     $h{'duck'}  = 'donald' ;
913
914     # Delete
915     delete $h{"duck"} ;
916
917     # Cycle through the keys printing them in order.
918     # Note it is not necessary to sort the keys as
919     # the btree will have kept them in order automatically.
920     foreach (keys %h)
921       { print "$_\n" }
922
923     untie %h ;
924
925 Here is the output from the code above.
926
927     mouse
928     Smith
929     Wall
930
931 There are a few point to bear in mind if you want to change the
932 ordering in a BTREE database:
933
934 =over 5
935
936 =item 1.
937
938 The new compare function must be specified when you create the database.
939
940 =item 2.
941
942 You cannot change the ordering once the database has been created. Thus
943 you must use the same compare function every time you access the
944 database.
945
946 =item 3
947
948 Duplicate keys are entirely defined by the comparison function.
949 In the case-insensitive example above, the keys: 'KEY' and 'key'
950 would be considered duplicates, and assigning to the second one
951 would overwirte the first. If duplicates are allowed for (with the
952 R_DUPS flag discussed below), only a single copy of duplicate keys
953 is stored in the database --- so (again with example above) assigning
954 three values to the keys: 'KEY', 'Key', and 'key' would leave just
955 the first key: 'KEY' in the database with three values. For some
956 situations this results in information loss, so care should be taken
957 to provide fully qualified comparison functions when necessary.
958 For example, the above comparison routine could be modified to
959 additionally compare case-sensitively if two keys are equal in the
960 case insensitive comparison:
961
962     sub compare {
963         my($key1, $key2) = @_;
964         lc $key1 cmp lc $key2 ||
965         $key1 cmp $key2;
966     }
967
968 And now you will only have duplicates when the keys themselves
969 are truly the same. (note: in versions of the db library prior to
970 about November 1996, such duplicate keys were retained so it was
971 possible to recover the original keys in sets of keys that
972 compared as equal).
973
974
975 =back 
976
977 =head2 Handling Duplicate Keys 
978
979 The BTREE file type optionally allows a single key to be associated
980 with an arbitrary number of values. This option is enabled by setting
981 the flags element of C<$DB_BTREE> to R_DUP when creating the database.
982
983 There are some difficulties in using the tied hash interface if you
984 want to manipulate a BTREE database with duplicate keys. Consider this
985 code:
986
987     use warnings ;
988     use strict ;
989     use DB_File ;
990
991     use vars qw($filename %h ) ;
992
993     $filename = "tree" ;
994     unlink $filename ;
995
996     # Enable duplicate records
997     $DB_BTREE->{'flags'} = R_DUP ;
998
999     tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE 
1000         or die "Cannot open $filename: $!\n";
1001
1002     # Add some key/value pairs to the file
1003     $h{'Wall'} = 'Larry' ;
1004     $h{'Wall'} = 'Brick' ; # Note the duplicate key
1005     $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1006     $h{'Smith'} = 'John' ;
1007     $h{'mouse'} = 'mickey' ;
1008
1009     # iterate through the associative array
1010     # and print each key/value pair.
1011     foreach (sort keys %h)
1012       { print "$_  -> $h{$_}\n" }
1013
1014     untie %h ;
1015
1016 Here is the output:
1017
1018     Smith   -> John
1019     Wall    -> Larry
1020     Wall    -> Larry
1021     Wall    -> Larry
1022     mouse   -> mickey
1023
1024 As you can see 3 records have been successfully created with key C<Wall>
1025 - the only thing is, when they are retrieved from the database they
1026 I<seem> to have the same value, namely C<Larry>. The problem is caused
1027 by the way that the associative array interface works. Basically, when
1028 the associative array interface is used to fetch the value associated
1029 with a given key, it will only ever retrieve the first value.
1030
1031 Although it may not be immediately obvious from the code above, the
1032 associative array interface can be used to write values with duplicate
1033 keys, but it cannot be used to read them back from the database.
1034
1035 The way to get around this problem is to use the Berkeley DB API method
1036 called C<seq>.  This method allows sequential access to key/value
1037 pairs. See L<THE API INTERFACE> for details of both the C<seq> method
1038 and the API in general.
1039
1040 Here is the script above rewritten using the C<seq> API method.
1041
1042     use warnings ;
1043     use strict ;
1044     use DB_File ;
1045
1046     use vars qw($filename $x %h $status $key $value) ;
1047
1048     $filename = "tree" ;
1049     unlink $filename ;
1050
1051     # Enable duplicate records
1052     $DB_BTREE->{'flags'} = R_DUP ;
1053
1054     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE 
1055         or die "Cannot open $filename: $!\n";
1056
1057     # Add some key/value pairs to the file
1058     $h{'Wall'} = 'Larry' ;
1059     $h{'Wall'} = 'Brick' ; # Note the duplicate key
1060     $h{'Wall'} = 'Brick' ; # Note the duplicate key and value
1061     $h{'Smith'} = 'John' ;
1062     $h{'mouse'} = 'mickey' ;
1063
1064     # iterate through the btree using seq
1065     # and print each key/value pair.
1066     $key = $value = 0 ;
1067     for ($status = $x->seq($key, $value, R_FIRST) ;
1068          $status == 0 ;
1069          $status = $x->seq($key, $value, R_NEXT) )
1070       {  print "$key -> $value\n" }
1071
1072     undef $x ;
1073     untie %h ;
1074
1075 that prints:
1076
1077     Smith   -> John
1078     Wall    -> Brick
1079     Wall    -> Brick
1080     Wall    -> Larry
1081     mouse   -> mickey
1082
1083 This time we have got all the key/value pairs, including the multiple
1084 values associated with the key C<Wall>.
1085
1086 To make life easier when dealing with duplicate keys, B<DB_File> comes with 
1087 a few utility methods.
1088
1089 =head2 The get_dup() Method
1090
1091 The C<get_dup> method assists in
1092 reading duplicate values from BTREE databases. The method can take the
1093 following forms:
1094
1095     $count = $x->get_dup($key) ;
1096     @list  = $x->get_dup($key) ;
1097     %list  = $x->get_dup($key, 1) ;
1098
1099 In a scalar context the method returns the number of values associated
1100 with the key, C<$key>.
1101
1102 In list context, it returns all the values which match C<$key>. Note
1103 that the values will be returned in an apparently random order.
1104
1105 In list context, if the second parameter is present and evaluates
1106 TRUE, the method returns an associative array. The keys of the
1107 associative array correspond to the values that matched in the BTREE
1108 and the values of the array are a count of the number of times that
1109 particular value occurred in the BTREE.
1110
1111 So assuming the database created above, we can use C<get_dup> like
1112 this:
1113
1114     use warnings ;
1115     use strict ;
1116     use DB_File ;
1117
1118     use vars qw($filename $x %h ) ;
1119
1120     $filename = "tree" ;
1121
1122     # Enable duplicate records
1123     $DB_BTREE->{'flags'} = R_DUP ;
1124
1125     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE 
1126         or die "Cannot open $filename: $!\n";
1127
1128     my $cnt  = $x->get_dup("Wall") ;
1129     print "Wall occurred $cnt times\n" ;
1130
1131     my %hash = $x->get_dup("Wall", 1) ;
1132     print "Larry is there\n" if $hash{'Larry'} ;
1133     print "There are $hash{'Brick'} Brick Walls\n" ;
1134
1135     my @list = sort $x->get_dup("Wall") ;
1136     print "Wall =>      [@list]\n" ;
1137
1138     @list = $x->get_dup("Smith") ;
1139     print "Smith =>     [@list]\n" ;
1140
1141     @list = $x->get_dup("Dog") ;
1142     print "Dog =>       [@list]\n" ;
1143
1144
1145 and it will print:
1146
1147     Wall occurred 3 times
1148     Larry is there
1149     There are 2 Brick Walls
1150     Wall =>     [Brick Brick Larry]
1151     Smith =>    [John]
1152     Dog =>      []
1153
1154 =head2 The find_dup() Method
1155
1156     $status = $X->find_dup($key, $value) ;
1157
1158 This method checks for the existence of a specific key/value pair. If the
1159 pair exists, the cursor is left pointing to the pair and the method 
1160 returns 0. Otherwise the method returns a non-zero value.
1161
1162 Assuming the database from the previous example:
1163
1164     use warnings ;
1165     use strict ;
1166     use DB_File ;
1167
1168     use vars qw($filename $x %h $found) ;
1169
1170     my $filename = "tree" ;
1171
1172     # Enable duplicate records
1173     $DB_BTREE->{'flags'} = R_DUP ;
1174
1175     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE 
1176         or die "Cannot open $filename: $!\n";
1177
1178     $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; 
1179     print "Larry Wall is $found there\n" ;
1180
1181     $found = ( $x->find_dup("Wall", "Harry") == 0 ? "" : "not") ; 
1182     print "Harry Wall is $found there\n" ;
1183
1184     undef $x ;
1185     untie %h ;
1186
1187 prints this
1188
1189     Larry Wall is  there
1190     Harry Wall is not there
1191
1192
1193 =head2 The del_dup() Method
1194
1195     $status = $X->del_dup($key, $value) ;
1196
1197 This method deletes a specific key/value pair. It returns
1198 0 if they exist and have been deleted successfully.
1199 Otherwise the method returns a non-zero value.
1200
1201 Again assuming the existence of the C<tree> database
1202
1203     use warnings ;
1204     use strict ;
1205     use DB_File ;
1206
1207     use vars qw($filename $x %h $found) ;
1208
1209     my $filename = "tree" ;
1210
1211     # Enable duplicate records
1212     $DB_BTREE->{'flags'} = R_DUP ;
1213
1214     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE 
1215         or die "Cannot open $filename: $!\n";
1216
1217     $x->del_dup("Wall", "Larry") ;
1218
1219     $found = ( $x->find_dup("Wall", "Larry") == 0 ? "" : "not") ; 
1220     print "Larry Wall is $found there\n" ;
1221
1222     undef $x ;
1223     untie %h ;
1224
1225 prints this
1226
1227     Larry Wall is not there
1228
1229 =head2 Matching Partial Keys 
1230
1231 The BTREE interface has a feature which allows partial keys to be
1232 matched. This functionality is I<only> available when the C<seq> method
1233 is used along with the R_CURSOR flag.
1234
1235     $x->seq($key, $value, R_CURSOR) ;
1236
1237 Here is the relevant quote from the dbopen man page where it defines
1238 the use of the R_CURSOR flag with seq:
1239
1240     Note, for the DB_BTREE access method, the returned key is not
1241     necessarily an exact match for the specified key. The returned key
1242     is the smallest key greater than or equal to the specified key,
1243     permitting partial key matches and range searches.
1244
1245 In the example script below, the C<match> sub uses this feature to find
1246 and print the first matching key/value pair given a partial key.
1247
1248     use warnings ;
1249     use strict ;
1250     use DB_File ;
1251     use Fcntl ;
1252
1253     use vars qw($filename $x %h $st $key $value) ;
1254
1255     sub match
1256     {
1257         my $key = shift ;
1258         my $value = 0;
1259         my $orig_key = $key ;
1260         $x->seq($key, $value, R_CURSOR) ;
1261         print "$orig_key\t-> $key\t-> $value\n" ;
1262     }
1263
1264     $filename = "tree" ;
1265     unlink $filename ;
1266
1267     $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
1268         or die "Cannot open $filename: $!\n";
1269
1270     # Add some key/value pairs to the file
1271     $h{'mouse'} = 'mickey' ;
1272     $h{'Wall'} = 'Larry' ;
1273     $h{'Walls'} = 'Brick' ; 
1274     $h{'Smith'} = 'John' ;
1275
1276
1277     $key = $value = 0 ;
1278     print "IN ORDER\n" ;
1279     for ($st = $x->seq($key, $value, R_FIRST) ;
1280          $st == 0 ;
1281          $st = $x->seq($key, $value, R_NEXT) )
1282
1283       {  print "$key    -> $value\n" }
1284
1285     print "\nPARTIAL MATCH\n" ;
1286
1287     match "Wa" ;
1288     match "A" ;
1289     match "a" ;
1290
1291     undef $x ;
1292     untie %h ;
1293
1294 Here is the output:
1295
1296     IN ORDER
1297     Smith -> John
1298     Wall  -> Larry
1299     Walls -> Brick
1300     mouse -> mickey
1301
1302     PARTIAL MATCH
1303     Wa -> Wall  -> Larry
1304     A  -> Smith -> John
1305     a  -> mouse -> mickey
1306
1307 =head1 DB_RECNO
1308
1309 DB_RECNO provides an interface to flat text files. Both variable and
1310 fixed length records are supported.
1311
1312 In order to make RECNO more compatible with Perl, the array offset for
1313 all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
1314
1315 As with normal Perl arrays, a RECNO array can be accessed using
1316 negative indexes. The index -1 refers to the last element of the array,
1317 -2 the second last, and so on. Attempting to access an element before
1318 the start of the array will raise a fatal run-time error.
1319
1320 =head2 The 'bval' Option
1321
1322 The operation of the bval option warrants some discussion. Here is the
1323 definition of bval from the Berkeley DB 1.85 recno manual page:
1324
1325     The delimiting byte to be used to mark  the  end  of  a
1326     record for variable-length records, and the pad charac-
1327     ter for fixed-length records.  If no  value  is  speci-
1328     fied,  newlines  (``\n'')  are  used to mark the end of
1329     variable-length records and  fixed-length  records  are
1330     padded with spaces.
1331
1332 The second sentence is wrong. In actual fact bval will only default to
1333 C<"\n"> when the openinfo parameter in dbopen is NULL. If a non-NULL
1334 openinfo parameter is used at all, the value that happens to be in bval
1335 will be used. That means you always have to specify bval when making
1336 use of any of the options in the openinfo parameter. This documentation
1337 error will be fixed in the next release of Berkeley DB.
1338
1339 That clarifies the situation with regards Berkeley DB itself. What
1340 about B<DB_File>? Well, the behavior defined in the quote above is
1341 quite useful, so B<DB_File> conforms to it.
1342
1343 That means that you can specify other options (e.g. cachesize) and
1344 still have bval default to C<"\n"> for variable length records, and
1345 space for fixed length records.
1346
1347 Also note that the bval option only allows you to specify a single byte
1348 as a delimeter.
1349
1350 =head2 A Simple Example
1351
1352 Here is a simple example that uses RECNO (if you are using a version 
1353 of Perl earlier than 5.004_57 this example won't work -- see 
1354 L<Extra RECNO Methods> for a workaround).
1355
1356     use warnings ;
1357     use strict ;
1358     use DB_File ;
1359
1360     my $filename = "text" ;
1361     unlink $filename ;
1362
1363     my @h ;
1364     tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_RECNO 
1365         or die "Cannot open file 'text': $!\n" ;
1366
1367     # Add a few key/value pairs to the file
1368     $h[0] = "orange" ;
1369     $h[1] = "blue" ;
1370     $h[2] = "yellow" ;
1371
1372     push @h, "green", "black" ;
1373
1374     my $elements = scalar @h ;
1375     print "The array contains $elements entries\n" ;
1376
1377     my $last = pop @h ;
1378     print "popped $last\n" ;
1379
1380     unshift @h, "white" ;
1381     my $first = shift @h ;
1382     print "shifted $first\n" ;
1383
1384     # Check for existence of a key
1385     print "Element 1 Exists with value $h[1]\n" if $h[1] ;
1386
1387     # use a negative index
1388     print "The last element is $h[-1]\n" ;
1389     print "The 2nd last element is $h[-2]\n" ;
1390
1391     untie @h ;
1392
1393 Here is the output from the script:
1394
1395     The array contains 5 entries
1396     popped black
1397     shifted white
1398     Element 1 Exists with value blue
1399     The last element is green
1400     The 2nd last element is yellow
1401
1402 =head2 Extra RECNO Methods
1403
1404 If you are using a version of Perl earlier than 5.004_57, the tied
1405 array interface is quite limited. In the example script above
1406 C<push>, C<pop>, C<shift>, C<unshift>
1407 or determining the array length will not work with a tied array.
1408
1409 To make the interface more useful for older versions of Perl, a number
1410 of methods are supplied with B<DB_File> to simulate the missing array
1411 operations. All these methods are accessed via the object returned from
1412 the tie call.
1413
1414 Here are the methods:
1415
1416 =over 5
1417
1418 =item B<$X-E<gt>push(list) ;>
1419
1420 Pushes the elements of C<list> to the end of the array.
1421
1422 =item B<$value = $X-E<gt>pop ;>
1423
1424 Removes and returns the last element of the array.
1425
1426 =item B<$X-E<gt>shift>
1427
1428 Removes and returns the first element of the array.
1429
1430 =item B<$X-E<gt>unshift(list) ;>
1431
1432 Pushes the elements of C<list> to the start of the array.
1433
1434 =item B<$X-E<gt>length>
1435
1436 Returns the number of elements in the array.
1437
1438 =item B<$X-E<gt>splice(offset, length, elements);>
1439
1440 Returns a splice of the the array.
1441
1442 =back
1443
1444 =head2 Another Example
1445
1446 Here is a more complete example that makes use of some of the methods
1447 described above. It also makes use of the API interface directly (see 
1448 L<THE API INTERFACE>).
1449
1450     use warnings ;
1451     use strict ;
1452     use vars qw(@h $H $file $i) ;
1453     use DB_File ;
1454     use Fcntl ;
1455
1456     $file = "text" ;
1457
1458     unlink $file ;
1459
1460     $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0666, $DB_RECNO 
1461         or die "Cannot open file $file: $!\n" ;
1462
1463     # first create a text file to play with
1464     $h[0] = "zero" ;
1465     $h[1] = "one" ;
1466     $h[2] = "two" ;
1467     $h[3] = "three" ;
1468     $h[4] = "four" ;
1469
1470
1471     # Print the records in order.
1472     #
1473     # The length method is needed here because evaluating a tied
1474     # array in a scalar context does not return the number of
1475     # elements in the array.  
1476
1477     print "\nORIGINAL\n" ;
1478     foreach $i (0 .. $H->length - 1) {
1479         print "$i: $h[$i]\n" ;
1480     }
1481
1482     # use the push & pop methods
1483     $a = $H->pop ;
1484     $H->push("last") ;
1485     print "\nThe last record was [$a]\n" ;
1486
1487     # and the shift & unshift methods
1488     $a = $H->shift ;
1489     $H->unshift("first") ;
1490     print "The first record was [$a]\n" ;
1491
1492     # Use the API to add a new record after record 2.
1493     $i = 2 ;
1494     $H->put($i, "Newbie", R_IAFTER) ;
1495
1496     # and a new record before record 1.
1497     $i = 1 ;
1498     $H->put($i, "New One", R_IBEFORE) ;
1499
1500     # delete record 3
1501     $H->del(3) ;
1502
1503     # now print the records in reverse order
1504     print "\nREVERSE\n" ;
1505     for ($i = $H->length - 1 ; $i >= 0 ; -- $i)
1506       { print "$i: $h[$i]\n" }
1507
1508     # same again, but use the API functions instead
1509     print "\nREVERSE again\n" ;
1510     my ($s, $k, $v)  = (0, 0, 0) ;
1511     for ($s = $H->seq($k, $v, R_LAST) ; 
1512              $s == 0 ; 
1513              $s = $H->seq($k, $v, R_PREV))
1514       { print "$k: $v\n" }
1515
1516     undef $H ;
1517     untie @h ;
1518
1519 and this is what it outputs:
1520
1521     ORIGINAL
1522     0: zero
1523     1: one
1524     2: two
1525     3: three
1526     4: four
1527
1528     The last record was [four]
1529     The first record was [zero]
1530
1531     REVERSE
1532     5: last
1533     4: three
1534     3: Newbie
1535     2: one
1536     1: New One
1537     0: first
1538
1539     REVERSE again
1540     5: last
1541     4: three
1542     3: Newbie
1543     2: one
1544     1: New One
1545     0: first
1546
1547 Notes:
1548
1549 =over 5
1550
1551 =item 1.
1552
1553 Rather than iterating through the array, C<@h> like this:
1554
1555     foreach $i (@h)
1556
1557 it is necessary to use either this:
1558
1559     foreach $i (0 .. $H->length - 1) 
1560
1561 or this:
1562
1563     for ($a = $H->get($k, $v, R_FIRST) ;
1564          $a == 0 ;
1565          $a = $H->get($k, $v, R_NEXT) )
1566
1567 =item 2.
1568
1569 Notice that both times the C<put> method was used the record index was
1570 specified using a variable, C<$i>, rather than the literal value
1571 itself. This is because C<put> will return the record number of the
1572 inserted line via that parameter.
1573
1574 =back
1575
1576 =head1 THE API INTERFACE
1577
1578 As well as accessing Berkeley DB using a tied hash or array, it is also
1579 possible to make direct use of most of the API functions defined in the
1580 Berkeley DB documentation.
1581
1582 To do this you need to store a copy of the object returned from the tie.
1583
1584         $db = tie %hash, "DB_File", "filename" ;
1585
1586 Once you have done that, you can access the Berkeley DB API functions
1587 as B<DB_File> methods directly like this:
1588
1589         $db->put($key, $value, R_NOOVERWRITE) ;
1590
1591 B<Important:> If you have saved a copy of the object returned from
1592 C<tie>, the underlying database file will I<not> be closed until both
1593 the tied variable is untied and all copies of the saved object are
1594 destroyed. 
1595
1596     use DB_File ;
1597     $db = tie %hash, "DB_File", "filename" 
1598         or die "Cannot tie filename: $!" ;
1599     ...
1600     undef $db ;
1601     untie %hash ;
1602
1603 See L<The untie() Gotcha> for more details.
1604
1605 All the functions defined in L<dbopen> are available except for
1606 close() and dbopen() itself. The B<DB_File> method interface to the
1607 supported functions have been implemented to mirror the way Berkeley DB
1608 works whenever possible. In particular note that:
1609
1610 =over 5
1611
1612 =item *
1613
1614 The methods return a status value. All return 0 on success.
1615 All return -1 to signify an error and set C<$!> to the exact
1616 error code. The return code 1 generally (but not always) means that the
1617 key specified did not exist in the database.
1618
1619 Other return codes are defined. See below and in the Berkeley DB
1620 documentation for details. The Berkeley DB documentation should be used
1621 as the definitive source.
1622
1623 =item *
1624
1625 Whenever a Berkeley DB function returns data via one of its parameters,
1626 the equivalent B<DB_File> method does exactly the same.
1627
1628 =item *
1629
1630 If you are careful, it is possible to mix API calls with the tied
1631 hash/array interface in the same piece of code. Although only a few of
1632 the methods used to implement the tied interface currently make use of
1633 the cursor, you should always assume that the cursor has been changed
1634 any time the tied hash/array interface is used. As an example, this
1635 code will probably not do what you expect:
1636
1637     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1638         or die "Cannot tie $filename: $!" ;
1639
1640     # Get the first key/value pair and set  the cursor
1641     $X->seq($key, $value, R_FIRST) ;
1642
1643     # this line will modify the cursor
1644     $count = scalar keys %x ; 
1645
1646     # Get the second key/value pair.
1647     # oops, it didn't, it got the last key/value pair!
1648     $X->seq($key, $value, R_NEXT) ;
1649
1650 The code above can be rearranged to get around the problem, like this:
1651
1652     $X = tie %x, 'DB_File', $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1653         or die "Cannot tie $filename: $!" ;
1654
1655     # this line will modify the cursor
1656     $count = scalar keys %x ; 
1657
1658     # Get the first key/value pair and set  the cursor
1659     $X->seq($key, $value, R_FIRST) ;
1660
1661     # Get the second key/value pair.
1662     # worked this time.
1663     $X->seq($key, $value, R_NEXT) ;
1664
1665 =back
1666
1667 All the constants defined in L<dbopen> for use in the flags parameters
1668 in the methods defined below are also available. Refer to the Berkeley
1669 DB documentation for the precise meaning of the flags values.
1670
1671 Below is a list of the methods available.
1672
1673 =over 5
1674
1675 =item B<$status = $X-E<gt>get($key, $value [, $flags]) ;>
1676
1677 Given a key (C<$key>) this method reads the value associated with it
1678 from the database. The value read from the database is returned in the
1679 C<$value> parameter.
1680
1681 If the key does not exist the method returns 1.
1682
1683 No flags are currently defined for this method.
1684
1685 =item B<$status = $X-E<gt>put($key, $value [, $flags]) ;>
1686
1687 Stores the key/value pair in the database.
1688
1689 If you use either the R_IAFTER or R_IBEFORE flags, the C<$key> parameter
1690 will have the record number of the inserted key/value pair set.
1691
1692 Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
1693 R_SETCURSOR.
1694
1695 =item B<$status = $X-E<gt>del($key [, $flags]) ;>
1696
1697 Removes all key/value pairs with key C<$key> from the database.
1698
1699 A return code of 1 means that the requested key was not in the
1700 database.
1701
1702 R_CURSOR is the only valid flag at present.
1703
1704 =item B<$status = $X-E<gt>fd ;>
1705
1706 Returns the file descriptor for the underlying database.
1707
1708 See L<Locking: The Trouble with fd> for an explanation for why you should
1709 not use C<fd> to lock your database.
1710
1711 =item B<$status = $X-E<gt>seq($key, $value, $flags) ;>
1712
1713 This interface allows sequential retrieval from the database. See
1714 L<dbopen> for full details.
1715
1716 Both the C<$key> and C<$value> parameters will be set to the key/value
1717 pair read from the database.
1718
1719 The flags parameter is mandatory. The valid flag values are R_CURSOR,
1720 R_FIRST, R_LAST, R_NEXT and R_PREV.
1721
1722 =item B<$status = $X-E<gt>sync([$flags]) ;>
1723
1724 Flushes any cached buffers to disk.
1725
1726 R_RECNOSYNC is the only valid flag at present.
1727
1728 =back
1729
1730 =head1 DBM FILTERS
1731
1732 A DBM Filter is a piece of code that is be used when you I<always>
1733 want to make the same transformation to all keys and/or values in a
1734 DBM database.
1735
1736 There are four methods associated with DBM Filters. All work identically,
1737 and each is used to install (or uninstall) a single DBM Filter. Each
1738 expects a single parameter, namely a reference to a sub. The only
1739 difference between them is the place that the filter is installed.
1740
1741 To summarise:
1742
1743 =over 5
1744
1745 =item B<filter_store_key>
1746
1747 If a filter has been installed with this method, it will be invoked
1748 every time you write a key to a DBM database.
1749
1750 =item B<filter_store_value>
1751
1752 If a filter has been installed with this method, it will be invoked
1753 every time you write a value to a DBM database.
1754
1755
1756 =item B<filter_fetch_key>
1757
1758 If a filter has been installed with this method, it will be invoked
1759 every time you read a key from a DBM database.
1760
1761 =item B<filter_fetch_value>
1762
1763 If a filter has been installed with this method, it will be invoked
1764 every time you read a value from a DBM database.
1765
1766 =back
1767
1768 You can use any combination of the methods, from none, to all four.
1769
1770 All filter methods return the existing filter, if present, or C<undef>
1771 in not.
1772
1773 To delete a filter pass C<undef> to it.
1774
1775 =head2 The Filter
1776
1777 When each filter is called by Perl, a local copy of C<$_> will contain
1778 the key or value to be filtered. Filtering is achieved by modifying
1779 the contents of C<$_>. The return code from the filter is ignored.
1780
1781 =head2 An Example -- the NULL termination problem.
1782
1783 Consider the following scenario. You have a DBM database
1784 that you need to share with a third-party C application. The C application
1785 assumes that I<all> keys and values are NULL terminated. Unfortunately
1786 when Perl writes to DBM databases it doesn't use NULL termination, so
1787 your Perl application will have to manage NULL termination itself. When
1788 you write to the database you will have to use something like this:
1789
1790     $hash{"$key\0"} = "$value\0" ;
1791
1792 Similarly the NULL needs to be taken into account when you are considering
1793 the length of existing keys/values.
1794
1795 It would be much better if you could ignore the NULL terminations issue
1796 in the main application code and have a mechanism that automatically
1797 added the terminating NULL to all keys and values whenever you write to
1798 the database and have them removed when you read from the database. As I'm
1799 sure you have already guessed, this is a problem that DBM Filters can
1800 fix very easily.
1801
1802     use warnings ;
1803     use strict ;
1804     use DB_File ;
1805
1806     my %hash ;
1807     my $filename = "/tmp/filt" ;
1808     unlink $filename ;
1809
1810     my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH 
1811       or die "Cannot open $filename: $!\n" ;
1812
1813     # Install DBM Filters
1814     $db->filter_fetch_key  ( sub { s/\0$//    } ) ;
1815     $db->filter_store_key  ( sub { $_ .= "\0" } ) ;
1816     $db->filter_fetch_value( sub { s/\0$//    } ) ;
1817     $db->filter_store_value( sub { $_ .= "\0" } ) ;
1818
1819     $hash{"abc"} = "def" ;
1820     my $a = $hash{"ABC"} ;
1821     # ...
1822     undef $db ;
1823     untie %hash ;
1824
1825 Hopefully the contents of each of the filters should be
1826 self-explanatory. Both "fetch" filters remove the terminating NULL,
1827 and both "store" filters add a terminating NULL.
1828
1829
1830 =head2 Another Example -- Key is a C int.
1831
1832 Here is another real-life example. By default, whenever Perl writes to
1833 a DBM database it always writes the key and value as strings. So when
1834 you use this:
1835
1836     $hash{12345} = "soemthing" ;
1837
1838 the key 12345 will get stored in the DBM database as the 5 byte string
1839 "12345". If you actually want the key to be stored in the DBM database
1840 as a C int, you will have to use C<pack> when writing, and C<unpack>
1841 when reading.
1842
1843 Here is a DBM Filter that does it:
1844
1845     use warnings ;
1846     use strict ;
1847     use DB_File ;
1848     my %hash ;
1849     my $filename = "/tmp/filt" ;
1850     unlink $filename ;
1851
1852
1853     my $db = tie %hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_HASH 
1854       or die "Cannot open $filename: $!\n" ;
1855
1856     $db->filter_fetch_key  ( sub { $_ = unpack("i", $_) } ) ;
1857     $db->filter_store_key  ( sub { $_ = pack ("i", $_) } ) ;
1858     $hash{123} = "def" ;
1859     # ...
1860     undef $db ;
1861     untie %hash ;
1862
1863 This time only two filters have been used -- we only need to manipulate
1864 the contents of the key, so it wasn't necessary to install any value
1865 filters.
1866
1867 =head1 HINTS AND TIPS 
1868
1869
1870 =head2 Locking: The Trouble with fd
1871
1872 Until version 1.72 of this module, the recommended technique for locking
1873 B<DB_File> databases was to flock the filehandle returned from the "fd"
1874 function. Unfortunately this technique has been shown to be fundamentally
1875 flawed (Kudos to David Harris for tracking this down). Use it at your own
1876 peril!
1877
1878 The locking technique went like this. 
1879
1880     $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0666)
1881         || die "dbcreat /tmp/foo.db $!";
1882     $fd = $db->fd;
1883     open(DB_FH, "+<&=$fd") || die "dup $!";
1884     flock (DB_FH, LOCK_EX) || die "flock: $!";
1885     ...
1886     $db{"Tom"} = "Jerry" ;
1887     ...
1888     flock(DB_FH, LOCK_UN);
1889     undef $db;
1890     untie %db;
1891     close(DB_FH);
1892
1893 In simple terms, this is what happens:
1894
1895 =over 5
1896
1897 =item 1.
1898
1899 Use "tie" to open the database.
1900
1901 =item 2.
1902
1903 Lock the database with fd & flock.
1904
1905 =item 3.
1906
1907 Read & Write to the database.
1908
1909 =item 4.
1910
1911 Unlock and close the database.
1912
1913 =back
1914
1915 Here is the crux of the problem. A side-effect of opening the B<DB_File>
1916 database in step 2 is that an initial block from the database will get
1917 read from disk and cached in memory.
1918
1919 To see why this is a problem, consider what can happen when two processes,
1920 say "A" and "B", both want to update the same B<DB_File> database
1921 using the locking steps outlined above. Assume process "A" has already
1922 opened the database and has a write lock, but it hasn't actually updated
1923 the database yet (it has finished step 2, but not started step 3 yet). Now
1924 process "B" tries to open the same database - step 1 will succeed,
1925 but it will block on step 2 until process "A" releases the lock. The
1926 important thing to notice here is that at this point in time both
1927 processes will have cached identical initial blocks from the database.
1928
1929 Now process "A" updates the database and happens to change some of the
1930 data held in the initial buffer. Process "A" terminates, flushing
1931 all cached data to disk and releasing the database lock. At this point
1932 the database on disk will correctly reflect the changes made by process
1933 "A".
1934
1935 With the lock released, process "B" can now continue. It also updates the
1936 database and unfortunately it too modifies the data that was in its
1937 initial buffer. Once that data gets flushed to disk it will overwrite
1938 some/all of the changes process "A" made to the database.
1939
1940 The result of this scenario is at best a database that doesn't contain
1941 what you expect. At worst the database will corrupt.
1942
1943 The above won't happen every time competing process update the same
1944 B<DB_File> database, but it does illustrate why the technique should
1945 not be used.
1946
1947 =head2 Safe ways to lock a database
1948
1949 Starting with version 2.x, Berkeley DB  has internal support for locking.
1950 The companion module to this one, B<BerkeleyDB>, provides an interface
1951 to this locking functionality. If you are serious about locking
1952 Berkeley DB databases, I strongly recommend using B<BerkeleyDB>.
1953
1954 If using B<BerkeleyDB> isn't an option, there are a number of modules
1955 available on CPAN that can be used to implement locking. Each one
1956 implements locking differently and has different goals in mind. It is
1957 therefore worth knowing the difference, so that you can pick the right
1958 one for your application. Here are the three locking wrappers:
1959
1960 =over 5
1961
1962 =item B<Tie::DB_Lock>
1963
1964 A B<DB_File> wrapper which creates copies of the database file for
1965 read access, so that you have a kind of a multiversioning concurrent read
1966 system. However, updates are still serial. Use for databases where reads
1967 may be lengthy and consistency problems may occur.
1968
1969 =item B<Tie::DB_LockFile> 
1970
1971 A B<DB_File> wrapper that has the ability to lock and unlock the database
1972 while it is being used. Avoids the tie-before-flock problem by simply
1973 re-tie-ing the database when you get or drop a lock.  Because of the
1974 flexibility in dropping and re-acquiring the lock in the middle of a
1975 session, this can be massaged into a system that will work with long
1976 updates and/or reads if the application follows the hints in the POD
1977 documentation.
1978
1979 =item B<DB_File::Lock> 
1980
1981 An extremely lightweight B<DB_File> wrapper that simply flocks a lockfile
1982 before tie-ing the database and drops the lock after the untie. Allows
1983 one to use the same lockfile for multiple databases to avoid deadlock
1984 problems, if desired. Use for databases where updates are reads are
1985 quick and simple flock locking semantics are enough.
1986
1987 =back
1988
1989 =head2 Sharing Databases With C Applications
1990
1991 There is no technical reason why a Berkeley DB database cannot be
1992 shared by both a Perl and a C application.
1993
1994 The vast majority of problems that are reported in this area boil down
1995 to the fact that C strings are NULL terminated, whilst Perl strings are
1996 not. See L<DBM FILTERS> for a generic way to work around this problem.
1997
1998 Here is a real example. Netscape 2.0 keeps a record of the locations you
1999 visit along with the time you last visited them in a DB_HASH database.
2000 This is usually stored in the file F<~/.netscape/history.db>. The key
2001 field in the database is the location string and the value field is the
2002 time the location was last visited stored as a 4 byte binary value.
2003
2004 If you haven't already guessed, the location string is stored with a
2005 terminating NULL. This means you need to be careful when accessing the
2006 database.
2007
2008 Here is a snippet of code that is loosely based on Tom Christiansen's
2009 I<ggh> script (available from your nearest CPAN archive in
2010 F<authors/id/TOMC/scripts/nshist.gz>).
2011
2012     use warnings ;
2013     use strict ;
2014     use DB_File ;
2015     use Fcntl ;
2016
2017     use vars qw( $dotdir $HISTORY %hist_db $href $binary_time $date ) ;
2018     $dotdir = $ENV{HOME} || $ENV{LOGNAME};
2019
2020     $HISTORY = "$dotdir/.netscape/history.db";
2021
2022     tie %hist_db, 'DB_File', $HISTORY
2023         or die "Cannot open $HISTORY: $!\n" ;;
2024
2025     # Dump the complete database
2026     while ( ($href, $binary_time) = each %hist_db ) {
2027
2028         # remove the terminating NULL
2029         $href =~ s/\x00$// ;
2030
2031         # convert the binary time into a user friendly string
2032         $date = localtime unpack("V", $binary_time);
2033         print "$date $href\n" ;
2034     }
2035
2036     # check for the existence of a specific key
2037     # remember to add the NULL
2038     if ( $binary_time = $hist_db{"http://mox.perl.com/\x00"} ) {
2039         $date = localtime unpack("V", $binary_time) ;
2040         print "Last visited mox.perl.com on $date\n" ;
2041     }
2042     else {
2043         print "Never visited mox.perl.com\n"
2044     }
2045
2046     untie %hist_db ;
2047
2048 =head2 The untie() Gotcha
2049
2050 If you make use of the Berkeley DB API, it is I<very> strongly
2051 recommended that you read L<perltie/The untie Gotcha>. 
2052
2053 Even if you don't currently make use of the API interface, it is still
2054 worth reading it.
2055
2056 Here is an example which illustrates the problem from a B<DB_File>
2057 perspective:
2058
2059     use DB_File ;
2060     use Fcntl ;
2061
2062     my %x ;
2063     my $X ;
2064
2065     $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_TRUNC
2066         or die "Cannot tie first time: $!" ;
2067
2068     $x{123} = 456 ;
2069
2070     untie %x ;
2071
2072     tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
2073         or die "Cannot tie second time: $!" ;
2074
2075     untie %x ;
2076
2077 When run, the script will produce this error message:
2078
2079     Cannot tie second time: Invalid argument at bad.file line 14.
2080
2081 Although the error message above refers to the second tie() statement
2082 in the script, the source of the problem is really with the untie()
2083 statement that precedes it.
2084
2085 Having read L<perltie> you will probably have already guessed that the
2086 error is caused by the extra copy of the tied object stored in C<$X>.
2087 If you haven't, then the problem boils down to the fact that the
2088 B<DB_File> destructor, DESTROY, will not be called until I<all>
2089 references to the tied object are destroyed. Both the tied variable,
2090 C<%x>, and C<$X> above hold a reference to the object. The call to
2091 untie() will destroy the first, but C<$X> still holds a valid
2092 reference, so the destructor will not get called and the database file
2093 F<tst.fil> will remain open. The fact that Berkeley DB then reports the
2094 attempt to open a database that is already open via the catch-all
2095 "Invalid argument" doesn't help.
2096
2097 If you run the script with the C<-w> flag the error message becomes:
2098
2099     untie attempted while 1 inner references still exist at bad.file line 12.
2100     Cannot tie second time: Invalid argument at bad.file line 14.
2101
2102 which pinpoints the real problem. Finally the script can now be
2103 modified to fix the original problem by destroying the API object
2104 before the untie:
2105
2106     ...
2107     $x{123} = 456 ;
2108
2109     undef $X ;
2110     untie %x ;
2111
2112     $X = tie %x, 'DB_File', 'tst.fil' , O_RDWR|O_CREAT
2113     ...
2114
2115
2116 =head1 COMMON QUESTIONS
2117
2118 =head2 Why is there Perl source in my database?
2119
2120 If you look at the contents of a database file created by DB_File,
2121 there can sometimes be part of a Perl script included in it.
2122
2123 This happens because Berkeley DB uses dynamic memory to allocate
2124 buffers which will subsequently be written to the database file. Being
2125 dynamic, the memory could have been used for anything before DB
2126 malloced it. As Berkeley DB doesn't clear the memory once it has been
2127 allocated, the unused portions will contain random junk. In the case
2128 where a Perl script gets written to the database, the random junk will
2129 correspond to an area of dynamic memory that happened to be used during
2130 the compilation of the script.
2131
2132 Unless you don't like the possibility of there being part of your Perl
2133 scripts embedded in a database file, this is nothing to worry about.
2134
2135 =head2 How do I store complex data structures with DB_File?
2136
2137 Although B<DB_File> cannot do this directly, there is a module which
2138 can layer transparently over B<DB_File> to accomplish this feat.
2139
2140 Check out the MLDBM module, available on CPAN in the directory
2141 F<modules/by-module/MLDBM>.
2142
2143 =head2 What does "Invalid Argument" mean?
2144
2145 You will get this error message when one of the parameters in the
2146 C<tie> call is wrong. Unfortunately there are quite a few parameters to
2147 get wrong, so it can be difficult to figure out which one it is.
2148
2149 Here are a couple of possibilities:
2150
2151 =over 5
2152
2153 =item 1.
2154
2155 Attempting to reopen a database without closing it. 
2156
2157 =item 2.
2158
2159 Using the O_WRONLY flag.
2160
2161 =back
2162
2163 =head2 What does "Bareword 'DB_File' not allowed" mean? 
2164
2165 You will encounter this particular error message when you have the
2166 C<strict 'subs'> pragma (or the full strict pragma) in your script.
2167 Consider this script:
2168
2169     use warnings ;
2170     use strict ;
2171     use DB_File ;
2172     use vars qw(%x) ;
2173     tie %x, DB_File, "filename" ;
2174
2175 Running it produces the error in question:
2176
2177     Bareword "DB_File" not allowed while "strict subs" in use 
2178
2179 To get around the error, place the word C<DB_File> in either single or
2180 double quotes, like this:
2181
2182     tie %x, "DB_File", "filename" ;
2183
2184 Although it might seem like a real pain, it is really worth the effort
2185 of having a C<use strict> in all your scripts.
2186
2187 =head1 REFERENCES
2188
2189 Articles that are either about B<DB_File> or make use of it.
2190
2191 =over 5
2192
2193 =item 1.
2194
2195 I<Full-Text Searching in Perl>, Tim Kientzle (tkientzle@ddj.com),
2196 Dr. Dobb's Journal, Issue 295, January 1999, pp 34-41
2197
2198 =back
2199
2200 =head1 HISTORY
2201
2202 Moved to the Changes file.
2203
2204 =head1 BUGS
2205
2206 Some older versions of Berkeley DB had problems with fixed length
2207 records using the RECNO file format. This problem has been fixed since
2208 version 1.85 of Berkeley DB.
2209
2210 I am sure there are bugs in the code. If you do find any, or can
2211 suggest any enhancements, I would welcome your comments.
2212
2213 =head1 AVAILABILITY
2214
2215 B<DB_File> comes with the standard Perl source distribution. Look in
2216 the directory F<ext/DB_File>. Given the amount of time between releases
2217 of Perl the version that ships with Perl is quite likely to be out of
2218 date, so the most recent version can always be found on CPAN (see
2219 L<perlmod/CPAN> for details), in the directory
2220 F<modules/by-module/DB_File>.
2221
2222 This version of B<DB_File> will work with either version 1.x, 2.x or
2223 3.x of Berkeley DB, but is limited to the functionality provided by
2224 version 1.
2225
2226 The official web site for Berkeley DB is F<http://www.sleepycat.com>.
2227 All versions of Berkeley DB are available there.
2228
2229 Alternatively, Berkeley DB version 1 is available at your nearest CPAN
2230 archive in F<src/misc/db.1.85.tar.gz>.
2231
2232 If you are running IRIX, then get Berkeley DB version 1 from
2233 F<http://reality.sgi.com/ariel>. It has the patches necessary to
2234 compile properly on IRIX 5.3.
2235
2236 =head1 COPYRIGHT
2237
2238 Copyright (c) 1995-2001 Paul Marquess. All rights reserved. This program
2239 is free software; you can redistribute it and/or modify it under the
2240 same terms as Perl itself.
2241
2242 Although B<DB_File> is covered by the Perl license, the library it
2243 makes use of, namely Berkeley DB, is not. Berkeley DB has its own
2244 copyright and its own license. Please take the time to read it.
2245
2246 Here are are few words taken from the Berkeley DB FAQ (at
2247 F<http://www.sleepycat.com>) regarding the license:
2248
2249     Do I have to license DB to use it in Perl scripts? 
2250
2251     No. The Berkeley DB license requires that software that uses
2252     Berkeley DB be freely redistributable. In the case of Perl, that
2253     software is Perl, and not your scripts. Any Perl scripts that you
2254     write are your property, including scripts that make use of
2255     Berkeley DB. Neither the Perl license nor the Berkeley DB license
2256     place any restriction on what you may do with them.
2257
2258 If you are in any doubt about the license situation, contact either the
2259 Berkeley DB authors or the author of DB_File. See L<"AUTHOR"> for details.
2260
2261
2262 =head1 SEE ALSO
2263
2264 L<perl(1)>, L<dbopen(3)>, L<hash(3)>, L<recno(3)>, L<btree(3)>,
2265 L<dbmfilter>
2266
2267 =head1 AUTHOR
2268
2269 The DB_File interface was written by Paul Marquess
2270 E<lt>Paul.Marquess@btinternet.comE<gt>.
2271 Questions about the DB system itself may be addressed to
2272 E<lt>db@sleepycat.com<gt>.
2273
2274 =cut