【问题标题】:How to remove CGI default meta charset encoding in Perl?如何在 Perl 中删除 CGI 默认元字符集编码?
【发布时间】:2012-04-17 19:12:05
【问题描述】:

使用 Perl 代码

#!/usr/bin/perl

use strict;
use warnings;
use CGI ":all";
use Encode;

my $cgi = new CGI;

$cgi->charset('utf-8');

print $cgi->header(-type    => 'text/html',
                   -charset => 'utf-8');

print $cgi->start_html(-title => 'Test',
                       -head  => meta({-http_equiv => 'Content-Type',
                                       -content => 'text/html; charset=utf-8'}));
my $text = 'test'; # for now

Encode::from_to($text, 'latin1', 'utf8');

print $cgi->p($text);
print $cgi->end_html;

我得到以下输出:

Content-Type: text/html; charset=utf-8

<!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>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>test</p>
</body>

我不知道为什么

&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;

在输出中,我不知道如何摆脱它。

我们将不胜感激所有建议。

【问题讨论】:

    标签: perl encoding character-encoding cgi meta


    【解决方案1】:

    -encoding 参数添加到start_html 并且不要手动构建元元素。 (尽管 CGI 文档建议您这样做)。

    print $cgi->start_html(-title => "Test", -encoding => "utf-8")
    

    【讨论】:

    • 这只会在 HTML 中添加一个 '' 元素,它不会更改 Content-Type HTTP 标头发送的字符集。
    【解决方案2】:

    使用最新版本的 CGI.pm(我目前安装了 3.52),您不需要手动构造 &lt;meta&gt; 元素。您只需在调用 header 方法时提供字符集。这个程序:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use CGI ":all";
    use Encode;
    
    my $cgi = CGI->new;
    binmode STDOUT, ':utf8';
    
    print $cgi->header(-type => 'text/html',
                       -charset => 'utf-8');
    
    print $cgi->start_html(-title => 'Test');
    my $text = "\x{201c}test\x{201d}"; # for now
    
    print $cgi->p($text);
    print $cgi->end_html;
    

    给我这个输出:

    Content-Type: text/html; charset=utf-8
    
    <!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>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <p> test </p>
    </body>
    </html>
    

    【讨论】:

    • 不工作:(这实际上删除了&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;并仍然保留&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
    • 你有什么版本的 CGI.pm?
    • start_html 中是-encoding,而不是-charset
    • @stackoverflow,你从来没有回答过你拥有哪个版本的 CGI.pm。我很好奇为什么它不适合你。
    • @stackoverflow,如果您查看更改文件,您会看到:“版本 3.16,2006 年 2 月 8 日 ... 7. 将 start_html() 和 header() 中的字符集固定在同步。”所以你发现了一个在下一个版本(现在是 6 年前发布)中得到修复的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多