Debian Codegraph @

upstream/mageia-cauldron/man3pm/DB_File.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "DB_File 3pm"
61 .TH DB_File 3pm 2026-05-26 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 DB_File \- Perl5 access to Berkeley DB version 1.x
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use DB_File;
72 \&
73 \& [$X =] tie %hash, \*(AqDB_File\*(Aq, [$filename, $flags, $mode, $DB_HASH] ;
74 \& [$X =] tie %hash, \*(AqDB_File\*(Aq, $filename, $flags, $mode, $DB_BTREE ;
75 \& [$X =] tie @array, \*(AqDB_File\*(Aq, $filename, $flags, $mode, $DB_RECNO ;
76 \&
77 \& $status = $X\->del($key [, $flags]) ;
78 \& $status = $X\->put($key, $value [, $flags]) ;
79 \& $status = $X\->get($key, $value [, $flags]) ;
80 \& $status = $X\->seq($key, $value, $flags) ;
81 \& $status = $X\->sync([$flags]) ;
82 \& $status = $X\->fd ;
83 \&
84 \& # BTREE only
85 \& $count = $X\->get_dup($key) ;
86 \& @list = $X\->get_dup($key) ;
87 \& %list = $X\->get_dup($key, 1) ;
88 \& $status = $X\->find_dup($key, $value) ;
89 \& $status = $X\->del_dup($key, $value) ;
90 \&
91 \& # RECNO only
92 \& $a = $X\->length;
93 \& $a = $X\->pop ;
94 \& $X\->push(list);
95 \& $a = $X\->shift;
96 \& $X\->unshift(list);
97 \& @r = $X\->splice(offset, length, elements);
98 \&
99 \& # DBM Filters
100 \& $old_filter = $db\->filter_store_key ( sub { ... } ) ;
101 \& $old_filter = $db\->filter_store_value( sub { ... } ) ;
102 \& $old_filter = $db\->filter_fetch_key ( sub { ... } ) ;
103 \& $old_filter = $db\->filter_fetch_value( sub { ... } ) ;
104 \&
105 \& untie %hash ;
106 \& untie @array ;
107 .Ve
108 .SH DESCRIPTION
109 .IX Header "DESCRIPTION"
110 \&\fBDB_File\fR is a module which allows Perl programs to make use of the
111 facilities provided by Berkeley DB version 1.x (if you have a newer
112 version of DB, see "Using DB_File with Berkeley DB version 2 or greater").
113 It is assumed that you have a copy of the Berkeley DB manual pages at
114 hand when reading this documentation. The interface defined here
115 mirrors the Berkeley DB interface closely.
116 .PP
117 Berkeley DB is a C library which provides a consistent interface to a
118 number of database formats. \fBDB_File\fR provides an interface to all
119 three of the database types currently supported by Berkeley DB.
120 .PP
121 The file types are:
122 .IP \fBDB_HASH\fR 5
123 .IX Item "DB_HASH"
124 This database type allows arbitrary key/value pairs to be stored in data
125 files. This is equivalent to the functionality provided by other
126 hashing packages like DBM, NDBM, ODBM, GDBM, and SDBM. Remember though,
127 the files created using DB_HASH are not compatible with any of the
128 other packages mentioned.
129 .Sp
130 A default hashing algorithm, which will be adequate for most
131 applications, is built into Berkeley DB. If you do need to use your own
132 hashing algorithm it is possible to write your own in Perl and have
133 \&\fBDB_File\fR use it instead.
134 .IP \fBDB_BTREE\fR 5
135 .IX Item "DB_BTREE"
136 The btree format allows arbitrary key/value pairs to be stored in a
137 sorted, balanced binary tree.
138 .Sp
139 As with the DB_HASH format, it is possible to provide a user defined
140 Perl routine to perform the comparison of keys. By default, though, the
141 keys are stored in lexical order.
142 .IP \fBDB_RECNO\fR 5
143 .IX Item "DB_RECNO"
144 DB_RECNO allows both fixed\-length and variable\-length flat text files
145 to be manipulated using the same key/value pair interface as in DB_HASH
146 and DB_BTREE. In this case the key will consist of a record (line)
147 number.
148 .SS "Using DB_File with Berkeley DB version 2 or greater"
149 .IX Subsection "Using DB_File with Berkeley DB version 2 or greater"
150 Although \fBDB_File\fR is intended to be used with Berkeley DB version 1,
151 it can also be used with version 2, 3 or 4. In this case the interface is
152 limited to the functionality provided by Berkeley DB 1.x. Anywhere the
153 version 2 or greater interface differs, \fBDB_File\fR arranges for it to work
154 like version 1. This feature allows \fBDB_File\fR scripts that were built
155 with version 1 to be migrated to version 2 or greater without any changes.
156 .PP
157 If you want to make use of the new features available in Berkeley DB
158 2.x or greater, use the Perl module BerkeleyDB <https://metacpan.org/pod/BerkeleyDB> instead.
159 .PP
160 \&\fBNote:\fR The database file format has changed multiple times in Berkeley
161 DB version 2, 3 and 4. If you cannot recreate your databases, you
162 must dump any existing databases with either the \f(CW\*(C`db_dump\*(C'\fR or the
163 \&\f(CW\*(C`db_dump185\*(C'\fR utility that comes with Berkeley DB.
164 Once you have rebuilt DB_File to use Berkeley DB version 2 or greater,
165 your databases can be recreated using \f(CW\*(C`db_load\*(C'\fR. Refer to the Berkeley DB
166 documentation for further details.
167 .PP
168 Please read "COPYRIGHT" before using version 2.x or greater of Berkeley
169 DB with DB_File.
170 .SS "Interface to Berkeley DB"
171 .IX Subsection "Interface to Berkeley DB"
172 \&\fBDB_File\fR allows access to Berkeley DB files using the \fBtie()\fR mechanism
173 in Perl 5 (for full details, see "\fBtie()\fR" in perlfunc). This facility
174 allows \fBDB_File\fR to access Berkeley DB files using either an
175 associative array (for DB_HASH & DB_BTREE file types) or an ordinary
176 array (for the DB_RECNO file type).
177 .PP
178 In addition to the \fBtie()\fR interface, it is also possible to access most
179 of the functions provided in the Berkeley DB API directly.
180 See "THE API INTERFACE".
181 .SS "Opening a Berkeley DB Database File"
182 .IX Subsection "Opening a Berkeley DB Database File"
183 Berkeley DB uses the function \fBdbopen()\fR to open or create a database.
184 Here is the C prototype for \fBdbopen()\fR:
185 .PP
186 .Vb 3
187 \& DB*
188 \& dbopen (const char * file, int flags, int mode,
189 \& DBTYPE type, const void * openinfo)
190 .Ve
191 .PP
192 The parameter \f(CW\*(C`type\*(C'\fR is an enumeration which specifies which of the 3
193 interface methods (DB_HASH, DB_BTREE or DB_RECNO) is to be used.
194 Depending on which of these is actually chosen, the final parameter,
195 \&\fIopeninfo\fR points to a data structure which allows tailoring of the
196 specific interface method.
197 .PP
198 This interface is handled slightly differently in \fBDB_File\fR. Here is
199 an equivalent call using \fBDB_File\fR:
200 .PP
201 .Vb 1
202 \& tie %array, \*(AqDB_File\*(Aq, $filename, $flags, $mode, $DB_HASH ;
203 .Ve
204 .PP
205 The \f(CW\*(C`filename\*(C'\fR, \f(CW\*(C`flags\*(C'\fR and \f(CW\*(C`mode\*(C'\fR parameters are the direct
206 equivalent of their \fBdbopen()\fR counterparts. The final parameter \f(CW$DB_HASH\fR
207 performs the function of both the \f(CW\*(C`type\*(C'\fR and \f(CW\*(C`openinfo\*(C'\fR parameters in
208 \&\fBdbopen()\fR.
209 .PP
210 In the example above \f(CW$DB_HASH\fR is actually a pre\-defined reference to a
211 hash object. \fBDB_File\fR has three of these pre\-defined references.
212 Apart from \f(CW$DB_HASH\fR, there is also \f(CW$DB_BTREE\fR and \f(CW$DB_RECNO\fR.
213 .PP
214 The keys allowed in each of these pre\-defined references is limited to
215 the names used in the equivalent C structure. So, for example, the
216 \&\f(CW$DB_HASH\fR reference will only allow keys called \f(CW\*(C`bsize\*(C'\fR, \f(CW\*(C`cachesize\*(C'\fR,
217 \&\f(CW\*(C`ffactor\*(C'\fR, \f(CW\*(C`hash\*(C'\fR, \f(CW\*(C`lorder\*(C'\fR and \f(CW\*(C`nelem\*(C'\fR.
218 .PP
219 To change one of these elements, just assign to it like this:
220 .PP
221 .Vb 1
222 \& $DB_HASH\->{\*(Aqcachesize\*(Aq} = 10000 ;
223 .Ve
224 .PP
225 The three predefined variables \f(CW$DB_HASH\fR, \f(CW$DB_BTREE\fR and \f(CW$DB_RECNO\fR are
226 usually adequate for most applications. If you do need to create extra
227 instances of these objects, constructors are available for each file
228 type.
229 .PP
230 Here are examples of the constructors and the valid options available
231 for DB_HASH, DB_BTREE and DB_RECNO respectively.
232 .PP
233 .Vb 7
234 \& $a = DB_File::HASHINFO\->new();
235 \& $a\->{\*(Aqbsize\*(Aq} ;
236 \& $a\->{\*(Aqcachesize\*(Aq} ;
237 \& $a\->{\*(Aqffactor\*(Aq};
238 \& $a\->{\*(Aqhash\*(Aq} ;
239 \& $a\->{\*(Aqlorder\*(Aq} ;
240 \& $a\->{\*(Aqnelem\*(Aq} ;
241 \&
242 \& $b = DB_File::BTREEINFO\->new();
243 \& $b\->{\*(Aqflags\*(Aq} ;
244 \& $b\->{\*(Aqcachesize\*(Aq} ;
245 \& $b\->{\*(Aqmaxkeypage\*(Aq} ;
246 \& $b\->{\*(Aqminkeypage\*(Aq} ;
247 \& $b\->{\*(Aqpsize\*(Aq} ;
248 \& $b\->{\*(Aqcompare\*(Aq} ;
249 \& $b\->{\*(Aqprefix\*(Aq} ;
250 \& $b\->{\*(Aqlorder\*(Aq} ;
251 \&
252 \& $c = DB_File::RECNOINFO\->new();
253 \& $c\->{\*(Aqbval\*(Aq} ;
254 \& $c\->{\*(Aqcachesize\*(Aq} ;
255 \& $c\->{\*(Aqpsize\*(Aq} ;
256 \& $c\->{\*(Aqflags\*(Aq} ;
257 \& $c\->{\*(Aqlorder\*(Aq} ;
258 \& $c\->{\*(Aqreclen\*(Aq} ;
259 \& $c\->{\*(Aqbfname\*(Aq} ;
260 .Ve
261 .PP
262 The values stored in the hashes above are mostly the direct equivalent
263 of their C counterpart. Like their C counterparts, all are set to a
264 default values \- that means you don\*(Aqt have to set \fIall\fR of the
265 values when you only want to change one. Here is an example:
266 .PP
267 .Vb 3
268 \& $a = DB_File::HASHINFO\->new();
269 \& $a\->{\*(Aqcachesize\*(Aq} = 12345 ;
270 \& tie %y, \*(AqDB_File\*(Aq, "filename", $flags, 0777, $a ;
271 .Ve
272 .PP
273 A few of the options need extra discussion here. When used, the C
274 equivalent of the keys \f(CW\*(C`hash\*(C'\fR, \f(CW\*(C`compare\*(C'\fR and \f(CW\*(C`prefix\*(C'\fR store pointers
275 to C functions. In \fBDB_File\fR these keys are used to store references
276 to Perl subs. Below are templates for each of the subs:
277 .PP
278 .Vb 7
279 \& sub hash
280 \& {
281 \& my ($data) = @_ ;
282 \& ...
283 \& # return the hash value for $data
284 \& return $hash ;
285 \& }
286 \&
287 \& sub compare
288 \& {
289 \& my ($key, $key2) = @_ ;
290 \& ...
291 \& # return 0 if $key1 eq $key2
292 \& # \-1 if $key1 lt $key2
293 \& # 1 if $key1 gt $key2
294 \& return (\-1 , 0 or 1) ;
295 \& }
296 \&
297 \& sub prefix
298 \& {
299 \& my ($key, $key2) = @_ ;
300 \& ...
301 \& # return number of bytes of $key2 which are
302 \& # necessary to determine that it is greater than $key1
303 \& return $bytes ;
304 \& }
305 .Ve
306 .PP
307 See "Changing the BTREE sort order" for an example of using the
308 \&\f(CW\*(C`compare\*(C'\fR template.
309 .PP
310 If you are using the DB_RECNO interface and you intend making use of
311 \&\f(CW\*(C`bval\*(C'\fR, you should check out "The \*(Aqbval\*(Aq Option".
312 .SS "Default Parameters"
313 .IX Subsection "Default Parameters"
314 It is possible to omit some or all of the final 4 parameters in the
315 call to \f(CW\*(C`tie\*(C'\fR and let them take default values. As DB_HASH is the most
316 common file format used, the call:
317 .PP
318 .Vb 1
319 \& tie %A, "DB_File", "filename" ;
320 .Ve
321 .PP
322 is equivalent to:
323 .PP
324 .Vb 1
325 \& tie %A, "DB_File", "filename", O_CREAT|O_RDWR, 0666, $DB_HASH ;
326 .Ve
327 .PP
328 It is also possible to omit the filename parameter as well, so the
329 call:
330 .PP
331 .Vb 1
332 \& tie %A, "DB_File" ;
333 .Ve
334 .PP
335 is equivalent to:
336 .PP
337 .Vb 1
338 \& tie %A, "DB_File", undef, O_CREAT|O_RDWR, 0666, $DB_HASH ;
339 .Ve
340 .PP
341 See "In Memory Databases" for a discussion on the use of \f(CW\*(C`undef\*(C'\fR
342 in place of a filename.
343 .SS "In Memory Databases"
344 .IX Subsection "In Memory Databases"
345 Berkeley DB allows the creation of in\-memory databases by using NULL
346 (that is, a \f(CW\*(C`(char *)0\*(C'\fR in C) in place of the filename. \fBDB_File\fR
347 uses \f(CW\*(C`undef\*(C'\fR instead of NULL to provide this functionality.
348 .SH DB_HASH
349 .IX Header "DB_HASH"
350 The DB_HASH file format is probably the most commonly used of the three
351 file formats that \fBDB_File\fR supports. It is also very straightforward
352 to use.
353 .SS "A Simple Example"
354 .IX Subsection "A Simple Example"
355 This example shows how to create a database, add key/value pairs to the
356 database, delete keys/value pairs and finally how to enumerate the
357 contents of the database.
358 .PP
359 .Vb 4
360 \& use warnings ;
361 \& use strict ;
362 \& use DB_File ;
363 \& our (%h, $k, $v) ;
364 \&
365 \& unlink "fruit" ;
366 \& tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0666, $DB_HASH
367 \& or die "Cannot open file \*(Aqfruit\*(Aq: $!\en";
368 \&
369 \& # Add a few key/value pairs to the file
370 \& $h{"apple"} = "red" ;
371 \& $h{"orange"} = "orange" ;
372 \& $h{"banana"} = "yellow" ;
373 \& $h{"tomato"} = "red" ;
374 \&
375 \& # Check for existence of a key
376 \& print "Banana Exists\en\en" if $h{"banana"} ;
377 \&
378 \& # Delete a key/value pair.
379 \& delete $h{"apple"} ;
380 \&
381 \& # print the contents of the file
382 \& while (($k, $v) = each %h)
383 \& { print "$k \-> $v\en" }
384 \&
385 \& untie %h ;
386 .Ve
387 .PP
388 here is the output:
389 .PP
390 .Vb 1
391 \& Banana Exists
392 \&
393 \& orange \-> orange
394 \& tomato \-> red
395 \& banana \-> yellow
396 .Ve
397 .PP
398 Note that the like ordinary associative arrays, the order of the keys
399 retrieved is in an apparently random order.
400 .SH DB_BTREE
401 .IX Header "DB_BTREE"
402 The DB_BTREE format is useful when you want to store data in a given
403 order. By default the keys will be stored in lexical order, but as you
404 will see from the example shown in the next section, it is very easy to
405 define your own sorting function.
406 .SS "Changing the BTREE sort order"
407 .IX Subsection "Changing the BTREE sort order"
408 This script shows how to override the default sorting algorithm that
409 BTREE uses. Instead of using the normal lexical ordering, a case
410 insensitive compare function will be used.
411 .PP
412 .Vb 3
413 \& use warnings ;
414 \& use strict ;
415 \& use DB_File ;
416 \&
417 \& my %h ;
418 \&
419 \& sub Compare
420 \& {
421 \& my ($key1, $key2) = @_ ;
422 \& "\eL$key1" cmp "\eL$key2" ;
423 \& }
424 \&
425 \& # specify the Perl sub that will do the comparison
426 \& $DB_BTREE\->{\*(Aqcompare\*(Aq} = \e&Compare ;
427 \&
428 \& unlink "tree" ;
429 \& tie %h, "DB_File", "tree", O_RDWR|O_CREAT, 0666, $DB_BTREE
430 \& or die "Cannot open file \*(Aqtree\*(Aq: $!\en" ;
431 \&
432 \& # Add a key/value pair to the file
433 \& $h{\*(AqWall\*(Aq} = \*(AqLarry\*(Aq ;
434 \& $h{\*(AqSmith\*(Aq} = \*(AqJohn\*(Aq ;
435 \& $h{\*(Aqmouse\*(Aq} = \*(Aqmickey\*(Aq ;
436 \& $h{\*(Aqduck\*(Aq} = \*(Aqdonald\*(Aq ;
437 \&
438 \& # Delete
439 \& delete $h{"duck"} ;
440 \&
441 \& # Cycle through the keys printing them in order.
442 \& # Note it is not necessary to sort the keys as
443 \& # the btree will have kept them in order automatically.
444 \& foreach (keys %h)
445 \& { print "$_\en" }
446 \&
447 \& untie %h ;
448 .Ve
449 .PP
450 Here is the output from the code above.
451 .PP
452 .Vb 3
453 \& mouse
454 \& Smith
455 \& Wall
456 .Ve
457 .PP
458 There are a few point to bear in mind if you want to change the
459 ordering in a BTREE database:
460 .IP 1. 5
461 The new compare function must be specified when you create the database.
462 .IP 2. 5
463 You cannot change the ordering once the database has been created. Thus
464 you must use the same compare function every time you access the
465 database.
466 .IP 3. 5
467 Duplicate keys are entirely defined by the comparison function.
468 In the case\-insensitive example above, the keys: \*(AqKEY\*(Aq and \*(Aqkey\*(Aq
469 would be considered duplicates, and assigning to the second one
470 would overwrite the first. If duplicates are allowed for (with the
471 R_DUP flag discussed below), only a single copy of duplicate keys
472 is stored in the database \-\-\- so (again with example above) assigning
473 three values to the keys: \*(AqKEY\*(Aq, \*(AqKey\*(Aq, and \*(Aqkey\*(Aq would leave just
474 the first key: \*(AqKEY\*(Aq in the database with three values. For some
475 situations this results in information loss, so care should be taken
476 to provide fully qualified comparison functions when necessary.
477 For example, the above comparison routine could be modified to
478 additionally compare case\-sensitively if two keys are equal in the
479 case insensitive comparison:
480 .Sp
481 .Vb 5
482 \& sub compare {
483 \& my($key1, $key2) = @_;
484 \& lc $key1 cmp lc $key2 ||
485 \& $key1 cmp $key2;
486 \& }
487 .Ve
488 .Sp
489 And now you will only have duplicates when the keys themselves
490 are truly the same. (note: in versions of the db library prior to
491 about November 1996, such duplicate keys were retained so it was
492 possible to recover the original keys in sets of keys that
493 compared as equal).
494 .SS "Handling Duplicate Keys"
495 .IX Subsection "Handling Duplicate Keys"
496 The BTREE file type optionally allows a single key to be associated
497 with an arbitrary number of values. This option is enabled by setting
498 the flags element of \f(CW$DB_BTREE\fR to R_DUP when creating the database.
499 .PP
500 There are some difficulties in using the tied hash interface if you
501 want to manipulate a BTREE database with duplicate keys. Consider this
502 code:
503 .PP
504 .Vb 3
505 \& use warnings ;
506 \& use strict ;
507 \& use DB_File ;
508 \&
509 \& my ($filename, %h) ;
510 \&
511 \& $filename = "tree" ;
512 \& unlink $filename ;
513 \&
514 \& # Enable duplicate records
515 \& $DB_BTREE\->{\*(Aqflags\*(Aq} = R_DUP ;
516 \&
517 \& tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
518 \& or die "Cannot open $filename: $!\en";
519 \&
520 \& # Add some key/value pairs to the file
521 \& $h{\*(AqWall\*(Aq} = \*(AqLarry\*(Aq ;
522 \& $h{\*(AqWall\*(Aq} = \*(AqBrick\*(Aq ; # Note the duplicate key
523 \& $h{\*(AqWall\*(Aq} = \*(AqBrick\*(Aq ; # Note the duplicate key and value
524 \& $h{\*(AqSmith\*(Aq} = \*(AqJohn\*(Aq ;
525 \& $h{\*(Aqmouse\*(Aq} = \*(Aqmickey\*(Aq ;
526 \&
527 \& # iterate through the associative array
528 \& # and print each key/value pair.
529 \& foreach (sort keys %h)
530 \& { print "$_ \-> $h{$_}\en" }
531 \&
532 \& untie %h ;
533 .Ve
534 .PP
535 Here is the output:
536 .PP
537 .Vb 5
538 \& Smith \-> John
539 \& Wall \-> Larry
540 \& Wall \-> Larry
541 \& Wall \-> Larry
542 \& mouse \-> mickey
543 .Ve
544 .PP
545 As you can see 3 records have been successfully created with key \f(CW\*(C`Wall\*(C'\fR
546 \&\- the only thing is, when they are retrieved from the database they
547 \&\fIseem\fR to have the same value, namely \f(CW\*(C`Larry\*(C'\fR. The problem is caused
548 by the way that the associative array interface works. Basically, when
549 the associative array interface is used to fetch the value associated
550 with a given key, it will only ever retrieve the first value.
551 .PP
552 Although it may not be immediately obvious from the code above, the
553 associative array interface can be used to write values with duplicate
554 keys, but it cannot be used to read them back from the database.
555 .PP
556 The way to get around this problem is to use the Berkeley DB API method
557 called \f(CW\*(C`seq\*(C'\fR. This method allows sequential access to key/value
558 pairs. See "THE API INTERFACE" for details of both the \f(CW\*(C`seq\*(C'\fR method
559 and the API in general.
560 .PP
561 Here is the script above rewritten using the \f(CW\*(C`seq\*(C'\fR API method.
562 .PP
563 .Vb 3
564 \& use warnings ;
565 \& use strict ;
566 \& use DB_File ;
567 \&
568 \& my ($filename, $x, %h, $status, $key, $value) ;
569 \&
570 \& $filename = "tree" ;
571 \& unlink $filename ;
572 \&
573 \& # Enable duplicate records
574 \& $DB_BTREE\->{\*(Aqflags\*(Aq} = R_DUP ;
575 \&
576 \& $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
577 \& or die "Cannot open $filename: $!\en";
578 \&
579 \& # Add some key/value pairs to the file
580 \& $h{\*(AqWall\*(Aq} = \*(AqLarry\*(Aq ;
581 \& $h{\*(AqWall\*(Aq} = \*(AqBrick\*(Aq ; # Note the duplicate key
582 \& $h{\*(AqWall\*(Aq} = \*(AqBrick\*(Aq ; # Note the duplicate key and value
583 \& $h{\*(AqSmith\*(Aq} = \*(AqJohn\*(Aq ;
584 \& $h{\*(Aqmouse\*(Aq} = \*(Aqmickey\*(Aq ;
585 \&
586 \& # iterate through the btree using seq
587 \& # and print each key/value pair.
588 \& $key = $value = 0 ;
589 \& for ($status = $x\->seq($key, $value, R_FIRST) ;
590 \& $status == 0 ;
591 \& $status = $x\->seq($key, $value, R_NEXT) )
592 \& { print "$key \-> $value\en" }
593 \&
594 \& undef $x ;
595 \& untie %h ;
596 .Ve
597 .PP
598 that prints:
599 .PP
600 .Vb 5
601 \& Smith \-> John
602 \& Wall \-> Brick
603 \& Wall \-> Brick
604 \& Wall \-> Larry
605 \& mouse \-> mickey
606 .Ve
607 .PP
608 This time we have got all the key/value pairs, including the multiple
609 values associated with the key \f(CW\*(C`Wall\*(C'\fR.
610 .PP
611 To make life easier when dealing with duplicate keys, \fBDB_File\fR comes with
612 a few utility methods.
613 .SS "The \fBget_dup()\fP Method"
614 .IX Subsection "The get_dup() Method"
615 The \f(CW\*(C`get_dup\*(C'\fR method assists in
616 reading duplicate values from BTREE databases. The method can take the
617 following forms:
618 .PP
619 .Vb 3
620 \& $count = $x\->get_dup($key) ;
621 \& @list = $x\->get_dup($key) ;
622 \& %list = $x\->get_dup($key, 1) ;
623 .Ve
624 .PP
625 In a scalar context the method returns the number of values associated
626 with the key, \f(CW$key\fR.
627 .PP
628 In list context, it returns all the values which match \f(CW$key\fR. Note
629 that the values will be returned in an apparently random order.
630 .PP
631 In list context, if the second parameter is present and evaluates
632 TRUE, the method returns an associative array. The keys of the
633 associative array correspond to the values that matched in the BTREE
634 and the values of the array are a count of the number of times that
635 particular value occurred in the BTREE.
636 .PP
637 So assuming the database created above, we can use \f(CW\*(C`get_dup\*(C'\fR like
638 this:
639 .PP
640 .Vb 3
641 \& use warnings ;
642 \& use strict ;
643 \& use DB_File ;
644 \&
645 \& my ($filename, $x, %h) ;
646 \&
647 \& $filename = "tree" ;
648 \&
649 \& # Enable duplicate records
650 \& $DB_BTREE\->{\*(Aqflags\*(Aq} = R_DUP ;
651 \&
652 \& $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
653 \& or die "Cannot open $filename: $!\en";
654 \&
655 \& my $cnt = $x\->get_dup("Wall") ;
656 \& print "Wall occurred $cnt times\en" ;
657 \&
658 \& my %hash = $x\->get_dup("Wall", 1) ;
659 \& print "Larry is there\en" if $hash{\*(AqLarry\*(Aq} ;
660 \& print "There are $hash{\*(AqBrick\*(Aq} Brick Walls\en" ;
661 \&
662 \& my @list = sort $x\->get_dup("Wall") ;
663 \& print "Wall => [@list]\en" ;
664 \&
665 \& @list = $x\->get_dup("Smith") ;
666 \& print "Smith => [@list]\en" ;
667 \&
668 \& @list = $x\->get_dup("Dog") ;
669 \& print "Dog => [@list]\en" ;
670 .Ve
671 .PP
672 and it will print:
673 .PP
674 .Vb 6
675 \& Wall occurred 3 times
676 \& Larry is there
677 \& There are 2 Brick Walls
678 \& Wall => [Brick Brick Larry]
679 \& Smith => [John]
680 \& Dog => []
681 .Ve
682 .SS "The \fBfind_dup()\fP Method"
683 .IX Subsection "The find_dup() Method"
684 .Vb 1
685 \& $status = $X\->find_dup($key, $value) ;
686 .Ve
687 .PP
688 This method checks for the existence of a specific key/value pair. If the
689 pair exists, the cursor is left pointing to the pair and the method
690 returns 0. Otherwise the method returns a non\-zero value.
691 .PP
692 Assuming the database from the previous example:
693 .PP
694 .Vb 3
695 \& use warnings ;
696 \& use strict ;
697 \& use DB_File ;
698 \&
699 \& my ($filename, $x, %h, $found) ;
700 \&
701 \& $filename = "tree" ;
702 \&
703 \& # Enable duplicate records
704 \& $DB_BTREE\->{\*(Aqflags\*(Aq} = R_DUP ;
705 \&
706 \& $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
707 \& or die "Cannot open $filename: $!\en";
708 \&
709 \& $found = ( $x\->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
710 \& print "Larry Wall is $found there\en" ;
711 \&
712 \& $found = ( $x\->find_dup("Wall", "Harry") == 0 ? "" : "not") ;
713 \& print "Harry Wall is $found there\en" ;
714 \&
715 \& undef $x ;
716 \& untie %h ;
717 .Ve
718 .PP
719 prints this
720 .PP
721 .Vb 2
722 \& Larry Wall is there
723 \& Harry Wall is not there
724 .Ve
725 .SS "The \fBdel_dup()\fP Method"
726 .IX Subsection "The del_dup() Method"
727 .Vb 1
728 \& $status = $X\->del_dup($key, $value) ;
729 .Ve
730 .PP
731 This method deletes a specific key/value pair. It returns
732 0 if they exist and have been deleted successfully.
733 Otherwise the method returns a non\-zero value.
734 .PP
735 Again assuming the existence of the \f(CW\*(C`tree\*(C'\fR database
736 .PP
737 .Vb 3
738 \& use warnings ;
739 \& use strict ;
740 \& use DB_File ;
741 \&
742 \& my ($filename, $x, %h, $found) ;
743 \&
744 \& $filename = "tree" ;
745 \&
746 \& # Enable duplicate records
747 \& $DB_BTREE\->{\*(Aqflags\*(Aq} = R_DUP ;
748 \&
749 \& $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
750 \& or die "Cannot open $filename: $!\en";
751 \&
752 \& $x\->del_dup("Wall", "Larry") ;
753 \&
754 \& $found = ( $x\->find_dup("Wall", "Larry") == 0 ? "" : "not") ;
755 \& print "Larry Wall is $found there\en" ;
756 \&
757 \& undef $x ;
758 \& untie %h ;
759 .Ve
760 .PP
761 prints this
762 .PP
763 .Vb 1
764 \& Larry Wall is not there
765 .Ve
766 .SS "Matching Partial Keys"
767 .IX Subsection "Matching Partial Keys"
768 The BTREE interface has a feature which allows partial keys to be
769 matched. This functionality is \fIonly\fR available when the \f(CW\*(C`seq\*(C'\fR method
770 is used along with the R_CURSOR flag.
771 .PP
772 .Vb 1
773 \& $x\->seq($key, $value, R_CURSOR) ;
774 .Ve
775 .PP
776 Here is the relevant quote from the dbopen man page where it defines
777 the use of the R_CURSOR flag with seq:
778 .PP
779 .Vb 4
780 \& Note, for the DB_BTREE access method, the returned key is not
781 \& necessarily an exact match for the specified key. The returned key
782 \& is the smallest key greater than or equal to the specified key,
783 \& permitting partial key matches and range searches.
784 .Ve
785 .PP
786 In the example script below, the \f(CW\*(C`match\*(C'\fR sub uses this feature to find
787 and print the first matching key/value pair given a partial key.
788 .PP
789 .Vb 4
790 \& use warnings ;
791 \& use strict ;
792 \& use DB_File ;
793 \& use Fcntl ;
794 \&
795 \& my ($filename, $x, %h, $st, $key, $value) ;
796 \&
797 \& sub match
798 \& {
799 \& my $key = shift ;
800 \& my $value = 0;
801 \& my $orig_key = $key ;
802 \& $x\->seq($key, $value, R_CURSOR) ;
803 \& print "$orig_key\et\-> $key\et\-> $value\en" ;
804 \& }
805 \&
806 \& $filename = "tree" ;
807 \& unlink $filename ;
808 \&
809 \& $x = tie %h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE
810 \& or die "Cannot open $filename: $!\en";
811 \&
812 \& # Add some key/value pairs to the file
813 \& $h{\*(Aqmouse\*(Aq} = \*(Aqmickey\*(Aq ;
814 \& $h{\*(AqWall\*(Aq} = \*(AqLarry\*(Aq ;
815 \& $h{\*(AqWalls\*(Aq} = \*(AqBrick\*(Aq ;
816 \& $h{\*(AqSmith\*(Aq} = \*(AqJohn\*(Aq ;
817 \&
818 \&
819 \& $key = $value = 0 ;
820 \& print "IN ORDER\en" ;
821 \& for ($st = $x\->seq($key, $value, R_FIRST) ;
822 \& $st == 0 ;
823 \& $st = $x\->seq($key, $value, R_NEXT) )
824 \&
825 \& { print "$key \-> $value\en" }
826 \&
827 \& print "\enPARTIAL MATCH\en" ;
828 \&
829 \& match "Wa" ;
830 \& match "A" ;
831 \& match "a" ;
832 \&
833 \& undef $x ;
834 \& untie %h ;
835 .Ve
836 .PP
837 Here is the output:
838 .PP
839 .Vb 5
840 \& IN ORDER
841 \& Smith \-> John
842 \& Wall \-> Larry
843 \& Walls \-> Brick
844 \& mouse \-> mickey
845 \&
846 \& PARTIAL MATCH
847 \& Wa \-> Wall \-> Larry
848 \& A \-> Smith \-> John
849 \& a \-> mouse \-> mickey
850 .Ve
851 .SH DB_RECNO
852 .IX Header "DB_RECNO"
853 DB_RECNO provides an interface to flat text files. Both variable and
854 fixed length records are supported.
855 .PP
856 In order to make RECNO more compatible with Perl, the array offset for
857 all RECNO arrays begins at 0 rather than 1 as in Berkeley DB.
858 .PP
859 As with normal Perl arrays, a RECNO array can be accessed using
860 negative indexes. The index \-1 refers to the last element of the array,
861 \&\-2 the second last, and so on. Attempting to access an element before
862 the start of the array will raise a fatal run\-time error.
863 .SS "The \*(Aqbval\*(Aq Option"
864 .IX Subsection "The 'bval' Option"
865 The operation of the bval option warrants some discussion. Here is the
866 definition of bval from the Berkeley DB 1.85 recno manual page:
867 .PP
868 .Vb 6
869 \& The delimiting byte to be used to mark the end of a
870 \& record for variable\-length records, and the pad charac\-
871 \& ter for fixed\-length records. If no value is speci\-
872 \& fied, newlines (\`\`\en\*(Aq\*(Aq) are used to mark the end of
873 \& variable\-length records and fixed\-length records are
874 \& padded with spaces.
875 .Ve
876 .PP
877 The second sentence is wrong. In actual fact bval will only default to
878 \&\f(CW"\en"\fR when the openinfo parameter in dbopen is NULL. If a non\-NULL
879 openinfo parameter is used at all, the value that happens to be in bval
880 will be used. That means you always have to specify bval when making
881 use of any of the options in the openinfo parameter. This documentation
882 error will be fixed in the next release of Berkeley DB.
883 .PP
884 That clarifies the situation with regards Berkeley DB itself. What
885 about \fBDB_File\fR? Well, the behavior defined in the quote above is
886 quite useful, so \fBDB_File\fR conforms to it.
887 .PP
888 That means that you can specify other options (e.g. cachesize) and
889 still have bval default to \f(CW"\en"\fR for variable length records, and
890 space for fixed length records.
891 .PP
892 Also note that the bval option only allows you to specify a single byte
893 as a delimiter.
894 .SS "A Simple Example"
895 .IX Subsection "A Simple Example"
896 Here is a simple example that uses RECNO (if you are using a version
897 of Perl earlier than 5.004_57 this example won\*(Aqt work \-\- see
898 "Extra RECNO Methods" for a workaround).
899 .PP
900 .Vb 3
901 \& use warnings ;
902 \& use strict ;
903 \& use DB_File ;
904 \&
905 \& my $filename = "text" ;
906 \& unlink $filename ;
907 \&
908 \& my @h ;
909 \& tie @h, "DB_File", $filename, O_RDWR|O_CREAT, 0666, $DB_RECNO
910 \& or die "Cannot open file \*(Aqtext\*(Aq: $!\en" ;
911 \&
912 \& # Add a few key/value pairs to the file
913 \& $h[0] = "orange" ;
914 \& $h[1] = "blue" ;
915 \& $h[2] = "yellow" ;
916 \&
917 \& push @h, "green", "black" ;
918 \&
919 \& my $elements = scalar @h ;
920 \& print "The array contains $elements entries\en" ;
921 \&
922 \& my $last = pop @h ;
923 \& print "popped $last\en" ;
924 \&
925 \& unshift @h, "white" ;
926 \& my $first = shift @h ;
927 \& print "shifted $first\en" ;
928 \&
929 \& # Check for existence of a key
930 \& print "Element 1 Exists with value $h[1]\en" if $h[1] ;
931 \&
932 \& # use a negative index
933 \& print "The last element is $h[\-1]\en" ;
934 \& print "The 2nd last element is $h[\-2]\en" ;
935 \&
936 \& untie @h ;
937 .Ve
938 .PP
939 Here is the output from the script:
940 .PP
941 .Vb 6
942 \& The array contains 5 entries
943 \& popped black
944 \& shifted white
945 \& Element 1 Exists with value blue
946 \& The last element is green
947 \& The 2nd last element is yellow
948 .Ve
949 .SS "Extra RECNO Methods"
950 .IX Subsection "Extra RECNO Methods"
951 If you are using a version of Perl earlier than 5.004_57, the tied
952 array interface is quite limited. In the example script above
953 \&\f(CW\*(C`push\*(C'\fR, \f(CW\*(C`pop\*(C'\fR, \f(CW\*(C`shift\*(C'\fR, \f(CW\*(C`unshift\*(C'\fR
954 or determining the array length will not work with a tied array.
955 .PP
956 To make the interface more useful for older versions of Perl, a number
957 of methods are supplied with \fBDB_File\fR to simulate the missing array
958 operations. All these methods are accessed via the object returned from
959 the tie call.
960 .PP
961 Here are the methods:
962 .ie n .IP "\fR\fB$X\fR\fB\->push(list) ;\fR" 5
963 .el .IP "\fR\f(CB$X\fR\fB\->push(list) ;\fR" 5
964 .IX Item "$X->push(list) ;"
965 Pushes the elements of \f(CW\*(C`list\*(C'\fR to the end of the array.
966 .ie n .IP "\fR\fB$value\fR\fB = \fR\fB$X\fR\fB\->pop ;\fR" 5
967 .el .IP "\fR\f(CB$value\fR\fB = \fR\f(CB$X\fR\fB\->pop ;\fR" 5
968 .IX Item "$value = $X->pop ;"
969 Removes and returns the last element of the array.
970 .ie n .IP \fR\fB$X\fR\fB\->shift\fR 5
971 .el .IP \fR\f(CB$X\fR\fB\->shift\fR 5
972 .IX Item "$X->shift"
973 Removes and returns the first element of the array.
974 .ie n .IP "\fR\fB$X\fR\fB\->unshift(list) ;\fR" 5
975 .el .IP "\fR\f(CB$X\fR\fB\->unshift(list) ;\fR" 5
976 .IX Item "$X->unshift(list) ;"
977 Pushes the elements of \f(CW\*(C`list\*(C'\fR to the start of the array.
978 .ie n .IP \fR\fB$X\fR\fB\->length\fR 5
979 .el .IP \fR\f(CB$X\fR\fB\->length\fR 5
980 .IX Item "$X->length"
981 Returns the number of elements in the array.
982 .ie n .IP "\fR\fB$X\fR\fB\->splice(offset, length, elements);\fR" 5
983 .el .IP "\fR\f(CB$X\fR\fB\->splice(offset, length, elements);\fR" 5
984 .IX Item "$X->splice(offset, length, elements);"
985 Returns a splice of the array.
986 .SS "Another Example"
987 .IX Subsection "Another Example"
988 Here is a more complete example that makes use of some of the methods
989 described above. It also makes use of the API interface directly (see
990 "THE API INTERFACE").
991 .PP
992 .Vb 5
993 \& use warnings ;
994 \& use strict ;
995 \& my (@h, $H, $file, $i) ;
996 \& use DB_File ;
997 \& use Fcntl ;
998 \&
999 \& $file = "text" ;
1000 \&
1001 \& unlink $file ;
1002 \&
1003 \& $H = tie @h, "DB_File", $file, O_RDWR|O_CREAT, 0666, $DB_RECNO
1004 \& or die "Cannot open file $file: $!\en" ;
1005 \&
1006 \& # first create a text file to play with
1007 \& $h[0] = "zero" ;
1008 \& $h[1] = "one" ;
1009 \& $h[2] = "two" ;
1010 \& $h[3] = "three" ;
1011 \& $h[4] = "four" ;
1012 \&
1013 \&
1014 \& # Print the records in order.
1015 \& #
1016 \& # The length method is needed here because evaluating a tied
1017 \& # array in a scalar context does not return the number of
1018 \& # elements in the array.
1019 \&
1020 \& print "\enORIGINAL\en" ;
1021 \& foreach $i (0 .. $H\->length \- 1) {
1022 \& print "$i: $h[$i]\en" ;
1023 \& }
1024 \&
1025 \& # use the push & pop methods
1026 \& $a = $H\->pop ;
1027 \& $H\->push("last") ;
1028 \& print "\enThe last record was [$a]\en" ;
1029 \&
1030 \& # and the shift & unshift methods
1031 \& $a = $H\->shift ;
1032 \& $H\->unshift("first") ;
1033 \& print "The first record was [$a]\en" ;
1034 \&
1035 \& # Use the API to add a new record after record 2.
1036 \& $i = 2 ;
1037 \& $H\->put($i, "Newbie", R_IAFTER) ;
1038 \&
1039 \& # and a new record before record 1.
1040 \& $i = 1 ;
1041 \& $H\->put($i, "New One", R_IBEFORE) ;
1042 \&
1043 \& # delete record 3
1044 \& $H\->del(3) ;
1045 \&
1046 \& # now print the records in reverse order
1047 \& print "\enREVERSE\en" ;
1048 \& for ($i = $H\->length \- 1 ; $i >= 0 ; \-\- $i)
1049 \& { print "$i: $h[$i]\en" }
1050 \&
1051 \& # same again, but use the API functions instead
1052 \& print "\enREVERSE again\en" ;
1053 \& my ($s, $k, $v) = (0, 0, 0) ;
1054 \& for ($s = $H\->seq($k, $v, R_LAST) ;
1055 \& $s == 0 ;
1056 \& $s = $H\->seq($k, $v, R_PREV))
1057 \& { print "$k: $v\en" }
1058 \&
1059 \& undef $H ;
1060 \& untie @h ;
1061 .Ve
1062 .PP
1063 and this is what it outputs:
1064 .PP
1065 .Vb 6
1066 \& ORIGINAL
1067 \& 0: zero
1068 \& 1: one
1069 \& 2: two
1070 \& 3: three
1071 \& 4: four
1072 \&
1073 \& The last record was [four]
1074 \& The first record was [zero]
1075 \&
1076 \& REVERSE
1077 \& 5: last
1078 \& 4: three
1079 \& 3: Newbie
1080 \& 2: one
1081 \& 1: New One
1082 \& 0: first
1083 \&
1084 \& REVERSE again
1085 \& 5: last
1086 \& 4: three
1087 \& 3: Newbie
1088 \& 2: one
1089 \& 1: New One
1090 \& 0: first
1091 .Ve
1092 .PP
1093 Notes:
1094 .IP 1. 5
1095 Rather than iterating through the array, \f(CW@h\fR like this:
1096 .Sp
1097 .Vb 1
1098 \& foreach $i (@h)
1099 .Ve
1100 .Sp
1101 it is necessary to use either this:
1102 .Sp
1103 .Vb 1
1104 \& foreach $i (0 .. $H\->length \- 1)
1105 .Ve
1106 .Sp
1107 or this:
1108 .Sp
1109 .Vb 3
1110 \& for ($a = $H\->get($k, $v, R_FIRST) ;
1111 \& $a == 0 ;
1112 \& $a = $H\->get($k, $v, R_NEXT) )
1113 .Ve
1114 .IP 2. 5
1115 Notice that both times the \f(CW\*(C`put\*(C'\fR method was used the record index was
1116 specified using a variable, \f(CW$i\fR, rather than the literal value
1117 itself. This is because \f(CW\*(C`put\*(C'\fR will return the record number of the
1118 inserted line via that parameter.
1119 .SH "THE API INTERFACE"
1120 .IX Header "THE API INTERFACE"
1121 As well as accessing Berkeley DB using a tied hash or array, it is also
1122 possible to make direct use of most of the API functions defined in the
1123 Berkeley DB documentation.
1124 .PP
1125 To do this you need to store a copy of the object returned from the tie.
1126 .PP
1127 .Vb 1
1128 \& $db = tie %hash, "DB_File", "filename" ;
1129 .Ve
1130 .PP
1131 Once you have done that, you can access the Berkeley DB API functions
1132 as \fBDB_File\fR methods directly like this:
1133 .PP
1134 .Vb 1
1135 \& $db\->put($key, $value, R_NOOVERWRITE) ;
1136 .Ve
1137 .PP
1138 \&\fBImportant:\fR If you have saved a copy of the object returned from
1139 \&\f(CW\*(C`tie\*(C'\fR, the underlying database file will \fInot\fR be closed until both
1140 the tied variable is untied and all copies of the saved object are
1141 destroyed.
1142 .PP
1143 .Vb 6
1144 \& use DB_File ;
1145 \& $db = tie %hash, "DB_File", "filename"
1146 \& or die "Cannot tie filename: $!" ;
1147 \& ...
1148 \& undef $db ;
1149 \& untie %hash ;
1150 .Ve
1151 .PP
1152 See "The \fBuntie()\fR Gotcha" for more details.
1153 .PP
1154 All the functions defined in dbopen are available except for
1155 \&\fBclose()\fR and \fBdbopen()\fR itself. The \fBDB_File\fR method interface to the
1156 supported functions have been implemented to mirror the way Berkeley DB
1157 works whenever possible. In particular note that:
1158 .IP \(bu 5
1159 The methods return a status value. All return 0 on success.
1160 All return \-1 to signify an error and set \f(CW$!\fR to the exact
1161 error code. The return code 1 generally (but not always) means that the
1162 key specified did not exist in the database.
1163 .Sp
1164 Other return codes are defined. See below and in the Berkeley DB
1165 documentation for details. The Berkeley DB documentation should be used
1166 as the definitive source.
1167 .IP \(bu 5
1168 Whenever a Berkeley DB function returns data via one of its parameters,
1169 the equivalent \fBDB_File\fR method does exactly the same.
1170 .IP \(bu 5
1171 If you are careful, it is possible to mix API calls with the tied
1172 hash/array interface in the same piece of code. Although only a few of
1173 the methods used to implement the tied interface currently make use of
1174 the cursor, you should always assume that the cursor has been changed
1175 any time the tied hash/array interface is used. As an example, this
1176 code will probably not do what you expect:
1177 .Sp
1178 .Vb 2
1179 \& $X = tie %x, \*(AqDB_File\*(Aq, $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\*(Aqt, it got the last key/value pair!
1190 \& $X\->seq($key, $value, R_NEXT) ;
1191 .Ve
1192 .Sp
1193 The code above can be rearranged to get around the problem, like this:
1194 .Sp
1195 .Vb 2
1196 \& $X = tie %x, \*(AqDB_File\*(Aq, $filename, O_RDWR|O_CREAT, 0777, $DB_BTREE
1197 \& or die "Cannot tie $filename: $!" ;
1198 \&
1199 \& # this line will modify the cursor
1200 \& $count = scalar keys %x ;
1201 \&
1202 \& # Get the first key/value pair and set the cursor
1203 \& $X\->seq($key, $value, R_FIRST) ;
1204 \&
1205 \& # Get the second key/value pair.
1206 \& # worked this time.
1207 \& $X\->seq($key, $value, R_NEXT) ;
1208 .Ve
1209 .PP
1210 All the constants defined in dbopen for use in the flags parameters
1211 in the methods defined below are also available. Refer to the Berkeley
1212 DB documentation for the precise meaning of the flags values.
1213 .PP
1214 Below is a list of the methods available.
1215 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->get($key, \fR\fB$value\fR\fB [, \fR\fB$flags\fR\fB]) ;\fR" 5
1216 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->get($key, \fR\f(CB$value\fR\fB [, \fR\f(CB$flags\fR\fB]) ;\fR" 5
1217 .IX Item "$status = $X->get($key, $value [, $flags]) ;"
1218 Given a key (\f(CW$key\fR) this method reads the value associated with it
1219 from the database. The value read from the database is returned in the
1220 \&\f(CW$value\fR parameter.
1221 .Sp
1222 If the key does not exist the method returns 1.
1223 .Sp
1224 No flags are currently defined for this method.
1225 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->put($key, \fR\fB$value\fR\fB [, \fR\fB$flags\fR\fB]) ;\fR" 5
1226 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->put($key, \fR\f(CB$value\fR\fB [, \fR\f(CB$flags\fR\fB]) ;\fR" 5
1227 .IX Item "$status = $X->put($key, $value [, $flags]) ;"
1228 Stores the key/value pair in the database.
1229 .Sp
1230 If you use either the R_IAFTER or R_IBEFORE flags, the \f(CW$key\fR parameter
1231 will have the record number of the inserted key/value pair set.
1232 .Sp
1233 Valid flags are R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE and
1234 R_SETCURSOR.
1235 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->del($key [, \fR\fB$flags\fR\fB]) ;\fR" 5
1236 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->del($key [, \fR\f(CB$flags\fR\fB]) ;\fR" 5
1237 .IX Item "$status = $X->del($key [, $flags]) ;"
1238 Removes all key/value pairs with key \f(CW$key\fR from the database.
1239 .Sp
1240 A return code of 1 means that the requested key was not in the
1241 database.
1242 .Sp
1243 R_CURSOR is the only valid flag at present.
1244 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->fd ;\fR" 5
1245 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->fd ;\fR" 5
1246 .IX Item "$status = $X->fd ;"
1247 Returns the file descriptor for the underlying database.
1248 .Sp
1249 See "Locking: The Trouble with fd" for an explanation for why you should
1250 not use \f(CW\*(C`fd\*(C'\fR to lock your database.
1251 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->seq($key, \fR\fB$value\fR\fB, \fR\fB$flags\fR\fB) ;\fR" 5
1252 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->seq($key, \fR\f(CB$value\fR\fB, \fR\f(CB$flags\fR\fB) ;\fR" 5
1253 .IX Item "$status = $X->seq($key, $value, $flags) ;"
1254 This interface allows sequential retrieval from the database. See
1255 dbopen for full details.
1256 .Sp
1257 Both the \f(CW$key\fR and \f(CW$value\fR parameters will be set to the key/value
1258 pair read from the database.
1259 .Sp
1260 The flags parameter is mandatory. The valid flag values are R_CURSOR,
1261 R_FIRST, R_LAST, R_NEXT and R_PREV.
1262 .ie n .IP "\fR\fB$status\fR\fB = \fR\fB$X\fR\fB\->sync([$flags]) ;\fR" 5
1263 .el .IP "\fR\f(CB$status\fR\fB = \fR\f(CB$X\fR\fB\->sync([$flags]) ;\fR" 5
1264 .IX Item "$status = $X->sync([$flags]) ;"
1265 Flushes any cached buffers to disk.
1266 .Sp
1267 R_RECNOSYNC is the only valid flag at present.
1268 .SH "DBM FILTERS"
1269 .IX Header "DBM FILTERS"
1270 A DBM Filter is a piece of code that is be used when you \fIalways\fR want to
1271 make the same transformation to all keys and/or values in a DBM database.
1272 An example is when you need to encode your data in UTF\-8 before writing to
1273 the database and then decode the UTF\-8 when reading from the database file.
1274 .PP
1275 There are two ways to use a DBM Filter.
1276 .IP 1. 5
1277 Using the low\-level API defined below.
1278 .IP 2. 5
1279 Using the DBM_Filter module.
1280 This module hides the complexity of the API defined below and comes
1281 with a number of "canned" filters that cover some of the common use\-cases.
1282 .PP
1283 Use of the DBM_Filter module is recommended.
1284 .SS "DBM Filter Low\-level API"
1285 .IX Subsection "DBM Filter Low-level API"
1286 There are four methods associated with DBM Filters. All work identically,
1287 and each is used to install (or uninstall) a single DBM Filter. Each
1288 expects a single parameter, namely a reference to a sub. The only
1289 difference between them is the place that the filter is installed.
1290 .PP
1291 To summarise:
1292 .IP \fBfilter_store_key\fR 5
1293 .IX Item "filter_store_key"
1294 If a filter has been installed with this method, it will be invoked
1295 every time you write a key to a DBM database.
1296 .IP \fBfilter_store_value\fR 5
1297 .IX Item "filter_store_value"
1298 If a filter has been installed with this method, it will be invoked
1299 every time you write a value to a DBM database.
1300 .IP \fBfilter_fetch_key\fR 5
1301 .IX Item "filter_fetch_key"
1302 If a filter has been installed with this method, it will be invoked
1303 every time you read a key from a DBM database.
1304 .IP \fBfilter_fetch_value\fR 5
1305 .IX Item "filter_fetch_value"
1306 If a filter has been installed with this method, it will be invoked
1307 every time you read a value from a DBM database.
1308 .PP
1309 You can use any combination of the methods, from none, to all four.
1310 .PP
1311 All filter methods return the existing filter, if present, or \f(CW\*(C`undef\*(C'\fR
1312 in not.
1313 .PP
1314 To delete a filter pass \f(CW\*(C`undef\*(C'\fR to it.
1315 .SS "The Filter"
1316 .IX Subsection "The Filter"
1317 When each filter is called by Perl, a local copy of \f(CW$_\fR will contain
1318 the key or value to be filtered. Filtering is achieved by modifying
1319 the contents of \f(CW$_\fR. The return code from the filter is ignored.
1320 .SS "An Example \-\- the NULL termination problem."
1321 .IX Subsection "An Example -- the NULL termination problem."
1322 Consider the following scenario. You have a DBM database
1323 that you need to share with a third\-party C application. The C application
1324 assumes that \fIall\fR keys and values are NULL terminated. Unfortunately
1325 when Perl writes to DBM databases it doesn\*(Aqt use NULL termination, so
1326 your Perl application will have to manage NULL termination itself. When
1327 you write to the database you will have to use something like this:
1328 .PP
1329 .Vb 1
1330 \& $hash{"$key\e0"} = "$value\e0" ;
1331 .Ve
1332 .PP
1333 Similarly the NULL needs to be taken into account when you are considering
1334 the length of existing keys/values.
1335 .PP
1336 It would be much better if you could ignore the NULL terminations issue
1337 in the main application code and have a mechanism that automatically
1338 added the terminating NULL to all keys and values whenever you write to
1339 the database and have them removed when you read from the database. As I\*(Aqm
1340 sure you have already guessed, this is a problem that DBM Filters can
1341 fix very easily.
1342 .PP
1343 .Vb 3
1344 \& use warnings ;
1345 \& use strict ;
1346 \& use DB_File ;
1347 \&
1348 \& my %hash ;
1349 \& my $filename = "filt" ;
1350 \& unlink $filename ;
1351 \&
1352 \& my $db = tie %hash, \*(AqDB_File\*(Aq, $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
1353 \& or die "Cannot open $filename: $!\en" ;
1354 \&
1355 \& # Install DBM Filters
1356 \& $db\->filter_fetch_key ( sub { s/\e0$// } ) ;
1357 \& $db\->filter_store_key ( sub { $_ .= "\e0" } ) ;
1358 \& $db\->filter_fetch_value( sub { s/\e0$// } ) ;
1359 \& $db\->filter_store_value( sub { $_ .= "\e0" } ) ;
1360 \&
1361 \& $hash{"abc"} = "def" ;
1362 \& my $a = $hash{"ABC"} ;
1363 \& # ...
1364 \& undef $db ;
1365 \& untie %hash ;
1366 .Ve
1367 .PP
1368 Hopefully the contents of each of the filters should be
1369 self\-explanatory. Both "fetch" filters remove the terminating NULL,
1370 and both "store" filters add a terminating NULL.
1371 .SS "Another Example \-\- Key is a C int."
1372 .IX Subsection "Another Example -- Key is a C int."
1373 Here is another real\-life example. By default, whenever Perl writes to
1374 a DBM database it always writes the key and value as strings. So when
1375 you use this:
1376 .PP
1377 .Vb 1
1378 \& $hash{12345} = "something" ;
1379 .Ve
1380 .PP
1381 the key 12345 will get stored in the DBM database as the 5 byte string
1382 "12345". If you actually want the key to be stored in the DBM database
1383 as a C int, you will have to use \f(CW\*(C`pack\*(C'\fR when writing, and \f(CW\*(C`unpack\*(C'\fR
1384 when reading.
1385 .PP
1386 Here is a DBM Filter that does it:
1387 .PP
1388 .Vb 6
1389 \& use warnings ;
1390 \& use strict ;
1391 \& use DB_File ;
1392 \& my %hash ;
1393 \& my $filename = "filt" ;
1394 \& unlink $filename ;
1395 \&
1396 \&
1397 \& my $db = tie %hash, \*(AqDB_File\*(Aq, $filename, O_CREAT|O_RDWR, 0666, $DB_HASH
1398 \& or die "Cannot open $filename: $!\en" ;
1399 \&
1400 \& $db\->filter_fetch_key ( sub { $_ = unpack("i", $_) } ) ;
1401 \& $db\->filter_store_key ( sub { $_ = pack ("i", $_) } ) ;
1402 \& $hash{123} = "def" ;
1403 \& # ...
1404 \& undef $db ;
1405 \& untie %hash ;
1406 .Ve
1407 .PP
1408 This time only two filters have been used \-\- we only need to manipulate
1409 the contents of the key, so it wasn\*(Aqt necessary to install any value
1410 filters.
1411 .SH "HINTS AND TIPS"
1412 .IX Header "HINTS AND TIPS"
1413 .SS "Locking: The Trouble with fd"
1414 .IX Subsection "Locking: The Trouble with fd"
1415 Until version 1.72 of this module, the recommended technique for locking
1416 \&\fBDB_File\fR databases was to flock the filehandle returned from the "fd"
1417 function. Unfortunately this technique has been shown to be fundamentally
1418 flawed (Kudos to David Harris for tracking this down). Use it at your own
1419 peril!
1420 .PP
1421 The locking technique went like this.
1422 .PP
1423 .Vb 12
1424 \& $db = tie(%db, \*(AqDB_File\*(Aq, \*(Aqfoo.db\*(Aq, O_CREAT|O_RDWR, 0644)
1425 \& || die "dbcreat foo.db $!";
1426 \& $fd = $db\->fd;
1427 \& open(DB_FH, "+<&=$fd") || die "dup $!";
1428 \& flock (DB_FH, LOCK_EX) || die "flock: $!";
1429 \& ...
1430 \& $db{"Tom"} = "Jerry" ;
1431 \& ...
1432 \& flock(DB_FH, LOCK_UN);
1433 \& undef $db;
1434 \& untie %db;
1435 \& close(DB_FH);
1436 .Ve
1437 .PP
1438 In simple terms, this is what happens:
1439 .IP 1. 5
1440 Use "tie" to open the database.
1441 .IP 2. 5
1442 Lock the database with fd & flock.
1443 .IP 3. 5
1444 Read & Write to the database.
1445 .IP 4. 5
1446 Unlock and close the database.
1447 .PP
1448 Here is the crux of the problem. A side\-effect of opening the \fBDB_File\fR
1449 database in step 2 is that an initial block from the database will get
1450 read from disk and cached in memory.
1451 .PP
1452 To see why this is a problem, consider what can happen when two processes,
1453 say "A" and "B", both want to update the same \fBDB_File\fR database
1454 using the locking steps outlined above. Assume process "A" has already
1455 opened the database and has a write lock, but it hasn\*(Aqt actually updated
1456 the database yet (it has finished step 2, but not started step 3 yet). Now
1457 process "B" tries to open the same database \- step 1 will succeed,
1458 but it will block on step 2 until process "A" releases the lock. The
1459 important thing to notice here is that at this point in time both
1460 processes will have cached identical initial blocks from the database.
1461 .PP
1462 Now process "A" updates the database and happens to change some of the
1463 data held in the initial buffer. Process "A" terminates, flushing
1464 all cached data to disk and releasing the database lock. At this point
1465 the database on disk will correctly reflect the changes made by process
1466 "A".
1467 .PP
1468 With the lock released, process "B" can now continue. It also updates the
1469 database and unfortunately it too modifies the data that was in its
1470 initial buffer. Once that data gets flushed to disk it will overwrite
1471 some/all of the changes process "A" made to the database.
1472 .PP
1473 The result of this scenario is at best a database that doesn\*(Aqt contain
1474 what you expect. At worst the database will corrupt.
1475 .PP
1476 The above won\*(Aqt happen every time competing process update the same
1477 \&\fBDB_File\fR database, but it does illustrate why the technique should
1478 not be used.
1479 .SS "Safe ways to lock a database"
1480 .IX Subsection "Safe ways to lock a database"
1481 Starting with version 2.x, Berkeley DB has internal support for locking.
1482 The companion module to this one, BerkeleyDB <https://metacpan.org/pod/BerkeleyDB>, provides an interface
1483 to this locking functionality. If you are serious about locking
1484 Berkeley DB databases, I strongly recommend using BerkeleyDB <https://metacpan.org/pod/BerkeleyDB>.
1485 .PP
1486 If using BerkeleyDB <https://metacpan.org/pod/BerkeleyDB> isn\*(Aqt an option, there are a number of modules
1487 available on CPAN that can be used to implement locking. Each one
1488 implements locking differently and has different goals in mind. It is
1489 therefore worth knowing the difference, so that you can pick the right
1490 one for your application. Here are the three locking wrappers:
1491 .IP \fBTie::DB_Lock\fR 5
1492 .IX Item "Tie::DB_Lock"
1493 A \fBDB_File\fR wrapper which creates copies of the database file for
1494 read access, so that you have a kind of a multiversioning concurrent read
1495 system. However, updates are still serial. Use for databases where reads
1496 may be lengthy and consistency problems may occur.
1497 .IP \fBTie::DB_LockFile\fR 5
1498 .IX Item "Tie::DB_LockFile"
1499 A \fBDB_File\fR wrapper that has the ability to lock and unlock the database
1500 while it is being used. Avoids the tie\-before\-flock problem by simply
1501 re\-tie\-ing the database when you get or drop a lock. Because of the
1502 flexibility in dropping and re\-acquiring the lock in the middle of a
1503 session, this can be massaged into a system that will work with long
1504 updates and/or reads if the application follows the hints in the POD
1505 documentation.
1506 .IP \fBDB_File::Lock\fR 5
1507 .IX Item "DB_File::Lock"
1508 An extremely lightweight \fBDB_File\fR wrapper that simply flocks a lockfile
1509 before tie\-ing the database and drops the lock after the untie. Allows
1510 one to use the same lockfile for multiple databases to avoid deadlock
1511 problems, if desired. Use for databases where updates are reads are
1512 quick and simple flock locking semantics are enough.
1513 .SS "Sharing Databases With C Applications"
1514 .IX Subsection "Sharing Databases With C Applications"
1515 There is no technical reason why a Berkeley DB database cannot be
1516 shared by both a Perl and a C application.
1517 .PP
1518 The vast majority of problems that are reported in this area boil down
1519 to the fact that C strings are NULL terminated, whilst Perl strings are
1520 not. See "DBM FILTERS" for a generic way to work around this problem.
1521 .PP
1522 Here is a real example. Netscape 2.0 keeps a record of the locations you
1523 visit along with the time you last visited them in a DB_HASH database.
1524 This is usually stored in the file \fI~/.netscape/history.db\fR. The key
1525 field in the database is the location string and the value field is the
1526 time the location was last visited stored as a 4 byte binary value.
1527 .PP
1528 If you haven\*(Aqt already guessed, the location string is stored with a
1529 terminating NULL. This means you need to be careful when accessing the
1530 database.
1531 .PP
1532 Here is a snippet of code that is loosely based on Tom Christiansen\*(Aqs
1533 \&\fIggh\fR script (available from your nearest CPAN archive in
1534 \&\fIauthors/id/TOMC/scripts/nshist.gz\fR).
1535 .PP
1536 .Vb 4
1537 \& use warnings ;
1538 \& use strict ;
1539 \& use DB_File ;
1540 \& use Fcntl ;
1541 \&
1542 \& my ($dotdir, $HISTORY, %hist_db, $href, $binary_time, $date) ;
1543 \& $dotdir = $ENV{HOME} || $ENV{LOGNAME};
1544 \&
1545 \& $HISTORY = "$dotdir/.netscape/history.db";
1546 \&
1547 \& tie %hist_db, \*(AqDB_File\*(Aq, $HISTORY
1548 \& or die "Cannot open $HISTORY: $!\en" ;;
1549 \&
1550 \& # Dump the complete database
1551 \& while ( ($href, $binary_time) = each %hist_db ) {
1552 \&
1553 \& # remove the terminating NULL
1554 \& $href =~ s/\ex00$// ;
1555 \&
1556 \& # convert the binary time into a user friendly string
1557 \& $date = localtime unpack("V", $binary_time);
1558 \& print "$date $href\en" ;
1559 \& }
1560 \&
1561 \& # check for the existence of a specific key
1562 \& # remember to add the NULL
1563 \& if ( $binary_time = $hist_db{"http://mox.perl.com/\ex00"} ) {
1564 \& $date = localtime unpack("V", $binary_time) ;
1565 \& print "Last visited mox.perl.com on $date\en" ;
1566 \& }
1567 \& else {
1568 \& print "Never visited mox.perl.com\en"
1569 \& }
1570 \&
1571 \& untie %hist_db ;
1572 .Ve
1573 .SS "The \fBuntie()\fP Gotcha"
1574 .IX Subsection "The untie() Gotcha"
1575 If you make use of the Berkeley DB API, it is \fIvery\fR strongly
1576 recommended that you read "The untie Gotcha" in perltie.
1577 .PP
1578 Even if you don\*(Aqt currently make use of the API interface, it is still
1579 worth reading it.
1580 .PP
1581 Here is an example which illustrates the problem from a \fBDB_File\fR
1582 perspective:
1583 .PP
1584 .Vb 2
1585 \& use DB_File ;
1586 \& use Fcntl ;
1587 \&
1588 \& my %x ;
1589 \& my $X ;
1590 \&
1591 \& $X = tie %x, \*(AqDB_File\*(Aq, \*(Aqtst.fil\*(Aq , O_RDWR|O_TRUNC
1592 \& or die "Cannot tie first time: $!" ;
1593 \&
1594 \& $x{123} = 456 ;
1595 \&
1596 \& untie %x ;
1597 \&
1598 \& tie %x, \*(AqDB_File\*(Aq, \*(Aqtst.fil\*(Aq , O_RDWR|O_CREAT
1599 \& or die "Cannot tie second time: $!" ;
1600 \&
1601 \& untie %x ;
1602 .Ve
1603 .PP
1604 When run, the script will produce this error message:
1605 .PP
1606 .Vb 1
1607 \& Cannot tie second time: Invalid argument at bad.file line 14.
1608 .Ve
1609 .PP
1610 Although the error message above refers to the second \fBtie()\fR statement
1611 in the script, the source of the problem is really with the \fBuntie()\fR
1612 statement that precedes it.
1613 .PP
1614 Having read perltie you will probably have already guessed that the
1615 error is caused by the extra copy of the tied object stored in \f(CW$X\fR.
1616 If you haven\*(Aqt, then the problem boils down to the fact that the
1617 \&\fBDB_File\fR destructor, DESTROY, will not be called until \fIall\fR
1618 references to the tied object are destroyed. Both the tied variable,
1619 \&\f(CW%x\fR, and \f(CW$X\fR above hold a reference to the object. The call to
1620 \&\fBuntie()\fR will destroy the first, but \f(CW$X\fR still holds a valid
1621 reference, so the destructor will not get called and the database file
1622 \&\fItst.fil\fR will remain open. The fact that Berkeley DB then reports the
1623 attempt to open a database that is already open via the catch\-all
1624 "Invalid argument" doesn\*(Aqt help.
1625 .PP
1626 If you run the script with the \f(CW\*(C`\-w\*(C'\fR flag the error message becomes:
1627 .PP
1628 .Vb 2
1629 \& untie attempted while 1 inner references still exist at bad.file line 12.
1630 \& Cannot tie second time: Invalid argument at bad.file line 14.
1631 .Ve
1632 .PP
1633 which pinpoints the real problem. Finally the script can now be
1634 modified to fix the original problem by destroying the API object
1635 before the untie:
1636 .PP
1637 .Vb 2
1638 \& ...
1639 \& $x{123} = 456 ;
1640 \&
1641 \& undef $X ;
1642 \& untie %x ;
1643 \&
1644 \& $X = tie %x, \*(AqDB_File\*(Aq, \*(Aqtst.fil\*(Aq , O_RDWR|O_CREAT
1645 \& ...
1646 .Ve
1647 .SH "COMMON QUESTIONS"
1648 .IX Header "COMMON QUESTIONS"
1649 .SS "Why is there Perl source in my database?"
1650 .IX Subsection "Why is there Perl source in my database?"
1651 If you look at the contents of a database file created by DB_File,
1652 there can sometimes be part of a Perl script included in it.
1653 .PP
1654 This happens because Berkeley DB uses dynamic memory to allocate
1655 buffers which will subsequently be written to the database file. Being
1656 dynamic, the memory could have been used for anything before DB
1657 malloced it. As Berkeley DB doesn\*(Aqt clear the memory once it has been
1658 allocated, the unused portions will contain random junk. In the case
1659 where a Perl script gets written to the database, the random junk will
1660 correspond to an area of dynamic memory that happened to be used during
1661 the compilation of the script.
1662 .PP
1663 Unless you don\*(Aqt like the possibility of there being part of your Perl
1664 scripts embedded in a database file, this is nothing to worry about.
1665 .SS "How do I store complex data structures with DB_File?"
1666 .IX Subsection "How do I store complex data structures with DB_File?"
1667 Although \fBDB_File\fR cannot do this directly, there is a module which
1668 can layer transparently over \fBDB_File\fR to accomplish this feat.
1669 .PP
1670 Check out the MLDBM module, available on CPAN in the directory
1671 \&\fImodules/by\-module/MLDBM\fR.
1672 .SS "What does ""wide character in subroutine entry"" mean?"
1673 .IX Subsection "What does ""wide character in subroutine entry"" mean?"
1674 You will usually get this message if you are working with UTF\-8 data and
1675 want to read/write it from/to a Berkeley DB database file.
1676 .PP
1677 The easist way to deal with this issue is to use the pre\-defined "utf8"
1678 \&\fBDBM_Filter\fR (see DBM_Filter) that was designed to deal with this
1679 situation.
1680 .PP
1681 The example below shows what you need if \fIboth\fR the key and value are
1682 expected to be in UTF\-8.
1683 .PP
1684 .Vb 2
1685 \& use DB_File;
1686 \& use DBM_Filter;
1687 \&
1688 \& my $db = tie %h, \*(AqDB_File\*(Aq, \*(Aq/tmp/try.db\*(Aq, O_CREAT|O_RDWR, 0666, $DB_BTREE;
1689 \& $db\->Filter_Key_Push(\*(Aqutf8\*(Aq);
1690 \& $db\->Filter_Value_Push(\*(Aqutf8\*(Aq);
1691 \&
1692 \& my $key = "\eN{LATIN SMALL LETTER A WITH ACUTE}";
1693 \& my $value = "\eN{LATIN SMALL LETTER E WITH ACUTE}";
1694 \& $h{ $key } = $value;
1695 .Ve
1696 .SS "What does ""Invalid Argument"" mean?"
1697 .IX Subsection "What does ""Invalid Argument"" mean?"
1698 You will get this error message when one of the parameters in the
1699 \&\f(CW\*(C`tie\*(C'\fR call is wrong. Unfortunately there are quite a few parameters to
1700 get wrong, so it can be difficult to figure out which one it is.
1701 .PP
1702 Here are a couple of possibilities:
1703 .IP 1. 5
1704 Attempting to reopen a database without closing it.
1705 .IP 2. 5
1706 Using the O_WRONLY flag.
1707 .SS "What does ""Bareword \*(AqDB_File\*(Aq not allowed"" mean?"
1708 .IX Subsection "What does ""Bareword 'DB_File' not allowed"" mean?"
1709 You will encounter this particular error message when you have the
1710 \&\f(CW\*(C`strict \*(Aqsubs\*(Aq\*(C'\fR pragma (or the full strict pragma) in your script.
1711 Consider this script:
1712 .PP
1713 .Vb 5
1714 \& use warnings ;
1715 \& use strict ;
1716 \& use DB_File ;
1717 \& my %x ;
1718 \& tie %x, DB_File, "filename" ;
1719 .Ve
1720 .PP
1721 Running it produces the error in question:
1722 .PP
1723 .Vb 1
1724 \& Bareword "DB_File" not allowed while "strict subs" in use
1725 .Ve
1726 .PP
1727 To get around the error, place the word \f(CW\*(C`DB_File\*(C'\fR in either single or
1728 double quotes, like this:
1729 .PP
1730 .Vb 1
1731 \& tie %x, "DB_File", "filename" ;
1732 .Ve
1733 .PP
1734 Although it might seem like a real pain, it is really worth the effort
1735 of having a \f(CW\*(C`use strict\*(C'\fR in all your scripts.
1736 .SH REFERENCES
1737 .IX Header "REFERENCES"
1738 Articles that are either about \fBDB_File\fR or make use of it.
1739 .IP 1. 5
1740 \&\fIFull\-Text Searching in Perl\fR, Tim Kientzle ([email protected]),
1741 Dr. Dobb\*(Aqs Journal, Issue 295, January 1999, pp 34\-41
1742 .SH HISTORY
1743 .IX Header "HISTORY"
1744 Moved to the Changes file.
1745 .SH BUGS
1746 .IX Header "BUGS"
1747 Some older versions of Berkeley DB had problems with fixed length
1748 records using the RECNO file format. This problem has been fixed since
1749 version 1.85 of Berkeley DB.
1750 .PP
1751 I am sure there are bugs in the code. If you do find any, or can
1752 suggest any enhancements, I would welcome your comments.
1753 .SH SUPPORT
1754 .IX Header "SUPPORT"
1755 General feedback/questions/bug reports should be sent to
1756 <https://github.com/pmqs/DB_File/issues> (preferred) or
1757 <https://rt.cpan.org/Public/Dist/Display.html?Name=DB_File>.
1758 .SH AVAILABILITY
1759 .IX Header "AVAILABILITY"
1760 \&\fBDB_File\fR comes with the standard Perl source distribution. Look in
1761 the directory \fIext/DB_File\fR. Given the amount of time between releases
1762 of Perl the version that ships with Perl is quite likely to be out of
1763 date, so the most recent version can always be found on CPAN (see
1764 "CPAN" in perlmodlib for details), in the directory
1765 \&\fImodules/by\-module/DB_File\fR.
1766 .PP
1767 \&\fBDB_File\fR is designed to work with any version of Berkeley DB, but is limited to the functionality provided by
1768 version 1. If you want to make use of the new features available in Berkeley DB
1769 2.x, or greater, use the Perl module BerkeleyDB <https://metacpan.org/pod/BerkeleyDB> instead.
1770 .PP
1771 The official web site for Berkeley DB is <http://www.oracle.com/technology/products/berkeley\-db/db/index.html>.
1772 All versions of Berkeley DB are available there.
1773 .PP
1774 Alternatively, Berkeley DB version 1 is available at your nearest CPAN
1775 archive in \fIsrc/misc/db.1.85.tar.gz\fR.
1776 .SH COPYRIGHT
1777 .IX Header "COPYRIGHT"
1778 Copyright (c) 1995\-2023 Paul Marquess. All rights reserved. This program
1779 is free software; you can redistribute it and/or modify it under the
1780 same terms as Perl itself.
1781 .PP
1782 Although \fBDB_File\fR is covered by the Perl license, the library it
1783 makes use of, namely Berkeley DB, is not. Berkeley DB has its own
1784 copyright and its own license.
1785 See AGPL <https://www.oracle.com/downloads/licenses/berkeleydb-oslicense.html> for more details.
1786 Please take the time to read the Berkeley DB license and decide how it impacts your use of this Perl module.
1787 .SH "SEE ALSO"
1788 .IX Header "SEE ALSO"
1789 perl, \fBdbopen\fR\|(3), \fBhash\fR\|(3), \fBrecno\fR\|(3), \fBbtree\fR\|(3),
1790 perldbmfilter, DBM_Filter
1791 .SH AUTHOR
1792 .IX Header "AUTHOR"
1793 The DB_File interface was written by Paul Marquess
1794 <[email protected]>.