脚本头的过早结束(Perl)

时间:2011-07-09 15:03:11

标签: perl cgi

#!C:\xampp\apache\bin\httpd.exe
$command=`perl -v`;
$title = "Perl Version";

print "Content-type: text/html\\n\\n";
print "<html><head><title>$title</title></head><body>";

print "
<h1>$title</h1>

\n";
print $command;

print "</body></html>";

我收到此错误:

  

脚本标题的提前结束:   version.cgi

2 个答案:

答案 0 :(得分:6)

您需要删除额外的反斜杠

此代码:

print "Content-type: text/html\\n\\n";

应该是这样的:

print "Content-type: text/html\n\n";

修改

此外,脚本中的第一行看起来不对。

#!C:\xampp\apache\bin\httpd.exe

这应该是Perl的路径,而不是httpd。

编辑2

最后,如果您在脚本的第一行之后添加了这两行,那么这一切都会更容易解决:

 use strict;
 use warnings;

使用-c -w标志在命令行上运行脚本以进行编译检查和警告 - 检查脚本,即perl -cw yourscript.cgi。这将为您提供脚本中的行数错误和警告。

总而言之,您的脚本可能如下所示:

#!C:\path\to\perl.exe

use strict;
use warnings;

my $command=$^V;
my $title = 'Perl Version';

print "Content-type: text/html\r\n\r\n";
print "
<html><head><title>$title</title></head><body>

<h1>$title</h1>

$command

</body></html>";

答案 1 :(得分:2)

您已将\\n写入标题中的\n

ETA:此外,perl -v不是获取版本的好方法。变量$^V包含更简洁和特定的版本号。