13.11. Dying Young

Perhaps the most vexing problem to deal with in CGI programming is how to handle exceptions. If something in your program dies, it (normally) prints a message to STDERR and exits. The error message doesn't look like an HTTP header, so if it comes out before you get to print the headers you'll get a 500 Server Error. And if it comes out later, it won't be in HTML (or GIF, or whatever you're outputting) and will mess up the formatting of what you're in the middle of sending.

This is mostly addressed by the CGI::Carp module. With the statement use CGI::Carp qw(fatalsToBrowser); if your program ever dies, the message will be trapped and output to the browser.[7] So, to use a short example, consider the following CGI program:

[7] ...

Get Perl Debugged now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.