I run into two problems using CGI. Errors during execution caused by something and errors in parsing caused by me and fat fingers. The former creates a browser on the other end that has receiv ed half of a page of HTML. The later creates a user starting at a 500 error with no context. I fixed this tow ways:
- I buffer all output to the browser in a string and then flush that buffer to STDOUT before exiting. If I encounter a problem during running I can output a boilerplate html page with some info.
- Lighttpd executes a wrapper who runs the correct file. This can be expensive with cycles, but allows me to capture errors I've created.
In my module that manages web sessions I have a log() function that looks similar to this
log {
my @args = @_;
my $fmt = shift @args;
open my $fh, ">>", "/var/log/error.log" or return;
printf {$fh} $fmt, @args;
close $fh;
return;
}
The open, append, close logic is compatible with log rotation. Anything capture on STDERR when executing the real CGI script wil be written there.
The CGI script can also use the same function to write to the log file.
To use the buffering idea you have to capture the output of any methods you use in CGI that want to print to STDOUT. For years, the only methods I used in the CGI module was the ones to retrieve the form values. It is possible that CGI has its own buffer with flush.
I'll do something like this
{
my $buffer = "";
sub _print {
my @args = @_;
$buffer .= sprintf shift @args, @args;
return;
}
sub _flush {
my $content = $buffer;
$buffer = "";
local $| = 1;
print $content;
}
}
_print($cgi->header( -type => .... ));
......
_flus();
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.