Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: what is anydbm_file?

by graff (Chancellor)
on Apr 01, 2004 at 05:32 UTC ( #341517=note: print w/ replies, xml ) Need Help??


in reply to what is anydbm_file?

I share your disappointment with the terseness of the various dbm man pages in perl -- there are some serious gaps there, and it is often not that helpful to read things like "to use this module, you really should have the complete (whatever_dbm C library) manual to refer to". (Guess what I've never been able to find on the university network where I do most of my work...) Hell, just a few more examples for how to open each of the dbm flavors would be nice.

(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:

#!/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;
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.

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:

perl -MAnyDBM_File -e 'print join $/, @AnyDBM_File::ISA, $/'
(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.)


Comment on Re: what is anydbm_file?
Select or Download Code
Re^2: what is anydbm_file?
by gmcharlt (Novice) on Feb 11, 2013 at 18:57 UTC
    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.
    I took a look at this, and found that when AnyDBM_File is loaded, it goes through its @ISA and and replaces it with a list containing a single entry: the first module in the original @ISA list that can be successfully loaded.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://341517]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2014-10-01 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (31 votes), past polls