Where is flock() for Perl on Windows?
Question
I have a Perl script that I'd like to run on Windows, using either Strawberry Perl or ActivePerl; I don't care which. This script however, uses flock()
calls, which does not seem to be included in either of those versions of Perl.
Can anyone help get this up and running?
Solution
Is the Fcntl module installed? Try this:
perl.exe -MFcntl -e 1
If it complains, you don't have the Fcntl module installed. If it doesn't complain, then you have access to Fcntl::flock, so put this in your script:
use Fcntl qw(:DEFAULT :flock);
and off you go.
OTHER TIPS
Try using perldoc -f flock to check the things are supported & then look into the given example to know the usage criteria of the function. Here copied from the perldoc:
C:>perldoc -f flock
use Fcntl ':flock'; # import LOCK_* constant
sub lock {
flock(MBOX,LOCK_EX);
# and, in case someone appended
# while we were waiting...
seek(MBOX, 0, 2);
}
sub unlock {
flock(MBOX,LOCK_UN);
}
open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow