我在使用Perl CGI脚本呈现HTML时遇到了很多麻烦。它一直以纯文本形式输出HTML。我甚至尝试使用print header("text/html");
明确设置Content-Type
以下代码有什么问题吗? -
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的Developer工具上检查Elements选项卡时,由于某种原因,整个HTML被包装在<pre>
标签内,而 <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>
标签又在另一个HTML文档中。所以HTML格式不正确,但我无法理解为什么 -
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
这正是我期望看到的,而不是你所看到的。我可以看到解释这种差异的两种可能性:
<pre>
部分有一些东西输出一组CGI标题(也许还有# other code
标签)。区分这两种情况的一种简单方法是从命令行运行程序,看看你得到了什么输出。如果你得到你的输出,那么问题出在HTML Generation functions should no longer be used的某个地方,如果你得到我的输出,那么问题出在Web服务器配置中。
顺便说一句,我强烈建议您阅读最新版本的CGI module documentation和CGI::Alternatives页面中标题为$ 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.
的部分,以了解更好方法的一些建议。
以下代码有什么问题吗? -
是。对于初学者来说,它甚至都没有编译。
//
但是在将所有#
更改为$ 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>
之后,我们得到了一个完美的程序,它不会输出任何PRE元素。
print(header(-type => 'text/html'));
试试这个:
qazxswpoi
至少那对我有用。