?
Solved

Tieing a hash to a file:   Which database format?

Posted on 2003-03-06
3
Medium Priority
?
204 Views
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);
}

0
Comment
Question by:jgore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 300 total points
ID: 8085976
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
 

Author Comment

by:jgore
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;

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

Author Comment

by:jgore
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";

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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

777 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