| James Marshall 2005-02-06, 8:55 pm |
| Not sure who maintains AnyDBM_File, or I'd email them directly....
Something I think would be nice in AnyDBM_File would be the ability to
open an existing DBM, regardless of its format (NDBM, DB, GDBM, etc.).
Right now, "any" means it can create a new DBM using any installed DBM
module. But I think it would be useful if "any" also meant that it could
handle any existing DBM file too, automatically opening it with the
correct DBM module:
$dbh= tie %db, 'AnyDBM_File', $existing_dbname, $flags, $perms ;
The code should be simple, unless I'm missing something. First, try to
open the DBM with each module; if it doesn't work, then do the same but
add the O_CREAT flag, like:
foreach (qw(NDBM_File DB_File GDBM_File SDBM_File ODBM_File)) {
last if eval("require $_")
&& ($dbh= tie %db, $_, $dbname, O_RDWR, $perms) ;
}
unless ($dbh) {
foreach (qw(NDBM_File DB_File GDBM_File SDBM_File ODBM_File)) {
last if eval("require $_")
&& ($dbh= tie %db, $_, $dbname, O_RDWR|O_CREAT, $perms) ;
}
}
die unless $dbh ;
This has a problem with older Perls-- in 5.6.0, NDBM_File will create a
DBM even without the O_CREAT flag; in 5.6.1 GDBM_File does the same. I
haven't tested it on 5.8.x, but hopefully it's fixed.
Other than that, the above code seems to work well in a program of mine
(new release of DBMEdit).
Thoughts? Suggestions?
James
.............................................................................
James Marshall james@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
.............................................................................
|