LockFile::Simple is a simple file locking scheme.
use LockFile::Simple qw(lock trylock unlock);
# Simple locking using default settings
lock("/some/file") || die "can't lock /some/filen";
warn "already lockedn" unless trylock("/some/file");
# Build customized locking manager object
$lockmgr = LockFile::Simple->make(-format => '%f.lck',
-max => 20, -delay => 1, -nfs => 1);
$lockmgr->lock("/some/file") || die "can't lock /some/filen";
$lockmgr->configure(-nfs => 0);
# Using lock handles
my $lock = $lockmgr->lock("/some/file");
This simple locking scheme is not based on any file locking system calls such as flock() or lockf() but rather relies on basic file system primitives and properties, such as the atomicity of the write() system call. It is not meant to be exempt from all race conditions, especially over NFS. The algorithm used is described below in the ALGORITHM section.
It is possible to customize the locking operations to attempt locking once every 5 seconds for 30 times, or delete stale locks (files that are deemed too ancient) before attempting the locking.