admin管理员组

文章数量:1125335

I use mysql, Perl and Apache on Ubuntu.

I have the following code:

use utf8;
use CGI('-utf8'); #added based on another forum article i was reading.
use Encode;
.....

my $localcontent = JSON->new->utf8->encode($data); 
#i write $localcontent back in mysql and can see that it is good.

print $cgi->header(
    -type          => 'application/json',
    -cookie      => $_sessioncookie,
    -content_length => length($localcontent),
    -charset        => 'UTF-8'
);
Encode::encode( 'UTF-8', $localcontent );
#i also tried binmode for STDOUT (binmode(STDOUT, ':encoding(UTF-8)');) but that gives the same result.

Then in the browser I get unreadable content, the special chars are messed up. Any tips how I can proceed with debugging or has tips about setting this up correctly?

I use mysql, Perl and Apache on Ubuntu.

I have the following code:

use utf8;
use CGI('-utf8'); #added based on another forum article i was reading.
use Encode;
.....

my $localcontent = JSON->new->utf8->encode($data); 
#i write $localcontent back in mysql and can see that it is good.

print $cgi->header(
    -type          => 'application/json',
    -cookie      => $_sessioncookie,
    -content_length => length($localcontent),
    -charset        => 'UTF-8'
);
Encode::encode( 'UTF-8', $localcontent );
#i also tried binmode for STDOUT (binmode(STDOUT, ':encoding(UTF-8)');) but that gives the same result.

Then in the browser I get unreadable content, the special chars are messed up. Any tips how I can proceed with debugging or has tips about setting this up correctly?

Share Improve this question edited 2 days ago Rawley Fowler 2,52410 silver badges20 bronze badges asked 2 days ago Frans bFrans b 312 bronze badges 5
  • 1 print $localcontent;. No encode, no binmode. You're already encoding it with the ->utf8 of JSON->new->utf8->encode. – ikegami Commented 2 days ago
  • Also, all the strings in your data structure ($data) should be strings of Unicode Code Points aka decoded text. Please provide use Data::Dumper; local $Data::Dumper::Useqq = 1; print( Dumper( $data ) ); if you still have problems. – ikegami Commented 2 days ago
  • 1 CGI is no longer a core module, and many recommend not using it. For example, the author himself in the documentation. – TLP Commented 2 days ago
  • I'm aware of CGI being deprecated. that is the next project unless CGI is part of the problem. – Frans b Commented yesterday
  • After removing all instances of binmode and Encode, this is the output from Dumper: {\\"user_lable\\":\\"Usu\\303\\241rio\\"}. Portugese translation for User. (should be: Usuário) – Frans b Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 1

Just print the value instead of using Encode:

use utf8;
use CGI('-utf8');
use JSON;

my $data = { foo => 'Бar' };

my $localcontent = JSON->new->utf8->encode($data); 

print $cgi->header(
    -type           => 'application/json',
    -cookie         => $_sessioncookie,
    -content_length => length($localcontent),
    -charset        => 'UTF-8'
);

print $localcontent;

CGI.pm is deprecated, you should look into Plack or Mojolicious.

Here's an example of this same application using Plack (without a framework):

use utf8;
use Plack::Builder;
use Plack::Request;
use JSON;

my $data = { foo => 'Бar' };

my $localcontent = JSON->new->utf8->encode($data);

builder {
    mount "/" => sub {
        my $req = Plack::Request->new(shift);
        my $res = $req->new_response(200);
        $res->content_type('application/json; charset=utf-8');
        $res->body($localcontent);
        return $res->finalize;
    };
};

Then just run via plackup <file name>.pl. You mention using Apache as well, so you'd most likely need to change over to mod_proxy from mod_cgi to proxy to the Plack process.

本文标签: perlCGI issue with writing UTF8 contentStack Overflow