I've written what's quickly becoming a hefty script and I need to implement file locking since it's a CGI that can be called multiple times - I haven't had it clobber a file yet but I'm gritting my teeth in morbid anticipation.
My question stems from the fact that every example of flock I've been able to find has called die when the lock fails.
My CGI script outputs pages all at once a la HTML::Template rather than piece by piece - if I die in the middle of the script, the user gets an ugly, ugly error message and not an HTML page. I don't want to do this under any circumstances.
Since die isn't an option I've set up the following:
if (sysopen(TRIGGERFILE, "$config->{xmlpath}trigfile.tf", O_WRONLY | O +_EXCL | O_CREAT)) { if (flock (TRIGGERFILE, LOCK_EX | LOCK_NB)) { print TRIGGERFILE "\n"; } else { push (@errors, qq(<p class="message">error message</p>)); } close TRIGGERFILE; } else { push (@errors, qq(<p class="message">error message</p>)); }
My question is about the close. If the lock fails, is the file still open? In other words, do I need a close statement after a failed lock or can I move it just below the statement that prints to the file?
Thanks again for your help, fellow monks.
In reply to Flock, die and statement order by Ionizor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |