为什么Perl和Python的"\n"打印输出有所不同?

zjm*_*ler 3 python perl

为什么我需要在使用Perl的"Content-Type:text/html"之后两次放置"\n",但是只用Python一次?例如,以下Python脚本有效:

#!/usr/bin/python
print "Content-Type: text/html\n"
print "Hello World!"
Run Code Online (Sandbox Code Playgroud)

但是以下Perl脚本不起作用(它提供了脚本头过早结束的错误消息):

#!/usr/bin/perl
print "Content-Type: text/html\n";
print "Hello World!";
Run Code Online (Sandbox Code Playgroud)

相反,我需要添加一个额外的"\n"来让它工作:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "Hello World!";
Run Code Online (Sandbox Code Playgroud)

Raf*_*ler 15

因为Python中的打印使用换行符打印并且在Perl中打印不会.

print "Hello world!"在Python中等同print "Hello world!\n"于perl.Perl 6有一个say与Python的打印相同的命令,但遗憾的是,Perl 6没有稳定的实现.在Perl 5.10或更高版本中,您可以say通过放入use feature 'say'脚本来使用.


ike*_*ami 10

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!";
Run Code Online (Sandbox Code Playgroud)

我建议不要碰$\; 它很容易影响你不希望它影响的代码.