#!/usr/local/bin/perl
#
# This script was originally written by Michael Toy, and modified
# by Hagan Heller in March, 1995 then hacked by Doug Oct 1996.
#

print "Content-type: text/html\n\n";
print "<TITLE>Form Echo</TITLE>";
print "<H1>Form Echo</H1>";

#
# This reads in the information sent when the user pressed Submit
#
if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }

#
# Now, using a little loop, we'll split up the data into name/value
# pairs, which makes them easier to work with. We'll also print out
# each pair as it is created.
#
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    print "<B>$name</B> = [$value]<BR>\n";

    $FORM{$name} = $value;
}

# As an extra bonus, here's how you get environment variables from
# someone submitting a script.

print "<HR>";
print "<H2>Environment Variables</H2>\n";

foreach $item (keys %ENV)
{
    print "<B>$item</B> = " . $ENV{"$item"} . "<BR>\n";
};
