Every CGI routine must first parse the incoming form data, so I saved that set of commands as a subroutine:
# perl0002.lib
# Written: 1999-06-03 James Alarie
# Base from "Perl and CGI for the world wide web" by Elizabeth Castro.
# I changed it to add localization of variables,
# retrieve QUERY_STRING information for POST,
# output into @formdata as well as %formdata,
# return values for fields named by $ENV or date_time variables,
# not separate multi-values of matching-name fields,
# break on either "&" or ";".
#
# FormParse Subroutine:
# call with:
# $_[0]: (add values as needed)
# 0 no expansion
# 1 expand %ENV variables
# 2 expand %date_time variables
# returns:
# %formdata, @formdata
sub form_parse{
local($buffer, @cookies, $countw, $countg);
local($pair, $key, $value, @pairs);
@formdata=(); %formdata=(); @pairs=();
if ($ENV{'REQUEST_METHOD'} eq 'GET') { # get from URL
@pairs = split(/[&;]/, $ENV{'QUERY_STRING'}); # split on "&" and ";"
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); # read STDIN
if ($ENV{'QUERY_STRING'} ne "") { # something on URL
$buffer = $ENV{'QUERY_STRING'}."&".$buffer; # ...use as prefix
}
@pairs = split(/[&;]/, $buffer); # split on "&" and ";"
} else {
print "Content-type: text/html\n\n"; # error!
print "<p>You must use Post or Get</p>";
}
foreach $pair (@pairs) { # one at a time
($key, $value) = split (/=/, $pair); # split on "="
$key =~ tr/+/ /; # trans "+" to space
$key =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg; # Hex to ASCII
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9]{2})/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g; # no server side includes
$exp = $_[0];
$exp2 = $exp % 2; # rightmost bit
if ($value eq '') {
if ($exp2 == 1) { $value = $ENV{$key}; }
}
$exp = int($exp / 2); # strip rightmost bit
$exp2 = $exp % 2;
if ($value eq '') {
if ($exp2 == 1) { $value = $date_time{$key}; }
}
push(@formdata, $key, $value);
if ($formdata{$key}) { # already known key
$formdata{$key} .= "$value"; # add another value
} else {
$formdata{$key} = $value;
}
}
}
#***** Accessed correctly:
1;