[英]Why do Perl's and Python's output of print with “\n” differ?
为什么我需要在使用Perl的“Content-Type:text / html”之后两次放置“\\ n”,但是只用Python一次? 例如,以下Python脚本有效:
#!/usr/bin/python
print "Content-Type: text/html\n"
print "Hello World!"
但是以下Perl脚本不起作用(它提供了脚本头过早结束的错误消息):
#!/usr/bin/perl
print "Content-Type: text/html\n";
print "Hello World!";
相反,我需要添加一个额外的“\\ n”来让它工作:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "Hello World!";
因为Python中的打印使用换行符打印并且在Perl中打印不会。
print "Hello world!"
在Python中相当于在perl中print "Hello world!\\n"
。 Perl 6有一个say
命令,它与Python的打印功能相同,但遗憾的是,Perl 6没有稳定的实现。 在Perl 5.10或更高版本,可以用say
通过把use feature 'say'
在你的脚本。
Perl的print
不添加换行符。 Perl say
的确如此。 这些是等价的:
# Python
print "Content-Type: text/html"
print ""
print "Hello World!"
# Perl
print "Content-Type: text/html\n";
print "\n";
print "Hello World!\n";
# Perl
local $\ = "\n";
print "Content-Type: text/html";
print "";
print "Hello World!";
# Perl
use 5.010;
say "Content-Type: text/html";
say "";
say "Hello World!";
我建议不要碰$\\
; 它很容易影响你不希望它影响的代码。
Python的print
输出自动换行; Perl没有(除非你设置$\\ = "\\n"
)。 有了新的Perl,也say
,正如其他人所说的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.