#!/usr/bin/perl use IO::File; use File::Copy; use Fcntl qw(:DEFAULT :flock); print "opening and locking file~\n"; my $fh_lock = new IO::File("file~",O_RDWR | O_CREAT); flock($fh_lock,LOCK_EX) or die "failed to lock file1"; print "opening file1\n"; my $fh1 = new IO::File("file1",O_RDWR | O_CREAT); print "opening file2\n"; my $fh2 = new IO::File("file2",O_WRONLY | O_CREAT); print "reading from file1\n"; seek($fh1,0,2); close($fh1); print "writing to file2\n"; print $fh2 "file2"; close($fh2); print "renaming file2 over file1\n"; rename("file2","file1") or print "failed to rename file2 over file1, $!\n"; print "unlocking file~\n"; #flock($fh_lock,LOCK_UN); #bad with older flock implementations close($fh_lock); unlink("file~") or print "couldn't remove lock - someone else must have it";