use Fcntl ':flock'; @ARGV = "file.tab"; $^I = ".bak"; flock(ARGV,LOCK_EX); seek(ARGV, 0, 2); while (<>) { s/foo/bar/; print; } flock(ARGV,LOCK_UN);