Item Description: Framework for building reusable web-applications in an object-orientated and extensible fashion
Review Synopsis:
CGI::Application
Item description : Framework for building reusable web-applications in an object-orientated and extensible fashion
Review Synopsis : An excellent and well-thought out module implementing powerful and downright easy methods to write and build CGI-based applications.
Why use CGI::Application?
The CGI::Application module implements this object-orientated framework within which to build your application by creating a super-class within which specific 'run-modes' can be defined. Each run-mode can be correlated to a particular stage of the HTML/CGI user interface. Furthermore, the CGI::Application module implements methods to address the CGI.pm and HTML::Template modules, facilitating powerful methods by which to build extensible and portable web applications - It is obvious that the module author, Jesse Erlbaum (jesse at vm dot com), has put a lot of work into the development and writing of this module.
This module allows you to do away with ugly code through the combination of CGI.pm, HTML::Template and methods provided by CGI::Application to implement persistent variables across execution states.
(Part of the reason too as to my personal usage of this module is its integration with my preferred templating framework, HTML::Template - Different web application frameworks were reviewed by princepawn at Web Application Frameworks and their Templating Engines with a Comparative Study of Template and HTML::Template)
Why avoid CGI::Application?
There seems to be few reasons why to avoid using the CGI::Application module - Usage may not be necessary if persistence and state methods are implemented with other frameworks such as Apache::Session, HTML::Mason or HTML::Embperl.
How to use CGI::Application?
The documentation for the CGI::Application module is excellent and covers the building of a (theoretical) database search application. In short, this module promotes the building of applications as self-styled modules based on the CGI::Application framework - The end script (application) consists of an instance script and a module package.
An example instance script may look like this:
#!/usr/bin/perl # Use our self-styled application module built on the CGI::Applicati +on framework # use self; # Create constructor for our application module (self->new()) and ex +ecute this module in the desired run mode (self->run()) - The desired + run mode is set via the value of a CGI parameter specified by the mo +de_param() method of this module (see self-styled CGI::Application mo +dule package). # my ($application) = self->new(); $application->run(); exit 0;
package self; sub setup { my ($self) = shift; # Set the CGI parameter from which the run-mode of the applicati +on is derived to be 'stage' - This method allows you to set your own +run-mode specifier, passed to your CGI script via hidden HTML form in +put tags # $self->mode_param('stage'); # Set the matched run-mode / subroutine functions to determine w +hich function should be executed for a given run-mode - This method i +s that which allows reusable code to be easily implemented for separa +te run-mode. # # In this example, the subroutines 'display_form' and 'display_r +esults' have been specified to run for run-modes '1' and '2' respecti +vely. The subroutines can be defined as either a hard reference to t +he run-mode method or the name of the run-mode method to be called. # $self->run_modes({ '1' => \&display_form, '2' => 'display_results', }); # Set the mode which should be run in the first instance of the +script, where no run-mode may be specified by a passed CGI variable - + By default, this mode is called 'start', which too must be reference +d to a subroutine via the run_modes() method. # $self->start_mode('1'); }; 1;
sub setup { my ($self) = shift; $self->mode_param('stage'); $self->run_modes({ '1' => \&display_form, '2' => 'display_results', }); $self->start_mode('1'); # Create DBI connection handle and make accessible to all run-mo +des via CGI::Application param() method # $self->param('dbh' => DBI->connect()); };
sub teardown { my ($self) = shift; # Disconnect the (persistent, with Apache::DBI) DBI handle creat +ed in setup() and passed via the param() method # $self->param('dbh')->disconnect; };
# An example run-mode method # sub display_page { my ($self) = shift; # Create a HTML::Template blessed reference with the template fi +le, page.html # my ($html) = $self->load_tmpl('page.html'); print STDOUT $html->output; # printing template output direc +tly to STDOUT (*BAD*) return $html->output; # return template output to CGI: +:Application framework for display (*GOOD*) };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: CGI::Application
by markjugg (Curate) on Sep 01, 2001 at 21:01 UTC | |
Re: CGI::Application
by techcode (Hermit) on Aug 14, 2005 at 10:03 UTC |