in reply to Flocking
This code allows you to open the file, read its contents, modify them, and write them back out without having to close and reopen. I haven't tested it with flocking, so you'll need to play with it to see what type of flock works.# always test for failure open (FILEHANDLE, "+< $filename") || die "Can't open $filename in upda +te mode: $!\n"; # read entire file into array of lines @array = <FILEHANDLE>; # do stuff to the file here # go back to beginning of file handle seek(FILEHANDLE, 0, 0) or die ("Seek failed on $filename: $!\n"); print FILEHANDLE @array or die ("Print failed on $filename: $!\n"); # truncate the file so we don't have excess garbage at the end truncate(FILEHANDLE, tell(FILEHANDLE)) or die ("Truncate failed on $fi +lename: $!\n"); close (FILEHANDLE) or die ("Close failed on $filename: $!\n");
Please note that flocking a file doesn't stop others from playing with it. They can still modify the file if they don't check to see if it's flocked.
Further resources:
Cheers!
PS: I took the above code straight from the Perl Cookbook. Beg your parents for a copy.
|
|---|