Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
See Re^4: CGI.pm: automatically decode param() for another workaround of CGI.pm input.

Here's my complete patch for CGI.pm. The module assumes your pages and forms are always in utf8, and that you always use the OO interface of CGI.pm (which should be the case in CGI::Application).
package CGI::as_utf8; BEGIN { use strict; use warnings; use CGI; use Encode; { no warnings 'redefine'; my $param_org = \&CGI::param; my $might_decode = sub { my $p = shift; # make sure upload() filehandles are not modified return ( !$p || ( ref $p && fileno($p) ) ) ? $p : eval { decode_utf8($p) } || $p; }; *CGI::param = sub { my $q = $_[0]; # assume object calls always my $p = $_[1]; # setting a param goes through the original interface goto &$param_org if scalar @_ != 2; return wantarray ? map { $might_decode->($_) } $q->$param_org($p) : $might_decode->( $q->$param_org($p) ); } } } 1;
Usage is simple. Just add a use CGI::as_utf8; in your CGI::Application module(s). It's been battle-tested on a site that does about 7 million cgi hits per day, so it works in practice. Suggestions for improvements are welcome though!

In reply to Re: CGI::Application - Which is the proper way of handling and outputting utf8 by rhesa
in thread CGI::Application - Which is the proper way of handling and outputting utf8 by isync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-23 20:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found