next up previous contents
Next: Multiple argument input to Up: CGI Script Input: Accepting Previous: The cgi.pm module

A Minimal Form Response CGI Perl Script

In this subsection we will develop a minimal.pl CGI routine that accepts input form our minimal form and sends back HTML that echoes the input data.

We will use the cgi-lib.pl to

We will need to learn some more basic Perl:

The first thing our Perl script will need to do is to include the Perl library file cgi-lib.pl.

The Perl command require will load in any external Perl file. It is easier and sometimes essential that all library files exist in the same folder or directory as the main Perl script calling the library.

Therefore make sure that all Perl files required for a Perl program do exist at the same folder or directory level.

NOTE: ON Unix the cgi-lib.pl is already installed in a special place and does not need to placed in your cgi-bin directory.

Thus to include our cgi-lib.pl file we need the Perl command:

require "cgi-lib.pl";

Note the format of the require command has the Perl file listed in ``...''.

Having included the library we can call on its many useful subroutines.

The &ReadParse() subroutine reads either GET or POST input and conveniently stores the name/value pairs in a Perl array (We will meet these formally shortly for now we simply use the array).

Thus a Perl call of the form:

&ReadParse(*input);

will store the input in an array input. & is used to indicate a Perl subroutine call.

Next we will need to extract out the relevant value of a given name.

This is relatively simple. Perl is very good a process data of this kind.

In our current example there is only one input field and we are therefore only interested in the value associated with the myfield name.

To get this value you simply do: $input{'myfield'}

Thus to print out the value typed we could do something like:

print "You typed: " . $input{'myfield'} . "\n";

A first minimal Perl script

So pulling together all we have learnt so far. A Perl script to take our minimal form input and return in HTML the value type could be:

#!/usr/local/bin/perl# minimal.cgi
# This is the minimalist form script 
# to demonstrate the use of 
# the cgi-lib.pl library  

require "cgi-lib.pl";

# Read in all the variables set by the form

&ReadParse(*input);

print "Content-Type: text/html\n\n";
print "<html> <head>\n";
print "<title>Minimal Input</title>\n";
print "</head>\n";
print "<body>\n";

print "You typed: " . $input{'myfield'} . "\n";

print "</body> </html>\n";

A second minimal Perl script

We can further than this and exploit some more cgi-lib.pl subroutines.

Nearly every CGI output has:

Fortunately subroutines exist to save us typing this same information all the time.

The &PrintHeader subroutine returns the string:

Content-Type: text/html\n\n

Thus we can use print in conjunction to produce our CGI header output via:

print &PrintHeader;

The &HtmlTop subroutine accepts a single string argument, MY TITLE say, and return an HTML Head and Body (opening only) with the argument as the HTML page TITLE and H1 Header. I.e.

<html> 
<head>
<title>MY TITLE</title>
</head>
<body>
<h1>MY TITLE</h1>

which is rather useful.

The &HtmlBot subroutine is the compliment of &HtmlTop and returns:

</body>
</html>

Thus we can use these functions and we only need to provide the main HTML body ourselves.

Thus we develop a better minimal.pl program as follows

#!/usr/local/bin/perl

# minimal.cgi
# This is the minimalist form script 
# to demonstrate the use of 
# the cgi-lib.pl library  

require "cgi-lib.pl";

# Read in all the variables set by the form

&ReadParse(*input);

# Read in all the variables set by the form
  &ReadParse(*input);

# Print the header + html top
  print &PrintHeader;
  print &HtmlTop ("Minimal Input");
  
print "You typed: " . $input{'myfield'} . "\n";

print &HtmlBot;


next up previous contents
Next: Multiple argument input to Up: CGI Script Input: Accepting Previous: The cgi.pm module
dave@cs.cf.ac.uk