This is one sub routine I use in forms to turn the data inside the form boxes into usable data. The names in the form will be converted to "$cgi{name}", so a form field with name "State" will become "$cgi{State}". Just put the sub into your Perl script and call it like this:

parseit()
or
&parseit

For perl generated html pages, I also find it handy to insert the names in the value field, ie:

name="State" value="$cgi{State}"

This lets you do error checking for missing fields and redraw the same page and it will remember what the person had already put in the fields, so they don't have to retype it.


Here is the actual sub routine, which is pretty basic. You can also add an else / elsif to the sub if you want to test for any input at all and redirect if not.
sub parseit {
 if (defined($ENV{CONTENT_LENGTH}) && $ENV{CONTENT_LENGTH} != 0) {
  read(STDIN, $content, $ENV{CONTENT_LENGTH});
   foreach $pairset (split(/\&/, $content)) {
    ($key, $val) = split(/=/, $pairset);
    $val =~ tr/+/ /;
    $val =~ s/%(..)/pack("c", hex($1))/eg;
    $cgi{$key} = $val;
   }
 }
} #end sub

Stuff for google to index: Humans, feel free to ignore.

How do I get input from a form?

How do I use cgi forms?

Get the data from forms?