or download this
open(FH, "+< filename.txt") || die "$!"; #open the file for read/writ
+e
flock(FH, LOCK_EX); # get an exclusive lock
...
print FH $scalar; # print at beginning of file, clobbering ol
+d data
truncate(FH, tell(FH)); # truncate any data beyond where we've writ
+ten (is this correct as well?)
close(FH); # close the file