A Tutorial for CGI::Application

A contact form using HTML::Template

Most Web developers find themselves creating some of the same functionality site after site. The repetition can become tedious—lots of typing, cutting and pasting, and testing—with the same errors popping up.

CGI::Application (C::A), and it's many Plug-in's, was developed to eliminate some of the tedium, helping the developer to more efficiently use their time by solving problems and structuring their applications.

CGI::Application falls under the category of a framework. Frameworks enable developers to use shorter, more readable coding conventions, at least on the surface, that can be performing more complicated, and possibly combined tasks behind it. The result is faster development, more consistent style, and fewer errors, to name but a few. C::A provides an addition model of mapping your Web application to "screens" (forms, etc.) and creating the functions to process by pages.

There are more robust frameworks (with more robust learning curves), like Catalyst and Maypole, and more recently, Jifty, but CGI::Application provides a very approachable, easy-to-use, low overhead framework for Web developers who like working in a less abstracted environment and a little closer to Perl itself.

This tutorial does not cover the installation of CGI::Application, or any of it's growing number of plug-ins, but will show how you can use CGI::Application and HTML::Template to build one of the most common ancillary interactive applications on the Web: a contact form. We will start out with an example that is pure CGI::Application, just to show that it can be done, though not really practical.

Also, this tutorial is not meant as a placement for C::A's well-written POD, which should be required reading.

A Word About HTML::Template

While this is not a tutorial for HTML::Template (H::T) or HTML::FillInForm, the novice Web developer will see an example of how they can all work together with CGI::Application to create more useful, dynamic, and user-friendly applications.

Basic C::A Concepts

All C::A applications are invoked by an instance script, which, in test case below, is named index.cgi. One of the advantages of using C::A, is that it encourages "smart" URI's, e.g., http://www.acmecorp.com/contact/ The instance script can be named anything, but this author prefers some__directory/index.cgi to take advantage of the smart URI, and using the .cgi extension, reserving .pl) and .pm for the actual modules.

A instance usually passes one run mode, which can be equated to the processing of one screen or Web page. For example, the first run mode might populate and display a form, and the second run mode might validate that form upon submission. If a runmode is not passed, a default run mode can be set at the top of the actual application (more on that in a minute).

After the function is run in the application, the application must return something or to something: usually an HTML page through a redirect, or in our case, an H::T template using output.

$self, or the object, is passed throughout the application and finally returned at the end. It can be called anything, but $self is common practice in the OO world.

The Layout

The locations of your files will vary depending on your server, e.g., you could be on a shared host and using something like '/usr/home/foobar' and /foobar/public_html/. The salient point is that the C::A applications (server-side) are placed out-of-reach of the public Web directory (client-side).

Sample Application

CGI::Application, Plugin's, and HTML::Template

index.cgi (our instance script)

Notes:

this instance script will pass a PARAM to the application

in this example, we need to output the form that now in a .tmpl file. Though some browsers will actually display the .tmpl file without H::T, we'll use an instance of our application to simply display the form.

the instance script might be evoked by a text link: <a href="http://www.acmecorp.com/contact/">Contact Us</a>

note that paths are relative to the location of the .tmpl file and not the instance script or application.

in the method 'display', the C::A convention for specifying a template is used: $self->load_tmpl

in the first firing of the instance script, no run mode will be passed, so the application will use the default function defined by the $self->start_mod('d') in the setup method. This will simply output, or display, the template file containing our form.

we also use the C::A::Plugin::FillInForm convention for outputting the form with the fill-in-form values: $self->fill_form

When putting a smiley right before a closing parenthesis, do you:

Use two parentheses: (Like this: :) )
Use one parenthesis: (Like this: :)
Reverse direction of the smiley: (Like this: (: )
Use angle/square brackets instead of parentheses
Use C-style commenting to set the smiley off from the closing parenthesis
Make the smiley a dunce: (:>
I disapprove of emoticons
Other