应该使用CGI.pm的标头方法来输出Content-Type标头吗?
如果我在 Perl 中使用 CGI 模块,我可以这样做
$cgi = CGI->new();
$cgi->header(-type=>"text/html");
或者选择经典
print "Content-Type: text/html\r\n\r\n";
我们使用哪个重要吗?两者有什么区别吗?两者似乎都有效。
对我来说,如果我使用 CGI ,我会选择第一个,但如果没有的话我不会只为了一项操作而加载模块。但只是想知道是否有关于这个主题的任何事情?
心理
If I'm using the CGI module in Perl, I can do this
$cgi = CGI->new();
$cgi->header(-type=>"text/html");
Or go for the classic
print "Content-Type: text/html\r\n\r\n";
Does it matter which we use? Is there any difference between the two? Both seem to work.
For me I'd go for the first if I was using CGI anyway, but if not then I wouldn't bother loading the module for just one action. But just wondering if there is any thing on the subject?
Psy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
严格来说,你必须打印 \r 字符:
这是表达你想说的内容的合法方式。
一般来说,我会坚持使用 CGI 提供的功能。
它允许更简洁和可读的代码,并且 CGI 比您更了解这些细节。
Strictly speaking, you have to print \r characters:
is a legal way to express what you want to say.
Generally, I'd stick with what CGI offers.
It allows much more concise and readable code, and CGI knows a lot more about such details than you do.
严格来说,您必须打印
CRLF
对。这与“\r\n\r\n”不同,除非您首先使用binmode STDOUT
。您应该使用CGI.pm。或者,如果像我一样,您不想要所有历史包袱,请使用 CGI::Simple 。
Strictly speaking, you have to print
CRLF
pairs. That is not the same thing as "\r\n\r\n" unless you usebinmode STDOUT
first.You should use CGI.pm. Or, if, like me, you do not want all the historical baggage, use CGI::Simple.
在 Perl 中,转义码“\r”和“\n”不保证与“\015”和“\012”相同。
如果要打印网络协议的 CRLF,则应使用“\015\012”或“\cM\cJ”。
这是直接来自 perldoc perlop 的引用:
还有更多类似的信息,来自 perldoc -f binmode:
In perl, the escape codes "\r" and "\n" are NOT guaranteed to be identical to "\015" and "\012".
If you want to print a CRLF for a network protocol, you should use "\015\012" or "\cM\cJ".
Here's the quote, direcly from perldoc perlop:
And more, similar info, from perldoc -f binmode:
不好的是,
您无法添加其他标头,如果您想使用 CGI 添加 Content-Type 和 Cookies,这里是工作示例解决方案
the bad using
you can't add another header, here working example solution if you want to add Content-Type and Cookies using CGI