如何在Perl中删除CGI默认元字符集编码?

前端之家收集整理的这篇文章主要介绍了如何在Perl中删除CGI默认元字符集编码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用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>

我不知道为什么

< Meta http-equiv =“Content-Type”content =“text / html; charset = iso-8859-1”/>

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

所有建议将不胜感激.

解决方法

在start_html中添加一个-encoding参数,不要手工构建元素. (尽管CGI文档建议你做什么).

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

猜你在找的Perl相关文章