How to Use a Form in a Perl CGI Web Page

Creating a Simple Perl User Interface with the CGI Module

© Mark Alexander Bain

Jul 19, 2009
How to Use a Form in a Perl CGI Web Page, Mark Alexander Bain
Nothing could be simpler than adding a form to a Perl CGI web page, and if a programmer uses the Perl CGI module then they can easily process the information it contains

Once a Perl programmer has access to a web server (such as Apache) then they'll have a Perl based web page up and running in no time at all (as discussed in How to Use Perl Programming on a Web Site). They will be able to create a web page that runs Perl code on the server and which returns a HTML output to the users' browsers.

However, that's a one way process - from the server to the user. The next step is, of course, to make this a two way process and enable the user to send information to the server as well as receiving information from it.

The obvious answer is for the perl Programmer to:

  • create a web page with a form
  • create a Perl script that will process the inputs from the form
  • return the results to the application user

Or, the programmer can use the Perl CGI module.

Using the CGI Module

It is quite feasible for the Perl programmer to use the inputs to a web page by processing the information sent by the POST and GET methods. However, there is no need for them to do that. Instead they just need to make use of one of the modules that comes with their Perl installation. That module is the Perl CGI module and is loaded by adding the following code to a script:

use CGI qw/:standard/;

They will now be able to use the param method which can access any GET or POST variable by name. For example:

#!c:/strawberry/perl/bin/perl
print "Content-type: text/html\n\n";
print "<h1>" . param ('greeting') . "</h2>";

Here's the script:

  • includes the shebang line that points the server to the location of the perl executable
  • prints the HTTP header line so that any outputs will be handled correctly by the web browser
  • displays the contents of the "greeting" input as a heading

If this file is saved into the server's CGI directory ad "helloworld.pl" then it can be displayed in a browser by using the URL:

http://localhost/cgi-bin/helloworld.pl?greeting=hello

This will display the required text as shown in Figure 1 at the bottom of this article.

Using Forms with Perl CGI, GET and POST

The technique shown above uses the GET method to send information to the Perl script. However, that is inherently insecure since the variable is passed as part of the web page's URL. It is, therefore, much safer to use a form and the POST method. This is another task made very simple by the Perl CGI module:

if (param ('greeting')) {
print "<h1>" . param('greeting') . "</h2>";
} else {
print start_form,
"Enter a greeting:", textfield ('greeting'), p,
submit ('Send Greeting'),
end_form;
}
Here the code will either:
  • display the contents of the "greeting" variable if it has been passed to the script (using either the POST or GET methods
  • display a form if the variable is not present
It's interesting that no HTML is included in the form creation - that's all supplied by the Perl CGI module methods (start_form, textfield, p, submit and end_form). The end result can be seen in Figure 2. The beauty of use the Perl CGI module is that the programmer needs now very little about HTML structure or how forms pass information, however they are still able to create an interactive web page with only the minimum of time and effort.

The copyright of the article How to Use a Form in a Perl CGI Web Page in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish How to Use a Form in a Perl CGI Web Page in print or online must be granted by the author in writing.


How to Use a Form in a Perl CGI Web Page, Mark Alexander Bain
Figure 1: The Perl CGI Module, Mark Alexander Bain
Figure 2: A Perl HTML Form, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo