Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

bradcathey's scratchpad

by bradcathey (Prior)
on Jun 02, 2004 at 02:37 UTC ( #359060=scratchpad: print w/ replies, xml ) Need Help??

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.



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

  1. 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., 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.
  3. 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.

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).

/opt/foobar/apps/---+ | | | | | | | | | | | /Acmecorp/---+ | | | | | | acmecorp.conf | | /var/www/acmecorp/--+ | home.html | /contact/----+ | | | index.cgi | /templates/---+ | | | contact.tmpl | thankyou.html

Sample Application

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

index.cgi (our instance script)


  1. this instance script will pass a PARAM to the application
  2. 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.
#!/usr/local/bin/perl -T use lib "/opt/foobar/apps/"; use warnings; use strict; use Acmecorp::Contact; my $app = Acmecorp::Contact->new( PARAM => 'client' ); $app->run();

contact.tmpl ('rm' is our runmode and will be passed upon submission to our instance script '/contact/index.cgi')


Foobar_Super (a super class for C::A applications)


  1. because you may be creating several C::A applications for other sites on your server, developing a super class eliminate repetition
  2. C::A Plugin's integrates the modules they are associated with, negating the necessity of loading the native CPAN module

acmecorp.conf (a configuration file)

Note: read by CA_Super's cgiapp_init



  1. in the method 'display', the C::A convention for specifying a template is used: $self->load_tmpl
  3. we also use the C::A::Plugin::FillInForm convention for outputting the form with the fill-in-form values: $self->fill_form


This tutorial has shown use of the basis tenents of using C::A as an application framework:

  1. file layout and directory structure
  2. instance scripts
  3. run modes
  4. cgiapp_init and redirect
  5. integration of C::A::Plugin's
  6. use of super class
  7. intro to the H::T templating system

As always, you are encouraged to read the POD for C::A and then take a look at the growing number of Plugins to see if C::A can further streamline your coding process.

Other Resources

CGI::Application Wiki
Mailing List
HTML::Template Tutorial

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-12-25 10:33 GMT
Find Nodes?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?

    Results (160 votes), past polls