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;
}Here's a table of the features that the different DBMish packages offer:
| Feature | ODBM | NDBM | SDBM | GDBM | BSD-DB |
|---|---|---|---|---|---|
| Linkage comes with Perl | Yes | Yes | Yes | Yes | Yes |
| Source bundled with Perl | No | No | Yes | No | No |
| Source redistributable | No | No | Yes | GPL | Yes |
| Often comes with UNIX | Yes | Yes[1] | No | No | No |
| Builds OK on UNIX | N/A | N/A | Yes | Yes | Yes[2] |
| Code size | Varies[3] | Varies[3] | Small | Big | Big |
| Disk usage | Varies[3] | Varies[3] | Small | Big | OK[4] |
| Speed | Varies[3] | Varies[3] | Slow | OK | Fast |
| FTPable | No | No | Yes | Yes | Yes |
| Easy to build | N/A | N/A | Yes | Yes | OK[5] |
| Block size limits | 1k | 4k | 1k[6] | None | None |
| Byte-order independent | No | No | No | No | Yes |
| User-defined sort order | No | No | No | No | Yes |
| Wildcard lookups | No | No | No | No | Yes |
[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).
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