Here's my crappy script!

Here's a script that generates a web form the first time it's called, then calls itself again with the form data as parameters. When it sees it's been called with a valid age parameter, it writes out a web page and informs the user of success.

I don't claim it's good, or elegant, but it works for me. I'm by no means an excellent Perl programmer, but I'm doing OK with just a few weeks of experience. If you use it or it's helps you out, let me know.

#!/usr/local/bin/perl
# Change above line to point to the perl binary on your server

# The CGI on my server doesn't recognize the <div> tag, so I
# added it to the namespace. You can do this with any arbitrary
# tag. 
use CGI qw(:standard *div);

# This is supposed to flush the cache so errors will show up
# in server logs. Useful for debugging.
$|=1;

# If I don't declare variables, the server complains as if 'use strict'
# was in force. 
my $namey = param("name");
my $agey = param("age");

# Variable points to the location of this script on the server so it
# can call itself.
my $mee = "/cgi/monkeymagic";

if ($agey != "0") {		# If we have a command line paramater
				# for age, make a page
		&pagit;
	} else {
		&formit;	# Otherwise send the form
	}


# This subroutine generates the form
sub formit {
print header,"\n", start_html('Testing is cool!');
print h1('This is a form!');
print p("Why not enter stuff and see what it does?");

print <<END_block;
	<form action="$mee?name+?age">
	<p>What is your name?<input name="name" value="Bob"></p>
	<p>What is your age?<input name="age" value="0"></p>
	<input type="submit">
	</form>
END_block

print end_html;
}


# This subroutine writes the web page with submitted data
# then sends back a web page to say everything is fine
sub pagit {
my $outy = "../test.html";  # Location of output file.
                            # Make sure it's writable by perl.
                            # If the system runs CGI scripts as user
                            # "nobody," you may have to create the 
                            # file beforehand and give it global
                            # write permission.

open OUTS, ">$outy" or die "Can't open $outy to write: $!";

print OUTS <<END_Text;
<html>
	<head>
	<title>A custom page, just for you!</title>
	<link rel="stylesheet" type="text/css" href="../monkeystyle.css">
    	</head>
	<body>
END_Text

# Just use a <div> to pretty things up a bit
print OUTS div({-id=>"hed"}, h1('This is a web page!'));
print OUTS "<p>This page was generated dynamically just for you. ";
print OUTS "Try again and see that it makes a new page every time. ";
print OUTS "(It might be necessary to refresh your browser if you're behind a proxy)</p>";
print OUTS p("Your name is $namey");
print OUTS p("Your age is $agey"); 
print OUTS "<p>Wasn't that cool? You can try it again if you want.";
print OUTS "Go <a href=\"$mee\">here</a> to try again.</p>";
print OUTS end_html;

# Not necessary, but nice anyway
close OUTS or die "Can't close file: $!";

# This part just sends back a page to the browser indicating
# success. Otherwise you get an error and don't know if the 
# script finished correctly.
print header, start_html('All done'), h1('Magic complete!');
print "<p>Results can be found <a href=\"$outy\">here</a></p>";
print end_html;
}