package HighLander;
use strict;
use Fcntl qw( LOCK_EX LOCK_NB );
open(our $fh, '<', $0)
or die("Can't open \"$0\": $!\n");
unless ( flock($fh, LOCK_EX|LOCK_NB )) {
print "Another instance of \"$0\" is already running. Exiting...\n";
exit(0);
}
1;
####
#!/usr/bin/perl
use strict;
use HighLander;
# if this script is already running, you'll never make it this far...
print "Running...\n";
sleep 10;
##
##
if ($! == ($^O =~ /Win32/ ? 33 : EWOULDBLOCK)) {
##
##
use Highlander qw( :verbose );
##
##
...
unless ( flock($fh, LOCK_EX|LOCK_NB )) {
if ($verbose) {
print "Another instance of \"$0\" is already running. Exiting...\n";
}
exit(0);
}
...