脚本头的过早结束(Perl)

3zz*_*zzy 1 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>";
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

脚本头的过早结束:version.cgi

mrk*_*mrk 6

您需要删除额外的反斜杠

这段代码:

print "Content-type: text/html\\n\\n";
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

print "Content-type: text/html\n\n";
Run Code Online (Sandbox Code Playgroud)

编辑

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

#!C:\xampp\apache\bin\httpd.exe
Run Code Online (Sandbox Code Playgroud)

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

编辑2

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

 use strict;
 use warnings;
Run Code Online (Sandbox Code Playgroud)

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