G'day Stackoverflowers,
I'm the author of Perl's autodie pragma, which changes Perl's built-ins to throw exceptions on failure. It's similar to Fatal, but with lexical scope, an extensible exception model, more intelligent return checking, and much, much nicer error messages. It will be replacing the Fatal
module in future releases of Perl (provisionally 5.10.1+), but can currently be downloaded from the CPAN for Perl 5.8.0 and above.
The next release of autodie
will add special handling for calls to flock
with the LOCK_NB
(non-blocking) option. While a failed flock
call would normally result in an exception under autodie
, a failed call to flock
using LOCK_NB
will merely return false if the returned errno ($!
) is EWOULDBLOCK
.
The reason for this is so people can continue to write code like:
use Fcntl qw(:flock);
use autodie; # All perl built-ins now succeed or die.
open(my $fh, '<', 'some_file.txt');
my $lock = flock($fh, LOCK_EX | LOCK_NB); # Lock the file if we can.
if ($lock) {
# Opportuntistically do something with the locked file.
}
In the above code, a lock that fails because someone else has the file locked already (EWOULDBLOCK
) is not considered to be a hard error, so autodying flock
merely returns a false value. In the situation that we're working with a filesystem that doesn't support file-locks, or a network filesystem and the network just died, then autodying flock
generates an appropriate exception when it sees that our errno is not EWOULDBLOCK
.
This works just fine in my dev version on Unix-flavoured systems, but it fails horribly under Windows. It appears that while Perl under Windows supports the LOCK_NB
option, it doesn't define EWOULDBLOCK
. Instead, the errno returned is 33 ("Domain error") when blocking would occur.
Obviously I can hard-code this as a constant into autodie
, but that's not what I want to do here, because it means that I'm screwed if the errno ever changes (or has changed). I would love to compare it to the Windows equivalent of POSIX::EWOULDBLOCK
, but I can't for the life of me find where such a thing would be defined. If you can help, let me know.
Answers I specifically don't want:
- Suggestions to hard-code it as a constant (or worse still, leave a magic number floating about).
- Not supporting
LOCK_NB
functionality at all under Windows. - Assuming that any failure from a
LOCK_NB
call toflock
should return merely false. - Suggestions that I ask on p5p or perlmonks. I already know about them.
- An explanation of how
flock
, or exceptions, orFatal
work. I already know. Intimately.
Many thanks in advance,
Paul