I can't get Perl flock to work. I lock the file by checking the return result to make sure it is actually locked, and I can still open and write to it as if nothing was happening.
This is how I lock the file:
#! / usr / bin / perl -w
use strict;
use Fcntl ': flock';
$ | = 1;
my $ f = $ ARGV [0];
open (my $ fh, '>>', $ f) or die "Could not open '$ f' - $!";
print "locking '$ f' ...";
flock ($ fh, LOCK_EX) or die "Could not lock '$ f' - $!";
print "locked \ n";
sleep 10;
print "waking up and unlocking \ n";
close ($ fh);
While this script was sleeping, I can play with the same text file:
#! / usr / bin / perl -w
use strict;
my $ f = $ ARGV [0];
open (my $ fh, '>>', $ f) or die "Could not open '$ f' - $!";
print $ fh "This line was appended to a locked file! \ n";
close ($ fh);
Why am I trying to open a file and write to it without saying that it is locked?
source share