note
rowdog
<p>
If windows supports hard links you could try something like this.
</p>
<code>
#!/usr/bin/perl
use strict;
use warnings;
open my $fh, ">", "mylock.$$" or die "open mylock.$$: $!";
sleep 2 until get_lock();
print "locked> ";
<>;
unlink "mylock"; # releases the lock
unlink "mylock.$$"; # just cleaning up
sub get_lock {
link "mylock.$$", "mylock" and return 1;
return (stat "mylock.$$" )[3] == 2;
}
</code>
<p>
The above is based on the discussion of portably locking with link(2) as found on the Linux man page for
<a href="http://www.kernel.org/doc/man-pages/online/pages/man2/open.2.html">open(2)</a> in the O_EXCL discussion.
</p>
<p>Edit: added comments because one unlink matters and the other doesn't.</p>
885300
885300