[cgiapp] UTF-8 output

Cees Hek ceeshek at gmail.com
Wed Nov 4 06:19:15 EST 2009


On Wed, Nov 4, 2009 at 9:49 PM, Michael Lackhoff
<lackhoff at zbmed.uni-koeln.de> wrote:
> On 04.11.2009 11:24 Cees Hek wrote:
>
>> Can't you get DBI to do the decoding for you?  I think it will depend
>> on the DBD module you use, but it is quite easy with PostgeSQL:
>
> I am using SQLite and it is also quite easy there but then I have -- as
> you say -- _decoded_ values. This is what I want within my application
> but as soon as I produce some output (let CGI::Application print the
> template with embedded DB data) Perl does its evil magic and encodes
> everything to latin1 -- at least this is what I get.
>
> Do you do some magic with STDOUT or how do you get the UTF-8 to the
> browser? Whatever I try, Perl always outputs latin1 from decoded strings
> and the only remedy I found was to not let it output decoded strings but
> encoded UTF-8 octets.

You mean something like this:

__PACKAGE__->add_callback('postrun', sub {
    my $self = shift;

    # Make sure the output is utf8 encoded if it needs it
    if ( $_[0] && ${$_[0]} && utf8::is_utf8(${$_[0]}) ){
        utf8::encode( ${$_[0]} );
    }

    return;
});

I have that in my CGI::App base class.

> Sorry for the long thread but this is a horror I encounter over and over
> again and I am hoping for a better understanding to get it right once
> and for all.

Not a problem.  Utf8 support is tricky to get right, and I am still
not sure if I have tackled everything myself.  I'm sure others will
get something out of the discussion as well.

Cheers,

Cees


More information about the cgiapp mailing list