plack / Plack

PSGI toolkit and server adapters
http://plackperl.org/
Other
486 stars 214 forks source link

use utf8 pragma breaks encoding #580

Closed MaxPerl closed 8 years ago

MaxPerl commented 8 years ago

Hello all, I noticed a curious problem: I know that I need to send output as a bytestring. Therefore the following worked as expected: `

! /usr/bin/env perl

use strict; use Plack::Request;

my $app = sub { my $env = shift;

my $request = Plack::Request->new($env);
my $body = "<head><meta charset=\"utf-8\"</head><p>Höölölüßßß</p>";
#Encode::decode('utf8', $body);
#Encode::encode('utf8', $body);
return [
    '200',
    [ 'Content-Type' => 'text/html', 'charset' => 'utf-8' ],
    [ $body ],
];

}; `

But if I add the pragma "use utf8;" with the same code, encoding is broken.

Anyone an idea, what is the reson for that?

syohex commented 8 years ago

$body is not bytes when you put utf8 pragma. You need to make $body be bytes as below.

#! /usr/bin/env perl
use strict;
use Plack::Request;
use utf8;

my $app = sub {
    my $env = shift;

    my $request = Plack::Request->new($env);
    my $body = "<head><meta charset=\"utf-8\"</head><p>Höölölüßßß</p>";
    return [
        '200',
        [ 'Content-Type' => 'text/html', 'charset' => 'utf-8' ],
        [ Encode::encode_utf8($body) ],
    ];
};
MaxPerl commented 8 years ago

Dear syohex, thank you very very much for your answer. Now finally I see what I did wrong. I must write $body=Encode::encode('utf8) instead of only Encode::encode('utf8). Sometimes I am really blockhead. Sorry for disturbing and thank you all for your great work!!!