让我的Perl CGI脚本呈现HTML时遇到很多麻烦。它始终将HTML输出为纯文本。我什至尝试使用print header("text/html");
以下代码有什么问题吗? -
use CGI qw(:standard);
# other code
print header("text/html");
# also tried just print header;
my $banner = "Some text";
print start_html($banner);
print h3($banner);
print start_form(-method=>"POST");
# HTML form specific code
print end_form;
print end_html;
当我在Chrome开发人员工具上检查 Elements 标签时,出于某种原因,整个HTML都包裹在<pre>
标签内,而标签又在另一个HTML文档中。因此HTML格式不正确,但我无法理解为什么-
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;"><!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Some text</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Some text</h3><form method="post" action="/path/script.pl?param1=val1¶m2=val2" enctype="multipart/form-data">
//form specific code
</form>
</body>
</html></pre>
</body>
</html>
任何帮助将不胜感激。
答案 0 :(得分:2)
运行程序时,得到以下输出:
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Some text</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
</body>
</html>
这正是我所希望看到的,与您所看到的完全不同。我可以看到两种可能性来解释这种差异:
# other code
部分中有一些东西正在输出一组CGI标头(也许还有<pre>
标签)。区分这两种情况的一种简单方法是从命令行运行程序,然后查看获得的输出。如果您得到输出,则问题出在# other code
中,如果您得到我的输出,则问题出在Web服务器配置中。
顺便说一句,我强烈建议您阅读HTML Generation functions should no longer be used和CGI module documentation页面的最新版本中标题为CGI::Alternatives的部分,以获取一些更好方法的建议。
答案 1 :(得分:0)
以下代码有什么问题吗? -
是的。对于初学者来说,它甚至不编译。
$ perl <<'__EOS__'
use CGI qw(:standard);
// other code
print header("text/html"); // also tried just print header;
my $banner = "Some text";
print start_html($banner);
print h3($banner);
print start_form(-method=>"POST");
// HTML form specific code
print end_form;
print end_html;
__EOS__
Bareword found where operator expected at - line 3, near "// other"
(Missing operator before other?)
Bareword found where operator expected at - line 3, near "other code"
(Do you need to predeclare other?)
Bareword found where operator expected at - line 5, near "// also"
(Missing operator before also?)
Bareword found where operator expected at - line 11, near "// HTML"
(Missing operator before HTML?)
Bareword found where operator expected at - line 11, near "specific code"
(Do you need to predeclare specific?)
syntax error at - line 3, near "// other "
syntax error at - line 5, near "// also tried "
syntax error at - line 11, near "// HTML form "
Execution of - aborted due to compilation errors.
但是在将所有//
更改为#
之后,我们得到了一个很好的程序,它不会输出任何PRE元素。
$ perl <<'__EOS__'
use CGI qw(:standard);
# other code
print header("text/html"); # also tried just print header;
my $banner = "Some text";
print start_html($banner);
print h3($banner);
print start_form(-method=>"POST");
# HTML form specific code
print end_form;
print end_html;
__EOS__
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Some text</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Some text</h3><form method="post" action="http://localhost" enctype="multipart/form-data"></form>
</body>
</html>
答案 2 :(得分:-3)
尝试一下:
print(header(-type => 'text/html'));
至少对我有用。