note
wolfi
barring any syntax errors:
<code>
use Fcntl qw(:DEFAULT :flock);
open (MYFILE1, "<", "$file")
or sysopen (MYFILE, "$file", O_RDONLY)
or die "$!";
flock (MYFILE1, LOCK_SH)
or die "$!";
chomp (@input=<MYFILE>);
close MYFILE1;
# pretending you then did your work on the input, split at the ":"
# and stuck the results back into @output...
open (MYFILE2, ">", "$file")
or sysopen (MYFILE, "$file", O_WRONLY)
or die "$!";
flock (MYFILE2, LOCK_EX)
or die "$!";
foreach $item(@output){
print MYFILE2 "$item"."\n";}
close MYFILE2;
</code>
<p>doing the double open-sysopen technique helps avoid races for opening the file and having that content altered in those few miliseconds.
<p>it's rare, if you have a small site, that two users would access the file at the same time - but i wouldn't take the risk. Besides, w/CGI work - if someone is ~intentionally~ messing w/your system -> you want to ensure that you're opening the right data and not something that was swapped in by a nefarious user. <i>(This is the reason for using 2 separate filehandles too -> MYFILE1 and MYFILE2)</i>.
<p>LOCK_EX <i>(exclusive locks)</i> are needed, when writing to files - otherwise, LOCK_SH <i>(shared locks)</i> are all that's needed, i believe, when reading.
<p>also: i'm not sure, if i'd actually use DIE like this in a cgi system, if the user is going to see the output. You probably want to 'die' gracefully -> using WARN, &subroutine, or some other action instead.
<p>for more info - see <a href='http://www.oreilly.com/catalog/pperl2/'>Camel</a> -> pgs 419-422, 571-573, 712, 714-715, 808-810
348758
348758