Piddly runtime error using DB_File and Fcntl 
Author Message
 Piddly runtime error using DB_File and Fcntl

Can anyone give me some pointers? I'm relatively new to Perl and this
one is driving me nuts.

I'm creating a module to aid in the sharing (locking) of database files
on a unix system (perl, version 5.004_04 built for i386-linux).
I'm basing the code in my modules on an example given
in Programming Perl, Ch. 7, DB_File, Locking Databases).

Here are the relevant sections of my module:

----------------------- BEGIN MODULE EXAMPLE ----------------------

#!/usr/bin/perl -w
# The dopen method returns a reference to an array containing
# the database object pointer in element 0 and a filehandle in element 1;

package DBShare;

use Fcntl;
use DB_File;
use FileHandle;

use strict;

my $Debug = 1;
sub LOCK_SH { 1 }
sub LOCK_EX { 2 }
sub LOCK_NB { 4 }
sub LOCK_UN { 8 }

# -------------------------------METHODS-----------------------

sub dopen {

   if ($Debug) { print "Enter dopen...  "; };

   my($type, $filename, $fd, %db_hash, $db);

   $db = [];
   $db->[0] = tie(%db_hash, 'DB_File', $filename, O_CREAT|O_RDWR, 0666)
                    or die "dbcreat $filename: $!";
   $db->[1] = new FileHandle;
   $fd = ($db->[0])->fd;
   open(($db->[1]), "+<&=$fd") or die "Unable to open dbfile: $!";
   flock(($db->[1]), LOCK_UN);

   if ($Debug) { print "Exit dopen!\n"; };

   return bless $db, $type;

} # END OF dopen

# -----------------------------

sub dkeys {

   if ($Debug) { print "Enter dkeys...  "; };

   $i = 0;

   unless (flock (($db->[1]), LOCK_SH | LOCK_NB)) {
       unless (flock (($db->[1]), LOCK_SH)) { die "Unable to sharelock dbfile: $!" }

   $i = 0;
   until ( $db->[0]->seq($key, $value, R_NEXT) != 0 ) {
      if ($Debug) { print ("Key $i : $key    ") }
      $keys[$i++] = $key;

   flock (($db->[1]), LOCK_UN);

   if ($Debug) { print "Exit dkeys!\n"; };

} # END OF dkeys

# various other methods are not included as they didn't seem relevant to
# me. I will post them if anyone feels they hold a key to the problem.

------------------------ END MODULE EXAMPLE ---------------

The following is the program I'm using to test the module:

------------------- BEGIN TEST PROGRAM -------------------

#!/usr/bin/perl -w

use DBShare;

$db = DBShare->dopen('test.db');

print ( $db->dkeys, "\n" );

$db->dwrite('name', 'David E. Miller');
$db->dwrite('rank', 'GEN');
$db->dwrite('serial', '123-45-6789');

print ($db->dread('name'), $db->dread('rank'), $db->dread('serial'),

print ( $db->dkeys, "\n" );

print ( $db->disakey('fred'), $db->disakey('name'), "\n");


print ( $db->dkeys, "\n" );

-------------------- END TEST PROGRAM ---------------------

Now the problem... When I run the test program, the results are fine
except that on each call to the method dkeys I get a non-fatal error as
shown below:

------------------BEGIN OUTPUT ---------------------

Enter dopen...  Exit dopen!
Use of uninitialized value at line 139.
Enter dkeys...  Exit dkeys!

Enter dwrite...  Exit dwrite!
Enter dwrite...  Exit dwrite!
Enter dwrite...  Exit dwrite!
Enter dread...  Exit dread!
Enter dread...  Exit dread!
Enter dread...  Exit dread!
David E. MillerGEN123-45-6789
Use of uninitialized value at line 139.
Enter dkeys...  Key 0 : rank    Key 1 : serial    Key 2 : name    Exit dkeys!
Enter disakey...  Exit disakey!
Enter disakey...  Exit disakey!
Enter ddelete...  Exit ddelete!
Use of uninitialized value at line 139.
Enter dkeys...  Key 0 : serial    Key 1 : name    Exit dkeys!

------------------END OUTPUT------------------------

The error refers to line 139, which is the "flock (($db->[1]),
LOCK_UN);" line in the dkeys method. Here are the things that are
bugging me about this problem:

   1) the error is printed prior to any of the print functions that are
imbedded in the method. How can any variable at line 139 be
uninitialized if (as far as I can tell) that line of code hasn't been
executed yet?
   2) all of the other methods in this module have the same flock
function almost immediately prior to the end of the method, yet the
dkeys method is the only one that produces this error.
   3) I can comment out that line and the error "moves" to the next line
of code.

I would appreciate any assistance that could be offered. I'm beating my
head against the monitor at this point.


Dave Miller

Wed, 18 Jun 1902 08:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Error using DB_File after moving from unix to NT

2. Error with my module using DB_File

3. HELP WANTED: runtime error message: VARIABLE ERROR.

4. Win32::AdminMisc and "error: Runtime Error"

5. Using the hash DB_File uses, after untie it??

6. Weird error lessage from fcntl: please help

7. Checking for open files using fcntl: flock in Solaris

8. Help w/newbie Perl runtime Error

9. EventLog Gets Runtime Exception Error

10. Has anyone used Fcntl to handle file locking?

11. Req for example: file lock using fcntl

12. ExtUtils::MakeMaker runtime error


Powered by phpBB® Forum Software