#!/usr/bin/perl

use CGI; 

$q = new CGI; 

# retrieve state 

$message = $q->param('message'); 

$one = $q->param('one'); 
$two = $q->param('two'); 
# check state 
if ($message) { # existing state 
   $sum = $one + $two; 
   $one = $two; 
   $two = $sum; 
   $message = "The next Fibonacci number is: " . $two; 
} else { # new player 
   $one = 0; 
   $two = 1; 
   $message = "How are you? First Fibonacci number is: " . $two; 
} 

print $q->header, qq{
<html><head><title>Fibonacci</title></head><body bgcolor="white">
  <form action="$ENV{SCRIPT_NAME}" method="GET">
    $message <p>
    When ready press <input type="submit" value="Proceed">
    <input type="hidden" name="one" value="$one">
    <input type="hidden" name="two" value="$two">
    <input type="hidden" name="message" value="$message">
  </form>
</body></html>
};