There are two sets of words that are used when dealing with files
-
Locking: LOCK_EX, LOCK_SH, LOCK_UN
-
Opening: O_RDONLY, O_RDWR, O_CREAT, O_EXCL, O_WRONLY
The locking words are not defined in Perl, unless you use the Fcntl
package with the parameter :flock
use Fcntl qw(:flock);
The opening words are not defined in Perl, unless you use the Fcntl
package with the parameter :DEFAULT
use Fcntl qw(:DEFAULT);
The only exception to this is when you are using DB_File: the opening
words are defined when using DB_File.
If you want to use both sets of words, then include both options
use Fcntl qw(:flock :DEFAULT);
The locking words
-
LOCK_EX
-
Obtain an exclusive lock. No other process can access the file while this
lock is in place.
-
LOCK_SH
-
Obtain a shared lock. The file can be shared with other files that have a
shared lock. The file cannot be shared with an exclusive lock.
-
LOCK_UN
-
Unlock the file. Removing all open file handles to the file will also unlock
the file.
The opening words
-
O_RDONLY
-
Open the file for reading only
-
O_WRONLY
-
Open the file for writing only
-
O_RDWR
-
Open the file for reading and writing
-
O_CREAT
-
Create the file if it doesn't exist
-
O_EXCL
-
Only open the file if it doesn't already exist.
-
Multiple Options
-
It is possible to specify multiple options by using the | operator. Common
combinations are
-
O_RDWR | O_CREAT
-
O_RDONLY | O_CREAT