Why not adopt me?
NAME
File::Flock::Tiny - yet another flock package
SYNOPSIS
my $lock = File::Flock::Tiny->lock($file);
... # do something
$lock->release;
DESCRIPTION
Simple wrapper around flock for ease of use.
METHODS
File::Flock::Tiny->lock($file)
Acquire exclusive lock on file. $file may be a file name or opened file handler. If filename given and file doesn't exist it will be created. Method returns lock object, file remains locked until this object will go out of the scope, or till you call release method on it.
File::Flock::Tiny->trylock($file)
Same as lock but if $file already locked immediately returns undef.
File::Flock::Tiny->write_pid($file)
Try to lock the file and save PID into it. Returns the lock object, or undef if file already locked.
$lock->release
Unlock file
$lock->close
Close locked filehandle, but do not release lock. Normally if you closed file it will be unlocked, but if you forked after locking file and closed lock in parent process, file will still be locked. Following example demonstrates the use for this method:
{
my $lock = File::Flock::Tiny->lock("lockfile");
my $pid = fork;
unless ( defined $pid ) {
do_something();
}
$lock->close;
}
# file still locked by child. Without $lock->close,
# it would be unlocked by parent when $lock got out
# of the scope
Note, that this behaviour is not portable! It works on Linux and BSD, but on Solaris locks are not inherited by child processes, so file will be unlocked as soon as parent process will close it. See also description of flock in perlfunc.
AUTHOR
Pavel Shaydo, <zwon at cpan.org>
BUGS
Please report any bugs or feature requests via GitHub bug tracker at http://github.com/trinitum/perl-File-Flock-Tiny/issues.
SEE ALSO
A lot of modules with similar functionality on CPAN, it just happened that I don't like any of them.
LICENSE AND COPYRIGHT
Copyright 2011 Pavel Shaydo.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.