#!/perl/bin/perl

# quiz.pl
#
# A little Perl script to read, decode and print the names
# and values passed to it from an HTML form through CGI.

# Get HTML header, ender, define the page title.

     require "/pub/scripts/perl-cgi/html.pl";  # Full path.
     $Title = "Your Personal Preferences";
     $MaxData = 6;

# Assign names to indexes to make them more descriptive.

     ($Sex, $HowOld, $FaveFilm, $OldStones, $LastName, $FirstName) =
          (0 .. 5);

# Set up a flag for the Stones checkbox.

     $OldStones = 1;
	
# Set up a series of arrays to handle the form data.

     @Gender = ("male", "female", "neither");
     @Age = ("12-18", "19-25", "26-30", "older than 30", "real old");
     @Film =
          (
          "Citizen Kane",
          "Benji",
          "Dawn of the Dead",
          "It's a Wonderful Life",
          "Intolerance",
          "none that we mentioned"
          );
	 	
# Get the length of the data.

     $DataLen = $ENV{'CONTENT_LENGTH'};

# Read the data from standard input.

     read (STDIN, $QueryString, $DataLen);

# Use split to make an array of name-value pairs broken at
# the ampersand character.  Then get the values.

     @NameValuePairs = split (/&/, $QueryString);
     $n = 0;
     foreach $NameValue (@NameValuePairs)
          {
          ($Name, $Value[$n]) = split (/=/, $NameValue);
          $n++;
          }

# Set or clear $OldStones flag based on the number of pairs
# counted in $n.  The checkbox value won't be sent if it's
# not checked.  Adjust indexes, too.

     if ($n != $MaxData)
          {
          $OldStones = 0;
          $FirstName--;
          $LastName--;
          }

# Put up an HTML header, page title and a rule.
	
     &HTML_Header ($Title);
     print "<BODY>\n";
     print "<H1>$Title</H1>\n";
     print "<HR>\n";

# Print the information in a friendly manner.

     print "<H3>Your name is ";

# See if anything has been entered in the name.

     if (($Value[$LastName] eq "") && ($Value[$FirstName] eq ""))
          {
          print "a mystery to us!\n";
          }
     else
          {
          print "$Value[$FirstName] $Value[$LastName]\n";
          }

     print "</H3>\n";
     print "<H3>You are a $Gender[$Value[$Sex]]</H3>\n";
     print "<H3>Your age is $Age[$Value[$HowOld]]</H3>\n";
     print "<H3>Your favorite film is ";
     print "\"$Film[$Value[$FaveFilm]]\"</H3>\n";
     print "<H3>You";
	
     if (!$OldStones)
          {
          print " don't";
          }

     print " think the Rolling Stones are too old</H3>\n";


# End the HTML document.

     &HTML_Ender;

#                         End quiz.pl
