sub create_lock_file my file old_file open FH file or die open file fl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sub create_lock_file {
my ($file, $old_file) = @_;
open (FH, ">>", $file)
or die("open($file): $!");
flock(FH, LOCK_EX)
or die("flock($file): $!");
seek(FH, 0, SEEK_END)
or die("seek($file): $!");
my $is_new_file = tell(FH) > 0;
open (FH2, "<", $old_file)
or die("open($old_file): $!");
flock(FH2, LOCK_EX)
or die("flock($old_file): $!");
my $old_user_ids;
{ local $/ = undef; $old_user_ids = <FH2>; }
syswrite(FH, $old_user_ids);
flock(FH, LOCK_UN);
flock(FH2, LOCK_UN);
close(FH);
close(FH2);
return $is_new_file;
}