#!/usr/bin/perl use IO::File; use Fcntl qw(:DEFAULT :flock); print "opening file1\n"; my $fh1 = new IO::File("file1",O_RDONLY | O_CREAT); print "locking file1\n"; flock($fh1,LOCK_EX) or die "failed to lock file1"; print "opening file2\n"; my $fh2 = new IO::File("file2",O_WRONLY | O_CREAT); print "writing to file2\n"; print $fh2 "file2"; close($fh2); print "renaming file2 over file1\n"; rename("file2","file1") or die "failed to rename file2 over file1"; print "unlocking file1\n"; #flock($fh1,LOCK_UN);#probably not needed as close does it #(might cause issues with buffering?) close($fh1);