in reply to what is anydbm_file?
(Update: in the years since I first posted this node, the docs have improved a lot, and easy examples are easy to find.)
In any case, if you intend to be sharing data between the DB_file system and the NDBM/ODBM/SDBM system, you may be up a creek unless at least one of these flavors is common to both systems. As explained in the AnyDBM_File man page (in a rare display of detail), each flavor has its own file storage format and different capacity constraints, so portability of data between two DBM flavors requires that both flavors be available to a given perl installation (on one machine). On top of that, most flavors create data files that are not portable across the big-/little-endian divide -- so even if you had the same DBM module on two machines, the data would not be directly portable if the systems differ in cpu byte-order.
But if you are writing a script such that each installation on a given machine will just be writing/reading data solely on that machine, you can start with something like the following test script, and you shouldn't have to change this to suit particular machines:
Note that the different dbm flavors have different file naming conventions -- some create one file, some create two, some add a standard file extension to the given file name, some do not.#!/usr/bin/perl use strict; use Fcntl; use AnyDBM_File; my $preferred_dbm = $AnyDBM_File::ISA[0]; my %hash; my $dbfile = "this_is_a_test"; print "opening test_dbm using $preferred_dbm ...\n"; tie( %hash, $preferred_dbm, $dbfile, O_CREAT|O_RDWR, 0664 ) or die $!; my $i = 0; my $limit = 20000; while ( $i < $limit ) { my $x = rand; $hash{"a$i"} = $x; $i++; print "$i records stored\n" if ( $i % 2000 == 0 ); } untie %hash; print "Saved $limit records in $dbfile using $preferred_dbm\n"; %hash = (); # just to prove things work as intended tie %hash, $preferred_dbm, $dbfile, O_RDONLY, 0444; for $i ( 1 .. 20 ) { my $k = 'a' . int( rand( $limit )); print "$k = $hash{$k}\n"; } untie %hash;
One last issue: the AnyDBM_File man page appears to say that at least one flavor (SDBM_File) is part of the core distribution -- another quick test you should try, just to see what's available (i.e. possible) and what's preferred is simply:
(update: On trying that last little test myself, I may be misunderstanding how @ISA really works in AnyDBM_File. I tried it on a system where I know there's more than one flavor available -- because I've used at least two different ones there in the past -- but only NDBM_File shows up. As a side note, I noticed that using NDBM on macosx creates one file "name.db", while doing the same thing on solaris creates two files, "name.pag" and "name.dir" -- go figure... At least the test script given above worked on both systems without modification; maybe that's all that matters.)perl -MAnyDBM_File -e 'print join $/, @AnyDBM_File::ISA, $/'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: what is anydbm_file?
by gmcharlt (Novice) on Feb 11, 2013 at 18:57 UTC |