in reply to Any_DBM_File blues.

You have several problems here. Some are just usage issues, and some are more serious: Which brings me to...

Why are you using AnyDBM_File? I think you should just use DB_File itself if you want to use DB_File--in which case you should read the DB_File docs, and in which case you can just do something like this:

use DB_File; my %database; tie %database, 'DB_File', "bar" or die "Can't tie: $!";
because
tie %database, 'DB_File', "bar"
is equivalent to
tie %database, 'DB_File', "bar", O_CREAT|O_RDWR, 0666, $DB_HASH;
Does this make sense?

Replies are listed 'Best First'.
RE: Re: Any_DBM_File blues.
by skazat (Chaplain) on May 02, 2000 at 02:23 UTC
    >>"0666" is a different thing than 0666.

    ok, my bad... but changed that, no difference ;(

    >> What is "DB_HASH"

    i haven't a clue, i got this code out of the new Perl DBI book. the code i gave seems to write the file alright. (it writes two files, one a .dir one a .pag, apparently i don't have DB_File.

    >> Why are you using AnyDBM_File?

    heres the funky chicken, i don't have DB_File, (or can't see it...) i also don't have: NDBM_File, GDBM_File SDBM_File , or ODBM_File. but i do have AnyDBM_File and somehow it works. oi

    i'm on concentric btw, they got this whacky VDE thingy that i loathe..

      Well, I don't have the DBI book, but that code sample that you say is from the book doesn't work for me.

      So it looks like, since you don't have those other DBM packages, you must be using SDBM_File, since it's guaranteed to be there (it comes w/ Perl, according to the AnyDBM_File manpage).

      So with that in mind, I was able to get this to work correctly, using SDBM_File. Add this line near the top of your file:

      use Fcntl;
      to import the file creation/read-write flags; and modify your tie line to look like this:
      tie %database, 'AnyDBM_File', "bar", O_RDWR|O_CREAT, 0666 or die "doh + $!\n";
      Which, if you look at the SDBM_File docs, matches up with what the tie line should look like. This works for me.

      BTW, $DB_HASH, in the context of DB_File, is a special constant telling DB_File to use its DB_HASH package. It's the default for DB_File.

        wow thanks alot, that worked perfectly. i never used the AnyDBM_File module before, i always used the DB_File module, all the examples in the perl cookbook use the DB_File module and its almost guarenteed to come with a BSD system (like i'm on)

        once again, thanks!

        cheers,
        -justin

      I've got the book (Programming the Perl DBI, Descartes & Bunce). Notice it's about DBI; I'd suggest using it if you can at all (I converted a project from using AnyDBM to MySQL and it saved me about 50% of the code and 90% of the headache).

      That said, if you're still stuck with DBMs, use Berkeley DB if at all possible; the rest just plain suck for anything more than pure hash usage. Grab DB_File from CPAN and see if you can just drop in the .pm; it sometimes works without a make. At least one of the others comes with perl, and they're pretty similar feature-wise. AnyDBM just uses one of these(from perldoc AnyDBM_File, the search order is ndbm, db_file, gdbm, sdbm, odbm), so you must have one of those five.

      Like btrott wrote, the $DB_HASH, $DB_BTREE, and $DB_RECNO formats are only supported by DB_File. I don't think you were trying (I did, I'll save you the pain of figuring it out :), but the neat trick the book has of using $DB_BTREE with R_DUP to allow duplicate keys doesn't work with anything other than DB_File.