How to remove CGI default meta charset encoding in Perl?
Asked Answered
I

3

6

Using the Perl code

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

I am getting the following output:

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>

And I don't know why

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

is in the output and I don't know how to get rid of it.

All suggestions will be appreciated.

Insociable answered 17/4, 2012 at 19:12 Comment(0)
P
5

add an -encoding parameter to start_html and don't build the meta element by hand. (despite what the CGI docs suggest you do).

print $cgi->start_html(-title => "Test", -encoding => "utf-8")
Paxon answered 17/4, 2012 at 19:44 Comment(1)
This only adds a '<meta>' element in the HTML, it does not change the charset as sent by the Content-Type HTTP header.Lateshalatest
K
6

With recent versions of CGI.pm (I currently have 3.52 installed), you shouldn't need to construct that <meta> element manually. You only have to supply the charset when you call the header method. This program:

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

gives me this output:

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>
Kibitz answered 17/4, 2012 at 19:19 Comment(9)
Not working :( That actually removes <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and still keeps <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />Clouded
What version of CGI.pm do you have?Kibitz
in start_html it's -encoding, not -charsetPaxon
@Kibitz - Comment of #evilotto is correct and solve the issue in my case. I am not sure about your system, maybe your default encoding is utf8 anyway, so your code is working fine regardless of the script modification. Thank you, I appreciate your time and help!Clouded
@stackoverflow, you never did answer which version of CGI.pm you have. I'm curious why it doesn't work for you.Kibitz
@Kibitz version 3.15 (not up to date), CPAN: 3.59 :-/ I am going to update, to check id that makes the problem hereClouded
@Kibitz - Huge apology from me, as with the latest version of CGI (3.59) it works even with your suggestion. But I am glad other suggestion is working for old version as well. Thank you!Clouded
@stackoverflow, if you look at the Changes file, you'll see: "Version 3.16, Feb 8, 2006 ... 7. Fixed charset in start_html() and header() to be in synch." So you found a bug that got fixed in the next version (released 6 years ago now).Kibitz
@Kibitz - Yes. And I am very sorry. I don't know what so old version of CGI we got here. I appreciate your comments, but it seems to be silly to change acceptance now, so I better will watch some your other posts and will vote there. Thanks!Clouded
P
5

add an -encoding parameter to start_html and don't build the meta element by hand. (despite what the CGI docs suggest you do).

print $cgi->start_html(-title => "Test", -encoding => "utf-8")
Paxon answered 17/4, 2012 at 19:44 Comment(1)
This only adds a '<meta>' element in the HTML, it does not change the charset as sent by the Content-Type HTTP header.Lateshalatest
C
-2

I have a working code

use CGI qw(:standard);

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

print 
    start_html(
        -title => 'Простой пример', 
        -bgcolor => "#cccccc",
        -encoding => "utf-8"
    );
Combative answered 11/3 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.