在 Perl CGI 脚本中打印 HTML 后该怎么办?
从 Perl CGI 脚本打印 HTML 后应该调用什么? 我见过空的 return
语句、exit
语句,在某些情况下什么也没有。 有关系吗?
#!perl
print "Content-type: text/html\n\n";
print <<'END_HTML';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
END_HTML
# do anything else here?
# return;
# exit;
更新
让我们假设您有一些测试,您正在打印不在文件末尾的 HTML。 在这种情况下,调用 exit 或 return 来直观地显示脚本应该在那时结束是否更清楚? 我知道这不是最好的写法 - 请仅从表面上理解这个问题。
#!perl
use CGI;
my $q = CGI->new();
my $action = $q->param('action');
my $html_start = "Content-type: text/html\n\n";
$html_start .= <<'END_HTML';
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hello world!</title>
</head>
<body>
END_HTML
my $html_end = <<'END_HTML';
</body>
</html>
END_HTML
if ($action eq 'foo') {
print $html_start;
print '<p>foo</p>';
print $html_end;
# do anything else here?
}
else {
print $html_start;
print '<p>bar</p>';
print $html_end;
# do anything else here?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该始终在输出末尾打印出“\n\n”,以告诉网络服务器输出已完成。 Apache 可能会在成功终止脚本后为您执行此操作。
如果脚本完成,从技术上讲,您应该使用适当的退出状态调用“exit”——零表示成功,非零表示错误。 Web 服务器检查退出状态并呈现相应的页面(如果退出状态显示除成功之外的任何内容,则通常会出现错误代码 500)。 如果您没有指定“返回”值或直接调用 exit,Perl(在本例中)将假定成功终止,并且 Web 服务器将像调用“exit(0)”一样对待您的 CGI 脚本。
这一切都在 CGI 规范中进行了描述(当前为 RFC 3875,IIRC)。
You should always print out "\n\n" at the end of your output to tell the webserver the output is finished. Apache may do this for you on successful script termination.
If your script is finished, you should technically call "exit" with the appropriate exit status-- zero for success, nonzero for errors. The webserver inspects the exit status and renders the appropriate page (error code 500 is typical if the exit status shows anything other than success). If you don't specify a "return" value or call exit directly, Perl (in this case) will assume successful termination and the webserver will treat your CGI script the same as if you called "exit(0)".
This is all described in the CGI specification (RFC 3875 is current, IIRC).
我想这更多的是关于编码风格而不是功能的问题。
如果您有一个简单、直接的程序,没有分支,则没有理由用任何特殊的东西来标记程序的终止:
但是,如果结构更复杂,我建议用一些东西标记终止点具体来说,是向阅读您代码的人发出信号,表明对于定义的一组条件,不再需要考虑任何内容。
我会使用exit而不是return,原因很简单,return可能会被误认为是子程序的结束,而exit非常明确地表明程序终止此时。
I guess this is more a question about coding style than functionality.
If you have a simple, straightforward program, with no branching, there is little reason to mark the termination of the program by anything special:
However, if the structure is more complex, I would advise to mark the termination point by something specific, to signal to someone who reads your code that, for that defined set of conditions, there is nothing to take into account there anymore.
I would use exit rather than return, for the simple reason that return can be mistaken for the end of a sub, whereas exit very specifically states that the program terminates at this point.
不,没关系。 我总是让脚本自行终止而不是“返回”; 或“退出”; 因为不需要这些语句中的任何一个(即在 Perl 文档的末尾),并且它与“exit;”相同 或“退出 0;”。
No, it doesn't matter. I always let the script die by itself instead of 'return;' or 'exit;' since there's no need for either of those statements (in the end of a perl document that is) and it's the same thing as 'exit;' or 'exit 0;'.
我建议使用 return 而不是 exit,这样如果你切换到 CGI::Fast,你就可以将它变成一个 sub。
I would recommend return instead of exit so that if you switch to CGI::Fast you can just turn it into a sub.
如果它是一个独立的事物,那么您不一定必须返回或退出,这是有道理的。 但如果这个片段来自函数内部怎么办? 它仍然适用吗? 出于某种原因,返回或退出会是首选吗? 仍然只是造型吗?
it makes sense that you don't necessarily have to return or exit if it is a stand-alone thing. but what if this snippet is from within a function? would it still apply? would return or exit be preferred for some reason? is it still just styling?
要回答您的“更新”问题:在我当前的应用程序中,我使用常见的方法来做这样的事情:
如果您喜欢冒险,您可以 挂钩
BEGIN
和END
自动执行此操作,但我不会 - 我认为这太麻烦了周围为了一个相当小的好处。如果我必须重新来过的话,我现在不会这样做:我会更多地依赖某种中间层,例如 Catalyst,这将使我能够快速升级到 mod_perl 或返回到 CGI 。 或者更好的是,从头开始编写我的应用程序以使用更多 AJAX - 这样,我的大部分“操作”都是由非常小的 Perl 脚本执行的,这些脚本仅处理输入并响应
中的 Javascript 应用程序文本/纯文本
或文本/json
。To answer your "updated" question: In my current application, I use common methods to do something like this:
If you're feeling adventurous, you could hook
BEGIN
andEND
to do this automatically, but I wouldn't - I think it'd be too much fiddling around for a fairly minor benefit.This isn't how I'd do it now, if I had to do it all over: I'd rely more on some sort of middle layer such as Catalyst, which would allow me to move up quickly to mod_perl or back to CGI. Or even better, write my application from the ground-up to use more AJAX - that way, most of my "actions" are carried out by really small Perl scripts which just process the input and respond back to the Javascript application in
text/plain
ortext/json
.