Red Antigua Logo
Yet another piece of web.
Search this site (by Google)
Tools    (top)
Your IP
Check a site for broken links
(W3C)

Perl modules    (top)
Tree::Numbered::Tools
(CPAN)
Perl tutorials    (top)
Perl modules
HTML::Template
CGI::Application
Cookies with CGI::Application
Upload files with CGI::Application
Download files with CGI::Application
Redirect with CGI::Application
CPAN shell
Install DBD::mysql from the CPAN shell
Perl trim function
Validate an IP with Perl
Run suid Perl scripts under Apache
Perl taint mode
Perl date functions with Date::Calc

In Spanish
Curso de Perl

C tutorials    (top)
C - Introduction
C - Absolute beginner's Emacs
C - Examples for beginners
C - Makefile examples
C - Autotools examples
Server configurations    (top)
DNS
Apache
Apache Authentication and Access Control
mod_perl on FreeBSD
MySQL
MySQL add account
phpMyAdmin
Squid
DHCP

UNIX on Windows    (top)
MSYS2 - UNIX environment for MS Windows 32/64 bits
Apache setup on Windows
MySQL setup on Windows
PHP setup on Windows
Perl setup on Windows
Emacs setup on Windows
PuTTY
WinSCP
GIMP on Windows
MinGW - gcc on Windows
MSYS - UNIX-styled shell on Windows
msysDTK - autotools on Windows
GDB for MinGW on Windows

Misc. FreeBSD/UNIX    (top)
'portupgrade' on FreeBSD
'ipf' on FreeBSD
'pf' on FreeBSD
'su' on FreeBSD
Mount an ISO image under FreeBSD
Load the correct sound driver under FreeBSD without knowing what sound card you are using
Simultaneous sound channels on FreeBSD
FreeBSD network stuff
DOS-to-UNIX file conversion
favicon.ico on UNIX
Emacs tips
Command Line Calculator
Save multimedia streams with 'mplayer'
xargs - solution to 'Argument list too long'
Process multiple images from the command line using 'ImageMagick'
Turn the system bell off under X Windows
Process each line in an input file from the command line (or in a shell script)
How to keep a program running in the background using 'nohup'
How to remove symbolic links in the current directory using 'find' and 'rm'
How to remove Emacs backup files in the current directory and all subdirectories using 'find' and 'rm'
How to execute .profile without logging in
Configure X to handle non-English characters
How to move /var to /usr/var

Redirect a web page    (top)
Redirect to another web page
Apache redirect
C redirect
Perl redirect
PHP redirect
HTML redirect
JavaScript redirect

Javascript    (top)
Trim function
Login form
Register form
Popup window

CGI::Application Tutorial
1. "Hello, World!", using CGI::Application and HTML::Template
IndexPreviousNext
CGI::Application was developed with HTML::Template in mind, so it's natural to use them together.
I assume that you already read the HTML::Template Tutorial.

An example such as 'Hello, World!' will not show much of the real benefit using CGI::Application. This module was developed to simplify the programming of web application with many 'screens'.
Anyway, I will give an example just to show where to put the different pieces of code.

We can use the same template as in the HTML::Template tutorial, helloworld.tmpl.html:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title><TMPL_VAR NAME=THAT_FAMOUS_STRING></title>
</head>
<body>
<b><TMPL_VAR NAME=THAT_FAMOUS_STRING></b>
</body>
</html>
Then we create the module HelloWorldCgiApp.pm where we put all the code (thanks to Cory Trese for valuable comments!):
# HelloWordCgiApp.pm
package HelloWorldCgiApp;
use lib qw(/home/redanti/perl);
use base 'CGI::Application';
use HTML::Template;
use strict;

# Run at startup
sub setup {
    my $self = shift;
    $self->start_mode('mode1');
    $self->run_modes(
		     'mode1' => 'helloworld_cgi_app',
		     );
    # Use current path as template path,
    # i.e. the template is in the same directory as this script
    $self->tmpl_path('./');
}
# HTML::Template-style "Hello, World!"
sub helloworld_cgi_app {
    # The application object
    my $self = shift;
    # The message
    my $that_famous_string = 'Hello, world!';
    # Open the html template
    # NOTE 1: load_tmpl() is a CGI::Application method, not a HTML::Template method.
    # NOTE 2: Setting the 'cache' flag caches the template if used with mod_perl. 
    my $template = $self->load_tmpl(	    
	                      'helloworld.tmpl.html',
			      cache => 1,
			     );	
    # Fill in some parameters
    $template->param(
		     THAT_FAMOUS_STRING => $that_famous_string,
		     );
    # Parse the template
    my $html_output = $template->output;
    return $html_output;
}
1; # so the 'require' or 'use' succeeds
Finally we need a 'wrapper', helloworld.cgi-app.pl, which creates an object of our application and runs it:

#!/usr/bin/perl -wT
##
##  CGI Application Hello World
##
use strict;
use lib qw(.);
use HelloWorldCgiApp;
my $helloworld = HelloWorldCgiApp->new();
$helloworld->run();

The output should look like this.
Not very exiting.
But we already know from the HTML::Template tutorial that we may change the look without changing the code.
This example only uses one 'screen', called 'run mode' in CGI::Application terms.
What if we want to extend our script to various 'screens'?
We just define more 'run modes' in our module, and we probably want a template for each 'run mode' (even if it's not mandatory).
The next example shows how to use 'run modes', and how to pass parameters between them.
IndexPreviousNext
Last modified: Tue Jan 16 12:51:47 Romance Standard Time 2007