B
Bigus
Hi
I am attempting to flock for the very first time - after some surfing
around, the following 2 methods sound like the most viable/robust I can
find:
==== METHOD 1 ====
use strict;
use warnings;
my $file = "flocktest.txt";
open FH, "+<$file" or die "Cannot open $file: $!";
flock FH, 2; # lock file
seek FH, 0, 0; # back to beginning of file
my @lines = <FH>; # get current content
### do stuff to content ###
seek FH, 0, 0; # back to beginning of file
truncate FH, 0; # empty file
print FH @lines; # print new content to file
close FH;
==== METHOD 2 ====
use strict;
use warnings;
my $file = 'flocktest.txt';
my $lockfile = $file.".lock"; # create dummy file to lock
open(LOCK, ">$lockfile") or die "Can't open $lockfile($!)";
flock(LOCK, 2); # lock file
open(FH, $file) or die "Can't open $file ($!)";
my @lines = <FH>; # get current content
close FH;
### do stuff to content ###
open(FH, ">$file") or die "Can't open $file ($!)";
print FH @lines; # print new content to file
close FH;
close LOCK;
==== END SAMPLE CODE =====
Are there any issues with these methods I should be aware of? Which one of
these, or other, methods would you be inclined to choose / think is better?
So far, I'm leaning towards the 2nd one since method 1 uses the seek &
truncate functions which I have not used before. However, only having to
open a file once in read/write mode does sound less "messy" somehow.
One other general thing - if another instance of the above scripts tried to
access the file while it was locked would it wait until it's free or would I
have to include a retry loop in the script?
Thanks
Bigus
I am attempting to flock for the very first time - after some surfing
around, the following 2 methods sound like the most viable/robust I can
find:
==== METHOD 1 ====
use strict;
use warnings;
my $file = "flocktest.txt";
open FH, "+<$file" or die "Cannot open $file: $!";
flock FH, 2; # lock file
seek FH, 0, 0; # back to beginning of file
my @lines = <FH>; # get current content
### do stuff to content ###
seek FH, 0, 0; # back to beginning of file
truncate FH, 0; # empty file
print FH @lines; # print new content to file
close FH;
==== METHOD 2 ====
use strict;
use warnings;
my $file = 'flocktest.txt';
my $lockfile = $file.".lock"; # create dummy file to lock
open(LOCK, ">$lockfile") or die "Can't open $lockfile($!)";
flock(LOCK, 2); # lock file
open(FH, $file) or die "Can't open $file ($!)";
my @lines = <FH>; # get current content
close FH;
### do stuff to content ###
open(FH, ">$file") or die "Can't open $file ($!)";
print FH @lines; # print new content to file
close FH;
close LOCK;
==== END SAMPLE CODE =====
Are there any issues with these methods I should be aware of? Which one of
these, or other, methods would you be inclined to choose / think is better?
So far, I'm leaning towards the 2nd one since method 1 uses the seek &
truncate functions which I have not used before. However, only having to
open a file once in read/write mode does sound less "messy" somehow.
One other general thing - if another instance of the above scripts tried to
access the file while it was locked would it wait until it's free or would I
have to include a retry loop in the script?
Thanks
Bigus