File::Flock is a Perl module for file locking with flock.
lock($filename, undef, 'nonblocking');
lock($filename, 'shared', 'nonblocking');
my $lock = new File::Flock '/somefile';
Lock files using the flock() call. If the file to be locked does not exist, then the file is created. If the file was created then it will be removed when it is unlocked assuming it's still an empty file.
Locks can be created by new'ing a File::Flock object. Such locks are automatically removed when the object goes out of scope. The unlock() method may also be used.
lock_rename() is used to tell File::Flock when a file has been renamed (and thus the internal locking data that is stored based on the filename should be moved to a new name). unlock() the new name rather than the original name.