Ubic::Lockf - file locker with an automatic out-of-scope unlocking mechanism


version 1.60


    use Ubic::Lockf;
    $lock = lockf($filehandle);
    $lock = lockf($filename);
    undef $lock; # unlocks either


lockf is a perlfunc flock wrapper. The lock is autotamically released as soon as the associated object is no longer referenced.


lockf($file, $options)

Create an Lockf 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 lockf 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:


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, permissions problems or the absence of a target file directory for example, an exception is raised.


Undef by default. If set, specifies the wait timeout for acquiring the blocking lock. The value of 0 is equivalent to blocking => 0 option.


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.


Gives the name of the file, as it was when the lock was taken.


Destroy lock object without unlocking.

This is useful in forking code, if you have one lock object in several processes and want to close locked fh in one process without unlocking in the other.


Vyacheslav Matyukhin <>


This software is copyright (c) 2016 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.