That's a good point Corion regarding locking $0. I updated the code by not appending the suffix ".lock" when path is given. The lock is released automatically upon termination of the script.
use Mutex::Flock;
( my $mutex = Mutex::Flock->new( path => $0 ) )->lock_exclusive;
...
Another way, with the timewait method.
my $mutex = MCE::Mutex::Flock->new( path => $0 );
# terminate script if a previous instance is still running
exit unless $mutex->timedwait(2);
...
The module is beneficial regarding supporting threads and processes. I will make time to complete it and subsequently a release for CPAN.
Regards, Mario
Edit: Added timedwait example.
|