#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $message = $q->param('message'); # retrieve state $n1 = $q->param('n1'); $n2 = $q->param('n2'); $good = $q->param('good'); $total = $q->param('total'); if ($message) { # is state not empty? $answer = $q->param('answer'); # read user input $feedback = "The answer was wrong."; if ($answer == $n1 + $n2) { $good += 1; $feedback = "The answer was right."; } $total += 1; $n1 = int(rand(100))-50; $n2 = int(rand(100))-50; $question = "What is $n1 + $n2?"; $message = "$feedback ($good/$total) $question"; } else { # new user (state is empty: initialize state) $n1 = int(rand(100))-50; $n2 = int(rand(100))-50; $good = 0; $total = 0; $question = "What is $n1 + $n2?"; $message = "Welcome to the game! ($good/$total) $question"; } # report state (message), store state (hidden fields), get ready for more input print qq{
$message

Type your answer here:

When ready press

}; print $q->end_html;