moritz has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Brethren,

Most of my CGI scripts follow a common workflow:

  1. Fetch CGI GET and POST params
  2. Decode them into perls internal format
  3. Do some computations, and fill out a template
  4. Encode the template as utf8, and send it to the browser

My question is: how can I automate step 2, decode the return value of param().

The charset methods seems to affect only the generated header:

!/usr/bin/perl use strict; use warnings; use CGI; use utf8; my $q = new CGI; $q->charset('utf8'); my $str = $q->param('foo'); print utf8::is_utf8($str) ? 1 : 0, "\n"; print $CGI::VERSION, "\n"; __END__ $ perl foo.pl foo=bää 0 3.15

Currently my only idea is to subclass CGI, but somehow I think there should be a better solution. But what is it?

I've read Understanding CGI.pm and UTF-8 handling but it didn't enlighten me.

Replies are listed 'Best First'.
Re: CGI.pm: automatically decode param()
by Corion (Patriarch) on Sep 03, 2007 at 09:16 UTC

    Somewhere between 3.15 and 3.29 there was utf8 encoding added. I'm not sure if it will solve your problem(s) but maybe upgrading helps. The following was added to the param() method:

    my $charset = $self->charset || ''; my $utf8 = $charset eq 'utf-8'; if ($utf8) { eval "require Encode; 1;" if $utf8 && !Encode->can('decode'); # +bring in these functions return wantarray ? map {Encode::decode(utf8=>$_) } @{$self->{$na +me}} : Encode::decode(utf8=>$self->{$name}->[0]); } else { return wantarray ? @{$self->{$name}} : $self->{$name}->[0]; }
      Sorry, I missed to say: upgrading is not an option because I don't have administrative access on the server. (It runs Debian Stable. So let's hope for a fast release of Lenny ;-))

        Have you considered installing your own private Perl library then? Installing locally is easy for modules that you can simply copy, like CGI.pm. I haven't look at a workaround to make PREFIX work with Debian, so you'll have some trouble if you want to install Perl modules locally using the standard <c>perl Makefile.PL PREFIX=~moritz/perl-lib; make test; make install

        dance.