writing to db file fails even though the open doesn't 
Author Message
 writing to db file fails even though the open doesn't

I am using perl 5.005_03 and am trying to write a very simple tool
that will write values sent to it via stdin to a db file
(dbmopen()/dbmclose()). Unfortunately, I am getting an error message
and can't figure out why. Also, the code creating the error message is
confusing matters further.

The program works as follows:

1. open dbm file specified on command-line
2. for each line of input in stdin: write left-side right-side to dbm
file
3. close dbm file

Simple, right? Well, the debug output below is confusing me. First,
let me demonstrate the program output and results. First, a listing
for the destination dmb file:

saturn# ls -l virtual*
ls: No match.

Now, I run the program (source included below):

saturn# ./writedb.pl hash virtual < abc
1
mapname = virtual
2
3
4
Can't open hash: No such file or directory
Can't open virtual: No such file or directory
5
6

Finally, I look for the destination dbm file again:

saturn# ls -l virtual*
-rw-r--r--  1 root  wheel  16384 Feb 12 23:16 virtual.db

Okay, so the program is creating virtual.db, but I am still getting
the error messages. Also, I can't figure out where the error message
"Can't open hash: No such file or directory" is coming from.

Please take a look at the source below and let me know if you see the
problem. I'm assuming it's obvious.

[--cut--]
#!/usr/bin/perl -w

use strict;

my %DB;
my $dbtype;
my $mapname;

if ($#ARGV != 1) {
        print "Usage: writedb db-type map-filename\n";
        exit 1;

Quote:
}

else {
        print "1\n";
        $dbtype = $ARGV[0];     # we are actually going to ingore the
dbtype!
        $mapname = $ARGV[1];
        print "mapname = $mapname\n";
        print "2\n";

Quote:
}

print "3\n";
dbmopen(%DB, $mapname, 0666) || die "Can't open map $mapname!\n";
print "4\n";

for (<>) {
        chomp;
        if (/\s*(.+)\s*(.+)\s*/) {
                $DB{$1} = $2;
        }

Quote:
}

print "5\n";
dbmclose(%DB);
print "6\n";
exit 0;
[--cut--]

Regards, Dustin

---

Information Systems Contractor
http://www.*-*-*.com/ ~dpuryear
PGP Key available at http://www.*-*-*.com/
In the beginning the Universe was created.
This has been widely regarded as a bad move. - Douglas Adams



Tue, 03 Aug 2004 04:19:57 GMT  
 writing to db file fails even though the open doesn't
: I am using perl 5.005_03 and am trying to write a very simple tool
: that will write values sent to it via stdin to a db file
: (dbmopen()/dbmclose()). Unfortunately, I am getting an error message
: and can't figure out why. Also, the code creating the error message is
: confusing matters further.

: dbmopen(%DB, $mapname, 0666) || die "Can't open map $mapname!\n";

dbmopen does not return an error.  You have to do something else to check
for errors.  I typically use an ordinary open to check I have the
necessary access to the file before using dbmopen on it.

If you are opening an existing db file (not your situation here, but I'll
mention it anyway) then dbmopen does not check it is a database.  To check
you have opened a db file and not something else make sure the db file has
at least one key in it, and then make sure that each() returns at least
one value.  (I believe this is true for various .db files, its nothing to
do with perl, and I don't know if my test might hang the program, but it
will prevent you from writing into the file so that's ok anyway.)

dbmopen does various things in the background, and I wonder if those
things may cause additional error messages.

Also, you will get odd problems if you define your own $SIG{'__DIE__'}
handler, because that will also interfere in odd ways with the dbmopen
routines.

(I think actually that dbmopen may be depricated, you may be supposed to
use the tie functions and a DBMxxx module.)



Tue, 03 Aug 2004 05:43:20 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Child processes don't clean up (defunct processes left) even though SIGCHLD does wait()

2. $* warning even though I didn't use it

3. failed open in W2K doesn't pass flow of control as expected

4. open() fails, even when it doesn't fail?

5. Apache, embperl, mod_perl : require doesn't work and can't write to file

6. Net::Telnet module Script exiting even though Errmode set to Return

7. system() returns -1 even though it succeeded

8. Endless loop doesn't stop even when the browser closes

9. While doesn't file open in DosPerl?

10. Image::Magick->Write() doesn't want to write

11. DBD::ODBC I can write to db, but read fails

12. open'ing lynx doesn't work when called by cron

 

 
Powered by phpBB® Forum Software