Perl CGI  «Prev 

Read the Query String so that we have Access to the State Information

The first order of business is to read the query string so that we have access to the state information. The getquery() routine from earlier in this module is ideal for the task, since it can handle either GET or POST query data.

# get the query vars, if any
%query = getquery();

# if there's no data, assume this is the first iteration
$state = 'first' unless %query;

For the case where there is no query string, we assume that this is the first iteration of the program, and set the state to "first".
Next, create a small loop to make all the query data available globally:
while(($qname, $qvalue) = each %query) {
  # convert any HTML entities
  $qvalue =~ s/</<\;/g;
  $qvalue =~ s/>/>\;/g;
  $qvalue =~ s/"/"\;/g;
  $$qname = $qvalue;
  }

This loops through the %query hash and sets up a variable for each name/value pair. In the process, this is a good time to get rid of any mischievous HTML that users may try to put in the guestbook. It is convenient to do it at this time because we are processing all the input variables anyway.
Next, we jump to the correct routine for the current state:

# the main jump table
if    ($state eq 'first'   ) { first()    }
elsif ($state eq 'create'  ) { edit()     }
elsif ($state eq 'view'    ) { view()     }
elsif ($state eq 'validate') { validate() }
elsif ($state eq 'edit'    ) { edit()     }
elsif ($state eq 'save'    ) { save()     }
else                         { unknown()  }

exit;

This is a pretty straightforward jump table. There are more-complex, and perhaps more-elegant ways to do this, but none that I have seen that are as clear to read.