2012-04-17 55 views
5

使用Perl代碼如何刪除Perl中的CGI默認元字符集編碼?

#!/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" />

是在輸出和我不不知道如何擺脫它。

所有建議將不勝感激。

回答

4

-encoding參數添加到start_html和不建用手meta元素。 (儘管CGI文檔建議你這麼做)。

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

+1非常感謝! – 2012-04-17 19:45:20

+0

這僅增加了一個「」元素在HTML,它不會改變的字符集由'內容Type' HTTP標頭,如發送。 – Flimm 2014-05-06 14:19:22

3

最近的版本CGI.pm(我目前已經安裝了3.52),你不需要手動構建那個<meta>元素。您只需致電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> 
+0

不工作:(這實際上消除''和仍保持'' – 2012-04-17 19:23:43

+1

你有什麼版本的CGI.pm? – cjm 2012-04-17 19:27:18

+0

in'start_html'它是'-encoding',而不是'-charset ' – 2012-04-17 19:28:06