Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: CGI::Application - Which is the proper way of handling and outputting utf8

by isync (Hermit)
on Nov 19, 2007 at 10:56 UTC ( [id://651636]=note: print w/replies, xml ) Need Help??


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?

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://651636]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found