Ionizor has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Flock, die and statement order
by Zaxo (Archbishop) on Nov 18, 2002 at 05:14 UTC | |
by Ionizor (Pilgrim) on Nov 18, 2002 at 15:30 UTC | |
by iburrell (Chaplain) on Nov 19, 2002 at 00:24 UTC | |
by Ionizor (Pilgrim) on Nov 20, 2002 at 14:19 UTC | |
|
Re: Flock, die and statement order
by nothingmuch (Priest) on Nov 18, 2002 at 04:58 UTC |