#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $message = $q->param('message'); $n1 = $q->param('n1'); $n2 = $q->param('n2'); $correct = $q->param('correct'); $total = $q->param('total'); $table = $q->param('table'); # all of the above are state $answer = $q->param('answer'); # input if ($message) { if ($answer == $n1 + $n2) { $message = "$n1 + $n2 is INDEED equal to $answer "; $correct += 1; } else { $message = "$n1 + $n2 is NOT equal to $answer "; } $total += 1; $message .= "(score now $correct/$total) "; # store in the table # table has the format: sdjkfgsd:sfgesdf:agdsdjhfd:kjsdagfsd if ($table) { $table .= ":" . $message; } else { $table = $message; } $n1 = int(rand(100)) - 50; $n2 = int(rand(100)) - 50; # $question is an output variable not part of the state $question = "What is $n1 + $n2? Answer: "; } else { # initialize state $correct = 0; $total = 0; $message = "Welcome the game starts at $correct out of $total "; $table = ""; $n1 = int(rand(100)) - 50; $n2 = int(rand(100)) - 50; # $question is an output variable not part of the state $question = "What is $n1 + $n2? Answer: "; } print qq{
$message

$question

Press to move on.

}; print ""; @lines = split(/:/, $table); foreach $line (@lines) { ($a, $b) = split(/ \(/, $line); print "
$a ($b"; } print "
"; print $q->end_html;