flock

flockFILEHANDLE,OPERATION
The flock function is Perl’s portable file-locking interface. It locks only
entire files, not individual records. The function manages locks on the
file associated with FILEHANDLE, returning
true for success and false otherwise. To avoid the possibility of lost data, Perl flushes
your FILEHANDLE before locking or unlocking
it. Perl might implement its flock
using flock(2), fcntl(2),
lockf(3), or some other platform-specific lock
mechanism; if none of these is available, calling flock raises an exception. See the section
File Locking in Chapter 15.
OPERATION is one of LOCK_SH, LOCK_EX, or LOCK_UN, possibly or‘d with LOCK_NB. These constants are traditionally
valued 1, 2, 8, and
4, but you can use the symbolic names
if you import them from the Fcntl module, either individually or as a group using the
:flock tag.
LOCK_SH requests a shared lock,
so it’s typically used for reading. LOCK_EX requests an exclusive lock, so it’s
typically used for writing. LOCK_UN
releases a previously requested lock; closing the file also releases any
locks. If the LOCK_NB bit is used
with LOCK_SH or LOCK_EX, flock returns immediately rather than waiting for an unavailable lock. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access