Perl 如何打开文件以打印行?

Perl 如何打开文件以打印行?,perl,Perl,这是我的脚本:它应该是一个hello world,如果您的页面被重定向,则显示您所在的最后一个页面,并在div中显示此页面本身的源代码: #!/usr/bin/perl BEGIN { $| = 1; open(STDERR, ">&STDOUT") ; #select(STDERR) ; $| = 1; #select(STDOUT) ; $| = 1; print "Content-type: te

这是我的脚本:它应该是一个hello world,如果您的页面被重定向,则显示您所在的最后一个页面,并在div中显示此页面本身的源代码:

#!/usr/bin/perl
BEGIN {
        $| = 1;
        open(STDERR, ">&STDOUT") ;
        #select(STDERR) ; $| = 1;
        #select(STDOUT) ; $| = 1;
        print "Content-type: text/html\n\n";
}

print <<EndHTML1
<html>
<head>
<title>HEEYYYYY YOOOUUUU GUUUUYYYYSSSS!</title>
</head>
<body>
<h1>Hello World!</h1>
<hr />
<p>You are accessing this page from $ENV{'HTTP_REFERER'}!</p>
<p>This page was written with perl and here is the source code:</p>
<div
style="background-color:#ABABAB;color:04FF00;margin:10px;border:5px;border-color:FF0000;width:100%;">
EndHTML1
if ( open(DATA, "< helloworld2.cgi")) { #it complains right here
        while (<DATA>) {
                print "<p>";
                print "$_";
                print "</p>\n";
        } # and here
}
else
{
        print "<p>Nope! Didn't work!</p>\n";
}
print <<EndHTML2
</div>
</body>
</html>
EndHTML2

在我看来这是对的。但是IDK这是我认真学习perl的第一天。仅供参考,该文件读取的是脚本本身。我不认为这有什么关系,因为它只是应该读的。并对其进行解释。

最好将open函数放在if块之外,然后删除if-else块。然后将Nope it NOT work消息放入die函数中。美元!将从打开的函数中告诉您错误

open(DATA, "< helloworld2.cgi") or die("Nope it didn't work : $!");
while (<DATA>) {
                print "<p>";
                print "$_";
                print "</p>\n";
} # and here

尝试在行中添加分号

print <<EndHTML1
所以它变成了

print <<EndHTML1;

这就是问题所在。据我所知。谢谢