Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Tieing a hash to a file:   Which database format?

Posted on 2003-03-06
Medium Priority
Last Modified: 2010-03-05

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

Question by:jgore
  • 2
LVL 27

Accepted Solution

wilcoxon earned 300 total points
ID: 8085976
Use AnyDBM_File.  Here's an example:

    # 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;

Author Comment

ID: 8086245
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;

eval "use $list[0]";
tie (%URLHash, $list[0], "dbFileName" , O_RDWR|O_CREAT, 0640);
return 1;

Author Comment

ID: 8087188
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";

tie %URLHash, $list[0], $Raven{'DataFile'}, O_RDWR|O_CREAT, 0640;

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question