Lock::File - file locker with an automatic out-of-scope unlocking mechanism
version 1.01
use Lock::File qw(lockfile); # blocking mode is default my $lock = lockfile('/var/lock/my_script.lock'); # unlock undef $lock; # or: $lock->unlock(); # print filename say $lock->name; $lock = lockfile('./my.lock', { blocking => 0 }) or die "Already locked"; # lock an open file: $lock = lockfile($fh); $lock = lockfile_multi('./my.lock', 5); # will try to lock on files "my.lock.0", "my.lock.1" .. "my.lock.4" $lock = lockfile_any('foo', 'bar');
lockfile is a perlfunc flock wrapper. The lock is autotamically released as soon as the assotiated object is no longer referenced.
lockfile
flock
lockfile_multi makes non-blocking lockfile calls for multiple files and throws and exception if all are locked.
lockfile_multi
Create a Lock instance. Always save the result in some variable(s), otherwise the lock will be released immediately.
The lock is automatically released when all the references to the Lockf object are lost. The $file mandatory parameter can be either a string representing a filename or a reference to an already opened filehandle. The second optional parameter is a hash of boolean options. Supported options are:
$file
OFF by default. Tells to achieve a shared lock. If not set, an exclusive lock is requested.
ON by default. If unset, a non-blocking mode of flock is used. If this flock fails because the lock is already held by some other process, undef is returned. If the failure reason is somewhat different, e.g. permissions problems or the absence of a target file directory, an exception is thrown.
undef
Unset by default. If set, specifies the wait timeout for acquiring the blocking lock.
Throws an exception on timeout.
The value of 0 is equivalent to blocking => 0 option, except that it throws an exception instead of returning undef if the file is already locked.
blocking => 0
Undef by default. If set, a chmod with the specified mode is performed on a newly created file. Ignored when filehandle is passed instead of a filename.
OFF by default. If set, the lock file will be deleted before unlocking.
Alternatively, you can use OO interface:
my $lock = Lock::File->new($file, $options);
Calls non-blocking lockfile's for files from $fname.0 to $fname.$max-1, and returns a Lock::File object for the first successful lock.
$fname.0
$fname.$max-1
Lock::File
Only remove and mode options are supported.
Same as lockfile_multi, but accepts arrayref of filenames.
Gives the name of the file, as it was when the lock was taken.
Transform exclusive lock to shared.
Transform shared lock to exclusive. Can block if other shared/exclusive locks are held by some other processes.
Force the lock to be released independent of how many references to the object are still alive.
There're tons of file locking modules on CPAN. Last time I counted, there were at least 17.
And yet, every time I tried to find a replacement for our in-house code on which this module is based, every one of them had quirks which I found too uncomfortable. I had to copy our code as Ubic::Lockf when I opensourced Ubic.
I wanted to do the review of all those modules, neilb style. I never got around to that, and then I realized how much this task holds me back from releasing other useful stuff.
So... sorry for bringing yet-another-file-locking module into the world.
First, there's File::Lock::Multi, which is completely unrelated to this module.
Second, there are so many locking modules that choosing a good name is *hard*.
Third, maybe I'm going to release Lock::Zookeeper with the similar interface in the future.
lockf(), lockf_multi() and lockf_any() functions, and unlockf() method were renamed to lockfile.* in 1.01 release.
lockf()
lockf_multi()
lockf_any()
unlockf()
lockfile.*
It's unlikely that many people used 1.00 version in important code because it's been only a week since its release, but I'm keeping them around for a few releases anyway.
In other words, they are all exported on :all, and they print a warning message.
:all
File::Flock::Tiny
File::Lockfile
Lazy::Lockfile
Lockfile::Simple
File::Lock::Multi
File::Lock
File::FcntlLock
File::TinyLock
ExclusiveLock::Guard
File::lockf
And many others.
Vyacheslav Matyukhin <mmcleric@yandex-team.ru>
This software is copyright (c) 2013 by Yandex LLC.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Lock::File, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Lock::File
CPAN shell
perl -MCPAN -e shell install Lock::File
For more information on module installation, please visit the detailed CPAN module installation guide.