Second Summer 2009


State machines (client-side state)
Here's the basic template for a Perl CGI script (using CGI.pm):

#!/usr/bin/perl

use CGI;

$input = new CGI;

print $input->header, $input->start_html;

$stateVariableOne = $input->param("stateVariableOne");
$stateVariableTwo = $input->param("stateVariableTwo");
...
$stateVariableEnn = $input->param("stateVariableEnn");

$inputVariableOne = $input->param("inputVariableOne");
$inputVariableTwo = $input->param("inputVariableTwo");
...
$inputVariableEnn = $input->param("inputVariableOEnn");

...
# code working on these variables and producing output variables (not mentioned in our template but appearing in the interface below) 
...

# report state, save state, get ready for more input 
print qq{<form>

<input type="hidden" name="stateVariableOne" value="$stateVariableOne">
<input type="hidden" name="stateVariableTwo" value="$stateVariableTwo">
...
<input type="hidden" name="stateVariableEnn" value="$stateVariableEnn">

Currently the state is: $stateVariableOne, $stateVariableTwo, ..., $stateVariableEnn 

... <input type="text" name="inputVariableOne">
... <input type="text" name="inputVariableTwo">
...
... <input type="text" name="inputVariableEnn">
...
<input type="submit" name="submit" value="Proceed">
<input type="submit" name="reset" value="Reset">
...

</form>
};

print $input->end_html;
Here's the exact same template in Python:

In class we will use these templates on various problems.

#!/usr/bin/python

import cgi

input = cgi.FieldStorage()

print "Content-type: text/html\n\n<html><head><title>...</title></head><body>"

(stateVariableOne, stateVariableTwo, ..., stateVariableEnn, inputVariableOne, inputVariableTwo, ..., inputVariableEnn) = ("", "", ..., "")

if input.has_key("stateVariableOne"): stateVariableOne = input["stateVariableOne"].value
if input.has_key("stateVariableTwo"): stateVariableTwo = input["stateVariableTwo"].value
...
if input.has_key("stateVariableEnn"): stateVariableEnn = input["stateVariableEnn"].value

if input.has_key("inputVariableOne"): inputVariableOne = input["inputVariableOne"].value
if input.has_key("inputVariableTwo"): inputVariableTwo = input["inputVariableTwo"].value
...
if input.has_key("inputVariableEnn"): inputVariableEnn = input["inputVariableOEnn"].value

...
# code working on these variables and producing output variables (not mentioned in our template but appearing in the interface below) 
...

# report state, save state, get ready for new input ...

print """
<form>

  Currently the state is: %s %s ... %s

  <input type="hidden" name="stateVariableOne" value="%s">
  <input type="hidden" name="stateVariableTwo" value="%s">
  ...
  <input type="hidden" name="stateVariableEnn" value="%s">

  ...
  ... <input type="text" name="inputVariableOne">
  ... <input type="text" name="inputVariableTwo">
  ...
  ... <input type="text" name="inputVariableEnn">
  ...
  <input type="submit" name="submit" value="Proceed">
  <input type="submit" name="reset" value="Reset">
  ...


</form>
""" % (stateVariableOne, stateVariableTwo, ..., stateVariableEnn, stateVariableOne, stateVariableTwo, ..., stateVariableEnn, inputVariableOne, inputVariableTwo, ..., inputVariableEnn)

print "<body></html>"
We'll implement a true Hangman game.


Updated by Adrian German for A202/A598