in reply to CGI::Application - Which is the proper way of handling and outputting utf8

OK, now I have modified my application to start with:

use utf8; # to state that the script itself is in utf8
binmode STDIN, ":encoding(utf8)";
binmode STDOUT, ":encoding(utf8)";
use as_utf8; # which is the hack posted at http://www.perlmonks.org/?node_id=651574

in setup():
$self->header_add(-charset => 'utf-8');

Any final comments/thougts?
  • Comment on Re: CGI::Application - Which is the proper way of handling and outputting utf8

Replies are listed 'Best First'.
Re^2: CGI::Application - Which is the proper way of handling and outputting utf8
by mrajcok (Initiate) on Mar 10, 2010 at 18:41 UTC
    With the 'as_utf8' hack, you do not need binmode STDIN, ":encoding(utf8)";. In fact, it will cause problems if you have a binary/file upload field in a web form, since it will try to UTF-8 decode the incoming binary file/data. The as_utf8 hack correctly only decodes text form field data.

    Instead of binmode STDOUT, ":encoding(utf8)";, you can put the following in cgiapp_postrun():

    sub cgiapp_postrun { # overrides my ($self, $output_ref) = @_; utf8::encode( $$output_ref ) if utf8::is_utf8($$output_ref) }
    This will only UTF-8 encode the output if it needs encoding -- i.e., only if it $output_ref contains non-ASCII characters.