Programming Perl

Programming PerlSearch this book
Previous: 7.2 Library ModulesChapter 7
The Standard Perl Library
Next: 7.2.2 AutoLoader - Load Functions Only on Demand
 

7.2.1 AnyDBM_File - Provide Framework for Multiple DBMs

use AnyDBM_File;

This module is a "pure virtual base class" - it has nothing of its own. It's just there to inherit from the various DBM packages. By default it inherits from NDBM_File for compatibility with earlier versions of Perl. If it doesn't find NDBM_File, it looks for DB_File, GDBM_File, SDBM_File (which is always there - it comes with Perl), and finally ODBM_File.

Perl's dbmopen function (which now exists only for backward compatibility) actually just calls tie to bind a hash to AnyDBM_File. The effect is to bind the hash to one of the specific DBM classes that AnyDBM_File inherits from.

You can override the defaults and determine which class dbmopen will tie to. Do this by redefining @ISA:

@AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File);

Note, however, that an explicit use takes priority over the ordering of @ISA, so that:

use GDBM_File;

will cause the next dbmopen to tie your hash to GDBM_File.

You can tie hash variables directly to the desired class yourself, without using dbmopen or AnyDBM_File. For example, by using multiple DBM implementations, you can copy a database from one format to another:

use Fcntl;         # for O_* values
use NDBM_File;
use DB_File;
tie %oldhash, "NDBM_File", $old_filename, O_RDWR;
tie %newhash, "DB_File",   $new_filename, O_RDWR|O_CREAT|O_EXCL, 0644;
while (($key,$val) = each %oldhash) {
    $newhash{$key} = $val;
}

7.2.1.1 DBM comparisons

Here's a table of the features that the different DBMish packages offer:

FeatureODBMNDBMSDBMGDBMBSD-DB
Linkage comes with PerlYesYesYesYesYes
Source bundled with PerlNoNoYesNoNo
Source redistributableNoNoYesGPLYes
Often comes with UNIXYesYes[1]NoNoNo
Builds OK on UNIXN/AN/AYesYesYes[2]
Code sizeVaries[3]Varies[3]SmallBigBig
Disk usageVaries[3]Varies[3]SmallBigOK[4]
SpeedVaries[3]Varies[3]SlowOKFast
FTPableNoNoYesYesYes
Easy to buildN/AN/AYesYesOK[5]
Block size limits1k4k1k[6]NoneNone
Byte-order independentNoNoNoNoYes
User-defined sort orderNoNoNoNoYes
Wildcard lookupsNoNoNoNoYes

[1] On mixed-universe machines, may be in the BSD compatibility library, which is often shunned.

[2] Providing you have an ANSI C compiler.

[3] Depends on how much your vendor has "tweaked" it.

[4] Can be trimmed if you compile for one access method.

[5] See the DB_File library module. Requires symbolic links.

[6] By default, but can be redefined (at the expense of compatibility with older files).

7.2.1.2 See also

Relevant library modules include: DB_File, GDBM_File, NDBM_File, ODBM_File, and SDBM_File. Related manpages: dbm(3), ndbm(3). Tied variables are discussed extensively in Chapter 5, and the dbmopen entry in Chapter 3, Functions, may also be helpful. You can pick up the unbundled modules from the src/misc/ directory on your nearest CPAN site. Here are the most popular ones, but note that their version numbers may have changed by the time you read this:

http://www.perl.com/CPAN/src/misc/db.1.85.tar.gz
http://www.perl.com/CPAN/src/misc/gdbm-1.7.3.tar.gz


Previous: 7.2 Library ModulesProgramming PerlNext: 7.2.2 AutoLoader - Load Functions Only on Demand
7.2 Library ModulesBook Index7.2.2 AutoLoader - Load Functions Only on Demand