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

I am getting input from a CGI script that I am writing. And when I receive text from a textarea, it looks like this This+is+an+amazing+test%21%21%21 I had seen somewhere that a one line command to switch this over, but I can't find it, and i can't remember it. Any help would be appreciated! Thanks

Replies are listed 'Best First'.
Re: Cleaning up CGI input Data
by btrott (Parson) on Mar 14, 2000 at 02:05 UTC
    You should use CGI.pm.
    use CGI; my $q = new CGI; print $q->param('foo');
    where 'foo' is the name of your textarea field.
RE: Cleaning up CGI input Data
by Anonymous Monk on Mar 14, 2000 at 00:48 UTC
    1. Posting textarea via "GET" is suxx. Use "POST". 2. This function called uri_unescape
Re: Cleaning up CGI input Data
by Anonymous Monk on Mar 14, 2000 at 02:31 UTC
    Here it is once again... the Canonical de-CGI Regex (tm):
    $data =~ y/+/ /; $data =~ s/%[0-9a-f]{2}/pack('C', $1)/egi;
Re: Cleaning up CGI input Data
by Anonymous Monk on Mar 14, 2000 at 21:50 UTC
    I wasn't able to get the canonical de-cgi regex work, it seems to strip out any encoded characters. This is what I'm currently using:

    s/%(..)/pack('c',hex($1))/ge;

    Any thoughts on the difference between the two?

      The anonymous regex could be better written: $data =~ s/%[0-9a-fA-F]{2}/pack('c', hex($1)/eg; I believe.

      The difference is that, his first translates all + characters into spaces. His also only grabs two valid hexadecimal characters following a percentage sign, while your regex matches two of any characters (besides a new line) following a percentage sign. It's probably better to be more specific. Still, btrott is right. Using CGI.pm is the way to go.