2012-04-17 11 views
5

Utilizando el código Perl¿Cómo eliminar la codificación de chaseet de metadatos por defecto de CGI en 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; 

estoy recibiendo el siguiente resultado:

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> 

Y yo no sé por qué

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

se encuentra en la salida y yo don No sé cómo deshacerse de eso.

Todas las sugerencias serán apreciadas.

Respuesta

4

añadir un parámetro -encoding a start_html y no construyen el elemento meta con la mano. (a pesar de lo que los documentos CGI sugieren que hagas).

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

+1 ¡Muchas gracias! –

+0

Esto solo agrega un elemento '' en el HTML, no cambia el juego de caracteres enviado por el encabezado HTTP 'Content-Type'. – Flimm

3

Con las versiones recientes de CGI.pm (actualmente tengo 3.52 instaladas), no debería necesitar construir ese elemento <meta> manualmente. Solo debe suministrar el juego de caracteres cuando llame al método header. Este programa:

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

me da este resultado:

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

No funciona :(Eso en realidad elimina '' 'y todavía mantiene ' –

+1

¿Qué versión de CGI.pm tienes? – cjm

+0

en' start_html' es '-encoding', no' -charset ' –

Cuestiones relacionadas