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 Answer
Reset to default 1Just 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
版权声明:本文标题:perl - CGI issue with writing UTF8 content - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736640593a1945984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
print $localcontent;
. Noencode
, nobinmode
. You're already encoding it with the->utf8
ofJSON->new->utf8->encode
. – ikegami Commented 2 days ago$data
) should be strings of Unicode Code Points aka decoded text. Please provideuse Data::Dumper; local $Data::Dumper::Useqq = 1; print( Dumper( $data ) );
if you still have problems. – ikegami Commented 2 days ago