Tieing a hash to a file: Which database format?


I want to use persistent hash's, like with:

use DB_File;
tie(%h, 'DB_File', $Raven{'DataFile'}, O_RDWR|O_CREAT, 0666)
or die "Couldn't tie DB_file 'filename': $!; aborting";  

That uses the Berkley database thingy to tie the %h to a flat file database on disk.
It works. My problem is that I don't which, if any, database modules the client will have installed.
How do I check if each database module exists and then use that?
I thought about something like the below. Isn't there anything better? I'm not even sure
what's below would work. Just a thought.

if (eval "require DB_File") {
    use DB_File ;
    tie %h, "DB_File", "DBdb", O_RDWR|O_CREAT, 0640, $DB_HASH;
} elsif (eval "require GDBM_File") {
    use GDBM_File ;
    tie %h, 'GDBM_File', "GDBMdb", &GDBM_WRCREAT, 0640;
} elsif (eval "require NDBM_File") {
    use NDBM_File ;
    tie(%h, 'NDBM_File', 'NDBMdb', O_RDWR|O_CREAT, 0640);
} elsif (eval "require ODBM_File") {
    use ODBM_File ;
    tie(%h, 'ODBM_File', 'ODBMdb', O_RDWR|O_CREAT, 0640);
} elsif (eval "require SDBM_File") {
    use SDBM_File ;
    tie(%h, 'SDBM_File', 'SDBMdb', O_RDWR|O_CREAT, 0640);
}

jgoreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wilcoxonCommented:
Use AnyDBM_File.  Here's an example:

BEGIN {
    # first set the ISA to your prefered order
    @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File ODBM_File)
}
# now use whichever one of those is found first in your list and is present on the system
use AnyDBM_File;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgoreAuthor Commented:
I didn't want to add a dependency.
So, I wrote the following instead. It does the exact same thing, kinda. It seems to work. But if you see any reason
why it's not good please let me know!

use Fcntl;                                      
if (LoadDatabase==0) { print "Failed"; }
  else { print "Tied Database!"; }

# ======  LoadDatabase =================
sub LoadDatabase                                  
{
my @list = qw(DB_File NDBM_File GDBM_File ODBM_File SDBM_File);
my $list;

  foreach my $element (@list)
   {
   if (eval "require $element") { @list = ($element); goto Found;}
   }
return 0;

Found:
eval "use $list[0]";
tie (%URLHash, $list[0], "dbFileName" , O_RDWR|O_CREAT, 0640);
return 1;
}
0
jgoreAuthor Commented:
Well, this is what I ended up with:



# =======  LoadDatabase ========
sub LoadDatabase
{
my @list = qw(DB_File NDBM_File GDBM_File ODBM_File SDBM_File);
my $list;

  foreach my $element (@list)
   {
   if (eval "require $element") { @list = ($element); goto Found;}
   }

print "Content-type: text/html\n\n<HTML><BODY>
<B>Sorry, you have no database modules installed! See instructions.</B></Body></HTML>";
die "No database modules found: DB_File NDBM_File GDBM_File ODBM_File SDBM_File";

Found:
tie %URLHash, $list[0], $Raven{'DataFile'}, O_RDWR|O_CREAT, 0640;
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.