Next System Programming in Perl 40

Useful file locking utility

        #!/usr/bin/perl
        use Fcntl ':flock';
        die "Usage: with-lock lockfile command [args...]\n"
          unless @ARGV;
        my ($semaphore, $cmd, @args) = @ARGV;
        open my($sem), "+<", $semaphore
          or die "Couldn't open semaphore '$semaphore': $!\n";
        if (flock $sem, LOCK_EX) {
          system($cmd, @args);
        }


Next 40