Send warnings generated by a CGI script to the browser.

Should not be used in production code, but instead as aid in diagnosing warnings that don't get produced in off-line testing.

{ my @warns; BEGIN { $SIG{__WARN__}= sub { push @warns, $_[0]; } } END { if( @warns ) { print "\n<html><p>Warnings:<pre>\n", @warns, "</pre></p></html>\n"; } } }

Replies are listed 'Best First'.
(tye)Re: warningsToBrowser
by tye (Sage) on Sep 29, 2001 at 10:38 UTC

    Someone complained about not being able to use "fatalsToBrowser", so here is a quick cut-n-paste way of getting both:

    { my( $warn, @warns ); BEGIN { $warn= sub { my( $head, $tail )= ( @_, "", "" ); if( @warns ) { print "\n$head<p>Warnings:<pre>\n", @warns, "</pre></p>$tail\n"; @warns= (); } }; $SIG{__WARN__}= sub { push @warns, $_[0]; }; $SIG{__DIE__}= sub { print "Content-Type: text/html\r\n\r\n", "<html><head><title>Fatal error</title>\n", "</head><body>"; &$warn(); print "<p>Died because:\n<pre>$_[0]</pre></p>", "</body></html>\n"; exit 0; }; } END { &$warn( "<html>", "</html>" ); } }

    Note that this breaks eval like old versions of CGI::Carp, so install a good version of CGI::Carp to fix that problem. (:

            - tye (but my friends call me "Tye")