open(FILE, ">file.dat") or die "file.dat: $!"; while ( ! flock(FILE, 2) ) { # you *might* want to consider bailing out at # some point. } truncate FILE, 0; # important! to avoid corrupted files print FILE, "blah\n"; close(FILE); }