Perlaccess : Tutorials : Examples
Examples: Form Parsing (7.2)
by Brett Roberts
http://www.bigresources.com

Form Parsing
Most perl scripts likely perform some sort of form parsing in it. Below I will explain the different elements that go into parsing data from form input:

# Replace this line with your path to perl
#!/usr/local/bin/perl

# This line sets the method that this script was accessed (GET or POST)
my $method = $ENV{'REQUEST_METHOD'};

# The following is a basic if else structure to use the appropriate
# processing for grabbing the form data
if ($method eq "GET") {
    $rawdata = $ENV{'QUERY_STRING'};
} else {
   # if not "GET" then it will default "POST"
   read(STDIN, $rawdata, $ENV{'CONTENT_LENGTH'});
}

# Splits the data pairs into sections
my @value_pairs = split (/&/,$rawdata);
my %formdata = ();

# Cycles through each pair to grab the values of the fields
foreach $pair (@value_pairs) {
    ($field, $value) = split (/=/,$pair);
    $value =~ tr/+/ /;
    $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
    $formdata{$field} = $value;
    # store the field data in the results hash
}

# next line sets the MIME type for the output
print "Content-type: text/html\n\n";

# cycle through the results and print each field/value
foreach $field (sort keys(%formdata)) {
   print "$field has value $formdata{$field}\n";
}