Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

merlyn (47)

merlyn
  merlyn@stonehenge.com
http://www.stonehenge.com/merlyn/
AOL IM: realmerlyn (Add Buddy, Send Message)
Yahoo! ID: realmerlyn (Add User, Send Message)

PAUSE-ID: MERLYN [cpan.org].
See my home page [stonehenge.com].

Journal of merlyn (47)

Tuesday January 10, 2006
03:20 AM

The 90's called... they want their code back...

[ #28298 ]
From a recent comp.infosystems.www.authoring.cgi post:

sub ParseForm
    {
    my ($key, $prefs, $buffer);
    if ($ENV{'REQUEST_METHOD'} eq 'GET')
        { @pairs = split(/&/, $ENV{'QUERY_STRING'}) }
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST')
        {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        @pairs = split(/&/, $buffer)
        }
    else {Error('Bad or Unknown Request Method',
                "The form's request method must be either 'POST' or
'GET'. Please check your HTML.")}

    foreach $pair (@pairs)
        {
        local($name, $value) = split(/=/, $pair);
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ s/<!--(.)*?-->//mg;
        $FORM{$name} = $value;
...

And the horror doesn't stop there, but I wanted to minimize your exposure. This is 2006, right?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • That code is pretty close to the form parser that was in the "CGI Programming with Perl" book and I've been using it for a while. *blush* Of course, I do VERY little CGI work and even when I do, it's usually just a quicky. But out of curiosity...what would you recommend instead of a form parser like this. Something in CGI.pm?
  • Use the CGI module:
    use CGI;

    my $cgi = CGI->new();

    my $foo_param = $cgi->param('foo');
    my $bar_param = $cgi->param{'bar');
    Obviously CGI.pm can do a lot more, but that's enough to get you started.