use Fcntl qw(:flock); my $file = "my.lock"; open(FOO, ">> $file") or die "Cannot write to $file: $!"; print "Getting the lock\n"; flock (FOO, LOCK_EX) or die "Cannot flock $file: $!"; print "Locked by $$, hit enter to exit: "; ;