[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: DBM file endings



(A copy of this message has also been posted to the following newsgroups:
comp.lang.perl.misc)

In article <vlb-ya023580002606971659070001@news2.apple.com>, vlb@apple.com
(Vicki Brown) wrote:

# I was given a set of Perl scripts that use DBM routines; the scripts assume
# that .pag and .dir files will be created. 

They shouldn't.  They should assume a name like "my_db" and the dbm package
will create and translate the necessary filenames.  On some packages it
might be .pag and .dir as two files to one db; other packages might create
on file and give it a .db extension.

Assuming the extension is not a good idea.

# On my system (MkLinux, essentially RedHat Linux) I get a single .db file
# instead.

You MacHead, you ...  :-)

# Am I assuming correctly that it's my _system_ dbm library that's handling
# this, and not Perl, per se?

Yes.

# Is there a simple way of determining which dbm library is in use or is this
# one of those times where the code has to check for a .db file and if it's
# not there look for .dir & .pag (i.e. where the code writer needs to know
# how the different libraries will act).

AnyDBM_File should open it no matter what created it.

#!perl
use AnyDBM_File;
use Fcntl;

#write with NDBM_File
tie %hash1,'NDBM_File','my_db', O_CREAT|O_RDWR, 0644;
$hash1{key} = value;
untie %hash1;

#open with AnyDBM_File
tie %hash2,'AnyDBM_File','my_db', O_CREAT|O_RDWR, 0644;
foreach (keys %hash2) {print "$_: $hash2{$_}\n"}
untie %hash2;

This worked on both MacPerl 5.1.3r2 and perl5.003 (Solaris 2.4).  The file
created by the Mac version was one file, named 'my_db'.  In the Solaris
version,  two files were created, 'my_db.dir' and 'my_db.pag'.  The Mac
version was also tested using DB_File instead of NDBM_File, and it worked
fine, too.  I don't have DB_File on the Solaris box.

Hope that all helps,

--
Chris Nandor                 pudge@pobox.com                 http://pudge.net/
%PGPKey=('B76E72AD',[1024,'08 24 09 0B CE 73 CA 10  1F F7 7F 13 81 80 B6 B6'])