Before we get into the code layout part let's take a look at the elements:

Perl is a scripting language designed to automate tasks and processes, in a powerful and flexible manner.

* Available on all platforms
* Design without limits philosophy so it scales indefinitely
* Easy to use and learn
* Ideal for all text manipulation and automation
* Extensive Modules for most common task

We where to start, I would recommend "Learning Perl" or "Learning perl on Win32" then get yourself over to www.perldoc.com

So where the CGI bit, the CGI (Common Graphic Interface) is a module available for perl which make the process of creating a web interface and handling web forms easy.

When coding in CGI Perl here's a few points to use your code layout to address the following common issues:

* Avoid "500 Internal Server Error"
* Easily control of multiple forms and actions from single script
* Error message handling

Here's a simple example that takes input and prints it in a different colour:

see it in action here

#!/usr/local/bin/perl

use CGI qw(:standard); # Call the CGI module 

use strict; # All variables must be defined security measure for all Web CGI's

# Define your variables here
# my $var = "some var";
# This uses the CGI module to get the variable input from the submitted form
my $input = param("input"); 

#
# This next bit can be used for script navigation the "if (param ("go_green"))" command
# looks for the variable "go_green" submitted in the form and runs sub change_green
#

# Call a subroutine "pageheader" this ensures any error will appear on the browser
pageheader (); 

if (param ("go_green")) { change_green ();
} elsif (param ("go_red")) { change_red ();
} else { inputform () }

footer (); # Call a subroutine "footer"

sub pageheader {
print header(), start_html("Code Layout Example"), h1 ("Page Title - Change Colour");
} # End of header

sub footer {
print <<END_OF_FOOTER; # Continue printing until we get this text on a new line
<p><font size=2>Sample Form Script Version 1.0 - 26 Jan 2003
<br>Copyright (c) 2003</font>
END_OF_FOOTER
} # End of footer

sub change_green {

# errorchecking ensure the is not using special characters 
# this is a recommended security feature
#
# The regex breaks as follows:
# ^ - from the begin of the string
# [0-9a-zA-Z ] - check the char matches one of these listed
# + - repeat the last for multiple entries
# $ until the end of the string

error ("Please don't enter non-alphanumeric characters") 
 unless $input =~ /^[0-9a-zA-Z ]+$/;

print "<p><font color=#008000>$input</font></p>";

} # end of sub change_green

sub change_red {
error ("Please don't enter non-alphanumeric characters") 
 unless $input =~ /^[0-9a-zA-Z ]+$/;
print "<p><font color=#FF0000>$input</font></p>";
} # end of sub change_green

sub inputform {
# Use the CGI commands strat_form, textfield, submit and end_form
#  to generate the html code

print start_form();
print "<p>Enter text: ", textfield ("input");
print " ", submit("go_green", "Go Green"), " ", submit("go_red", "Go Red");
print end_form();
} # end of inputform

sub error { # Error sub routine prints the error and closes the program
print h3 ("Error");
print <<END_OF_ERROR;
<hr><b>@_</b> 
<p>Please use <b>back arrow</b> to go back and correct input.";
END_OF_ERROR
&footer; # Call the footer
exit; # Stop the program 
} # end of error

see it in action here

This basic code layout can be expanded to create complex CGI tools here an example of one of the first ones I wrote golf.onwire.co.uk

Home --> Web --> Code Layout

Code Layout