Well, my CGI program was installed in a controlled environment, where errors should be very rare, and users were generally _very_ clueless. I can't just have my program die on them! Instead, my program would skip the file operation in question, but log an error, and display an error message with a possible solution, print out the rest of the page layout, and then end at the same spot the program would normally.
Maybe it's a matter of taste, but I just don't like the or die construct, it doesn't give me the ability to neatly and readably handle the exception.
Example:
sub display_values_from_file {
my $filename = shift;
if(open FILE, "<$filename") { # check return value of open
flock FILE, 1;
while(<FILE>) {
print &process_line_from_file;
}
close FILE;
} else {
print "The file could not be opened. Please try again later!";
log_error "open $filename : $!";
}
}
# main program
... # do something
print $some_nice_html;
display_values_from_file "/foo/bar.baz";
print $rest_of_html_page;
# end
This way, when an error occurs, the user will be informed of the error, and all the links the user would need would still be there. |