(This is in reference to
Testing if your code successfully flocked.)
Notes:
- Proc::Background works in both Windows and Unix. It should work elsewhere, but see its documentation for more information.
- I wrote this to work on Solaris. Linux, apparently, associates the lock with the filehandle and not the file, so this may be unnecessary there.
- The shell-quoting stuff works for me. It may not work for you. If there's a better way to do it, please let me know and I'll update.
use Proc::Background;
my $proc = Proc::Background->new(
"perl -MFcntl=:flock -e 'open FH, \">>\", \"$filename\";exit flock
+ FH, LOCK_E
X|LOCK_NB",
);
my $rc = $proc->wait >> 8;
ok( !$rc, "Cannot flock $logfile" );