[cgiapp] Re: utf8 form processing
Mark Stosberg
mark at summersault.com
Thu Oct 16 10:12:41 EDT 2008
On Wed, 15 Oct 2008 17:11:34 +0200
Rhesa Rozendaal <perl at rhesa.com> wrote:
> 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.
That looks useful, Rhesa.
Is there a variation of it that makes sense to submit as patch for CGI.pm?
Mark
More information about the cgiapp
mailing list