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.)

In reply to Re: what is anydbm_file? by graff
in thread what is anydbm_file? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.