#!/usr/bin/perl -Tw use strict; use Fcntl (':flock'); .... sub DeleteRecords { no strict 'subs'; # strict does not like O_EXCL and O_CREAT?? my($file, @delete_these) = @_; # avoid race conditions, where subscriber.txt is changed between # reading contents with &GetRecords and writing new subscriber # list with &SetRecords. We create a lockfile only if it does # not already exist ie no other process performing &DeleteRecords my $count = 0; until (sysopen (LOCK, "$path_to_files/lockfile.txt", O_EXCL | O_CREAT | O_WRONLY)) { sleep 1; DieNice("Can't create lockfile '$path_to_files/lockfile.txt': $!\n") if ++$count >= $timeout; } my @records = &GetRecords($file); for my $record (@delete_these) { @records = grep{ $_ ne $record }@records; } &SetRecords($file, @records); close LOCK; unlink "$path_to_files/lockfile.txt" or DieNice("Can't unlink lockfile: $!\n"); return; }