CGI Guess Game

Guess Game

#!/usr/local/bin/perl -- -*-perl-*-
#
$testcgi = "http://www.test.com/cgi/guess.pl";
#
#
#
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@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;
   $value =~ s/<([^>]|\n)*>//g;
   $value =~ s/<//g;
   $value =~ s/>//g;
   $FORM{$name} = $value;
}
#
$number = $FORM{number};
$goes = $FORM{goes};
#
if ($FORM{guess} != 0) {&go} else {&guess1}
#
sub go {   
#
$goes = $goes + 1;
#
#
if ($FORM{guess} == $number) { &guess4}
elsif ($FORM{guess} < $number) { &guess3}
elsif ($FORM{guess} > $number) { &guess2}
    elsif ($FORM{guess} == 0) { &guess1};
    exit;
}
# 
sub guess1 {
    print "Content-type: text/html\n\n";
    print "<html><head><title>Guessing Game</title></head>\n";
    print "<body bgcolor=purple text=white><center>";
    print "<h1>Hello there!</h1></center>\n";
    print "<br>Please enter your first guess:\n";
    print "<br>";
    print "<form method=POST action=\"$testcgi\">\n";
    print "<input type=text name=\"guess\"><p>\n";
    print "<input type=hidden name=\"number\" value=\"$number\"><p>\n";
    print "<input type=hidden name=\"goes\" value=\"$goes\"><p>\n";
    print "<input type=submit value=\"TRY\">\n";
    print "<hr>\n";
    print "</form></body></html>\n";
    exit;
}
#
sub guess2 {  
    print "Content-type: text/html\n\n";
    print "<html><head><title>Guessing Game</title></head>\n";
    print "<body bgcolor=purple text=white><center>";
    print "<h1>Sorry!</h1></center>\n";
    print "<br>That was too high!\n";
    print "<br>Please enter your next guess:\n"; 
    print "<br>";
    print "<form method=POST action=\"$testcgi\">\n";
    print "<input type=text name=\"guess\"><p>\n";
    print "<input type=hidden name=\"number\" value=\"$number\"><p>\n";
    print "<input type=hidden name=\"goes\" value=\"$goes\"><p>\n";
    print "<input type=submit value=\"TRY\">\n";
    print "<hr>\n";
    print "</form></body></html>\n";
    exit;
}
sub guess3 {
    print "Content-type: text/html\n\n";
    print "<html><head><title>Guessing Game</title></head>\n";
    print "<body bgcolor=purple text=white><center>";
    print "<h1>Sorry!</h1></center>\n";
    print "That was too low!\n";
    print "<br>Please enter your next guess:\n";
    print "<br>";
    print "<form method=POST action=\"$testcgi\">\n";
    print "<input type=text name=\"guess\"><p>\n";
    print "<input type=hidden name=\"number\" value=\"$number\"><p>\n";
    print "<input type=hidden name=\"goes\" value=\"$goes\"><p>\n";
    print "<input type=submit value=\"TRY\">\n";
    print "<hr>\n";
    print "</form></body></html>\n";
    exit;
}
sub guess4 {
    print "Content-type: text/html\n\n";
    print "<html><head><title>Guessing Game</title></head>\n";
    print "<body bgcolor=purple text=white link=red vlink=black><center>";
    print "<h1>Well Done!</h1></center>\n";
    print "That was correct\n";
    print "<br>The number was: $number";
    print "<br>You had $goes trys!";
    print "<br>\n";
    print "<br>";    
    print "<a href=\"http://www.test.com/guess.html\">Click here</a> to play again!\n";
    print "<hr>\n";
    print "</body></html>\n";
    exit;
}