[thelist] Perl problem and question

Poojie poojie at dccnet.com
Sun Aug 5 00:00:48 CDT 2001


CDitty,

I can understand your complaints about Perl, because it takes more
effort to learn in the beginning because it is a lower level
language than PHP (although in the grand-scheme of things, it is a
pretty high level language). The fact that Perl doesn't have all
the "easy-to-use" built-in functions is actually part of  why it is
such a cool language -- there are so many different ways of solving
a particular problem; and when writing your own functions you know
_exactly_ what it is doing, how it is doing it, and more
importantly how efficient the function is. Anyhoo, enough about
Perl being cool.

Since your date problem is solved, I'll take a swing at your form
variables appearing nullified.

Unlike PHP, you have to manually parse the HTTP header with a
function like this:

sub parse_request() {
# --------------------------------------------------------
# Parses form input. Returns hash reference of name value pairs
#
    my (@input, $req);
    my ($temp, $pair, $name, $value);

    if ($ENV{'REQUEST_METHOD'} eq 'GET')     {
        @input = split(/&/, $ENV{'QUERY_STRING'});
    } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read(STDIN, $temp, $ENV{'CONTENT_LENGTH'});
        @input = split(/&/, $temp);
    }

    foreach $pair (@input) {
       ($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;
        exists $req->{$name} ?
            ($req->{$name} .= ",$value") :
            ($req->{$name}  = $value);
    }

    return $req;
}

This function will parse the HTTP header and return a hash
reference containing your form variables. Call the function like
this:

my $req = parse_request();

And you will be able to access your data like this:

if ($req->{'email'} == '') { ... }


The parse_request() function looks pretty nasty at first, but
eventually it will make sense -- at least that's how it was for me.

Daryl






More information about the thelist mailing list