3

我正在使用以下代码来测试向浏览器输出致命错误:

use CGI;
use CGI::Carp qw(fatalsToBrowser);

die "test";

我希望在浏览器中看到一些错误,但没有任何错误,我只是得到一个常规的 500 响应。我忘记了我为远程请求打开了自定义错误页面,现在我得到了Script failed to send data..

还:

> perl -w index.pl
Status: 500
Content-type: text/html

<h1>Software error:</h1>
<pre>test at index.pl line 4.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

</p>
[Mon Feb  8 18:29:52 2010] index.pl: test at index.pl line 4.
4

1 回答 1

4

尝试在任何事情之前打印几行新行。正如“CGI 标准”所说,这向服务器发出了 HTTP 标头结束的信号。或者你可以使用这样的东西:(从鲤鱼手册页复制):

use CGI::Carp qw(set_die_handler);
BEGIN {
   sub handle_errors {
      my $msg = shift;
      print "content-type: text/html\n\n";
      print "<h1>Oh gosh</h1>";
      print "<p>Got an error: $msg</p>";
  }
  set_die_handler(\&handle_errors);
}

如果这不起作用,这里还有一些技巧:

#!perl
BEGIN { $| = 1; print "\n\n" }
open STDERR, ">&STDOUT";

..以及这篇 Perl Journal 文章中的更多技巧。

于 2010-02-07T22:48:33.517 回答