[cgiapp] utf8 form processing

Rhesa Rozendaal perl at rhesa.com
Wed Oct 15 11:11:34 EDT 2008


Mike Tonks wrote:
> Hi All,
> 
> I recently encountered the dreaded utf8 funny characters, again.  This
> time on the input data coming from form entry fields.
> 
> It's CGI.pm that actually does the processing, and needs to read the
> stream as utf8.  There is a flag for this, but I couldn't get that to
> work, so as a temporary measure I read all the parameters and pass
> them through decode_utf8.  Does anyone have a better method?

Here's what I use:

package CGI::as_utf;

BEGIN
{
     use strict;
     use warnings;
     use CGI;
     use Encode;

     {
         no warnings 'redefine';
         my $param_org = \&CGI::param;

         my $might_decode = sub {
             my $p = shift;
             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];

             goto &$param_org if scalar @_ != 2;

             return wantarray
                 ? map { $might_decode->($_) } $q->$param_org($p)
                 : $might_decode->( $q->$param_org($p) );
             }
     }
}

1;

This does the right thing for file uploads, as well as handling scalar and 
list context.


rhesa


More information about the cgiapp mailing list