#!/usr/bin/perl -Tw

#++
# Based on <URL:http://www.wiley.com/legacy/compbooks/stein/text/advice.txt>
#
# Source code to accompany 
#   _Official Guide to Programming with CGI.pm: The Standard for Building Web Scripts_
#   by Lincoln Stein
#   (c) 1998 by Lincoln Stein
#  Published by John Wiley & Sons, Inc.
#--

use CGI qw(:standard);
use strict;

  # initialize variables
   my @advice = (                                       # hardcoded array
	   'A stitch in time saves nine.',
	   'Look both ways before crossing the street.',
	   'Chew completely before swallowing.',
	   'A penny saved is a penny earned.',
	   'Fools rush in where angels fear to tread.'
           );

   my $action = param('action');                        # field from HTML form


  # generate HTML
    print header,                           # note the comma (,) 
          start_html("Good Advice"),        #
          h1("Good Advice");                # using CGI.pm to create HTML

  # truly dynamic generation
  #  output changes depending on input
    if ($action=~/Message (\d+)/) {         # untainting data 
        my $message_no = $1 - 1;            # arrays begin at 0
        print strong($advice[$message_no]); 
    } 

  # this part is always present
    print start_form;
    foreach (1..5) {
        print submit(-name=>'action',-value=>"Message $_ ");
    }

  print end_form,end_html;
## End of example ##
