View Full Version : flocking trouble!
Hi,
I use this flock method elsewhere (in another script) and it doesn't throw an error. But it does here :(
open(DAT,">$dat_file") || print "$dat_file cannot be opened!\n";
flock (DAT, LOCK_EX) || die "can’t lock filename: $!";
print DAT "RoomNumber:$newRoomNumber\n";
Firstly, what am I missing and,
Secondly, how can I check that it deosn actually lock the file?
Bazz
FishMonger
04-07-2006, 03:19 PM
Which error messege are you getting..."$dat_file cannot be opened!\n"... or ..."can’t lock filename: $!"... or some other error?
nkrgupta
04-07-2006, 03:25 PM
I'm not sure if its necessary, but are you importing the Fcntl module for using LOCK_EX instead of its value 2? Just try it out in case you aren't.
use Fcntl;
Blimey, am I getting stupid. :(
Thanks to you both.
I was getting the error "can’t lock filename: $!".. so I knew it was to do with the flock.
I had forgotten that I was using a module. I just expected it to be part of the perl installation so I had ommitted the 'use Fcntl qw(:DEFAULT :flock);' line which I had used in the other file and, which works.
two further small questions.
Now that I won't get the error, is there any way to confirm that it does infact lock the file.
And this is a bit more difficult to explain...
I have a single script.
It comprises two subs - &form and &writefile.
On first run, it checks the flat file to ensure that a specific value is null/blank/not there. If so, the form displays.
when I submit the form it re-runs the script and writes the form data to the flat file.
my question is; How can I check for the null value on first run of the script and lock that file throughout so that only the perosn completing the form, can write to the flat file?
I want to avoid finding a null value and by the time I submit the form, someone else has written to that file.
Bazz
KevinADC
04-07-2006, 08:22 PM
I don't think you can keep the file locked between invocations of the script but you could recheck for the null value again before writing to the file. If the null value is no longer present throw an error message (or whatever is appropriate).
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.