[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] flock with MLDBM



Briefly, how does one go about implementing file-locks when using MLDBM?

(This is on a LinuxPPC server.)

I thought it would be possible to modify the script in _Programming 
Perl_ (393) and in _Perl Cookbook_ (497-8):
--
#! /usr/bin/perl

use DB_File;
use strict;

sub LOCK_SH {1}
sub LOCK_EX {2}
sub LOCK_NB {4}
sub LOCK_UN {8}

my ($oldval, $fd, $db, %db, $value, $key);

$key = shift || 'default';
$value = shift || 'magic';
$value .= " $$";

$db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0666)
	or die "dbcreat /tmp/foo.db $!";
$fd = $db->fd;
print "$$: db fd is $fd\n";
open(DB_FH, "+<&=$fd")            # [I HAVE NO IDEA WHAT'S GOING ON HERE!]

unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {
	print "$$: CONTENTION; can't read during write update!
		Waiting for read lock ($!) ....";
	unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" }
}
print "$$: Read lock granted\n";

$oldval = $db{key};
print "$$: Old value was $oldval\n";
flock(DB_FH, LOCK_UN);

unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
	print "$$: CONTENTION; must have exclusive lock!
		Waiting for write lock ($!) ....";
	unless (flock (DB_FH, LOCK_EX)) {die "flock: $!" }
}

print "$$: Write lock granted\n";
$db{$key}=$value;
$db->sync;
sleep 10;

flock(DB_FH, LOCK_UN);
undef $db;
untie %db;
close(DB_FH);
print "$$: Updated db to $key=$value\n";
--

It all seems to work as advertised using DB_File, but changing the 
"use DB_File" line to "useMLDBM 'DB_File'" results in 'O_CREAT', 
'O_RDWR', and all the 'LOCK_'s being seen as barewords.

Please will somebody put me out of my misery!
-- 
My brain hurts!
SeanC
                      Mediatek Training Institute
            129 Moore Road, Berea, Durban, South Africa
    phone: +27 (0)31 305 4200              SeanC@mediatek.co.za
      fax: +27 (0)31 305 4236
                   <http://members.xoom.com/s_carte>

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org