package CGI::Application;
use Carp;
use strict;
use Class::ISA;
$CGI::Application::VERSION = '4.06';
my %INSTALLED_CALLBACKS = (
# hook name package sub
init => { 'CGI::Application' => [ 'cgiapp_init' ] },
prerun => { 'CGI::Application' => [ 'cgiapp_prerun' ] },
postrun => { 'CGI::Application' => [ 'cgiapp_postrun' ] },
teardown => { 'CGI::Application' => [ 'teardown' ] },
load_tmpl => { },
error => { },
);
###################################
#### INSTANCE SCRIPT METHODS ####
###################################
sub new {
my $class = shift;
my @args = @_;
if (ref($class)) {
# No copy constructor yet!
$class = ref($class);
}
# Create our object!
my $self = {};
bless($self, $class);
### SET UP DEFAULT VALUES ###
#
# We set them up here and not in the setup() because a subclass
# which implements setup() still needs default values!
$self->header_type('header');
$self->mode_param('rm');
$self->start_mode('start');
# Process optional new() parameters
my $rprops;
if (ref($args[0]) eq 'HASH') {
my $rthash = %{$args[0]};
$rprops = $self->_cap_hash($args[0]);
} else {
$rprops = $self->_cap_hash({ @args });
}
# Set tmpl_path()
if (exists($rprops->{TMPL_PATH})) {
$self->tmpl_path($rprops->{TMPL_PATH});
}
# Set CGI query object
if (exists($rprops->{QUERY})) {
$self->query($rprops->{QUERY});
}
# Set up init param() values
if (exists($rprops->{PARAMS})) {
croak("PARAMS is not a hash ref") unless (ref($rprops->{PARAMS}) eq 'HASH');
my $rparams = $rprops->{PARAMS};
while (my ($k, $v) = each(%$rparams)) {
$self->param($k, $v);
}
}
# Lock prerun_mode from being changed until cgiapp_prerun()
$self->{__PRERUN_MODE_LOCKED} = 1;
# Call cgiapp_init() method, which may be implemented in the sub-class.
# Pass all constructor args forward. This will allow flexible usage
# down the line.
$self->call_hook('init', @args);
# Call setup() method, which should be implemented in the sub-class!
$self->setup();
return $self;
}
sub run {
my $self = shift;
my $q = $self->query();
my $rm_param = $self->mode_param() || croak("No rm_param() specified");
my $rm;
# Support call-back instead of CGI mode param
if (ref($rm_param) eq 'CODE') {
# Get run mode from subref
$rm = $rm_param->($self);
}
# support setting run mode from PATH_INFO
elsif (ref($rm_param) eq 'HASH') {
$rm = $rm_param->{run_mode};
}
# Get run mode from CGI param
else {
$rm = $q->param($rm_param);
}
# If $rm undefined, use default (start) mode
my $def_rm = $self->start_mode();
$def_rm = '' unless defined $def_rm;
$rm = $def_rm unless (defined($rm) && length($rm));
# Set get_current_runmode() for access by user later
$self->{__CURRENT_RUNMODE} = $rm;
# Allow prerun_mode to be changed
delete($self->{__PRERUN_MODE_LOCKED});
# Call PRE-RUN hook, now that we know the run mode
# This hook can be used to provide run mode specific behaviors
# before the run mode actually runs.
$self->call_hook('prerun', $rm);
# Lock prerun_mode from being changed after cgiapp_prerun()
$self->{__PRERUN_MODE_LOCKED} = 1;
# If prerun_mode has been set, use it!
my $prerun_mode = $self->prerun_mode();
if (length($prerun_mode)) {
$rm = $prerun_mode;
$self->{__CURRENT_RUNMODE} = $rm;
}
my %rmodes = ($self->run_modes());
my $rmeth;
my $autoload_mode = 0;
if (exists($rmodes{$rm})) {
$rmeth = $rmodes{$rm};
} else {
# Look for run mode "AUTOLOAD" before dieing
unless (exists($rmodes{'AUTOLOAD'})) {
croak("No such run mode '$rm'");
}
$rmeth = $rmodes{'AUTOLOAD'};
$autoload_mode = 1;
}
# Process run mode!
my $body;
eval {
$body = $autoload_mode ? $self->$rmeth($rm) : $self->$rmeth();
};
if ($@) {
my $error = $@;
$self->call_hook('error', $error);
if (my $em = $self->error_mode) {
$body = $self->$em( $error );
} else {
croak("Error executing run mode '$rm': $error");
}
}
# Make sure that $body is not undefined (supress 'uninitialized value' warnings)
$body = "" unless defined $body;
# Support scalar-ref for body return
my $bodyref = (ref($body) eq 'SCALAR') ? $body : \$body;
# Call cgiapp_postrun() hook
$self->call_hook('postrun', $bodyref);
# Set up HTTP headers
my $headers = $self->_send_headers();
# Build up total output
my $output = $headers.$$bodyref;
# Send output to browser (unless we're in serious debug mode!)
unless ($ENV{CGI_APP_RETURN_ONLY}) {
print $output;
}
# clean up operations
$self->call_hook('teardown');
return $output;
}
############################
#### OVERRIDE METHODS ####
############################
sub cgiapp_get_query {
my $self = shift;
# Include CGI.pm and related modules
require CGI;
# Get the query object
my $q = CGI->new();
return $q;
}
sub cgiapp_init {
my $self = shift;
my @args = (@_);
# Nothing to init, yet!
}
sub cgiapp_prerun {
my $self = shift;
my $rm = shift;
# Nothing to prerun, yet!
}
sub cgiapp_postrun {
my $self = shift;
my $bodyref = shift;
# Nothing to postrun, yet!
}
sub setup {
my $self = shift;
$self->run_modes(
'start' => 'dump_html',
);
}
sub teardown {
my $self = shift;
# Nothing to shut down, yet!
}
######################################
#### APPLICATION MODULE METHODS ####
######################################
sub dump {
my $self = shift;
my $output = '';
# Dump run mode
my $current_runmode = $self->get_current_runmode();
$current_runmode = "" unless (defined($current_runmode));
$output .= "Current Run mode: '$current_runmode'\n";
# Dump Params
$output .= "\nQuery Parameters:\n";
my @params = $self->query->param();
foreach my $p (sort(@params)) {
my @data = $self->query->param($p);
my $data_str = "'".join("', '", @data)."'";
$output .= "\t$p => $data_str\n";
}
# Dump ENV
$output .= "\nQuery Environment:\n";
foreach my $ek (sort(keys(%ENV))) {
$output .= "\t$ek => '".$ENV{$ek}."'\n";
}
return $output;
}
sub dump_html {
my $self = shift;
my $query = $self->query();
my $output = '';
# Dump run-mode
my $current_runmode = $self->get_current_runmode();
$output .= "
Current Run-mode:
'$current_runmode'
\n";
# Dump Params
$output .= "Query Parameters:
\n";
$output .= $query->Dump;
# Dump ENV
$output .= "Query Environment:
\n\n";
foreach my $ek ( sort( keys( %ENV ) ) ) {
$output .= sprintf(
"- %s => '%s'
\n",
$query->escapeHTML( $ek ),
$query->escapeHTML( $ENV{$ek} )
);
}
$output .= "
\n";
return $output;
}
sub header_add {
my $self = shift;
return $self->_header_props_update(\@_,add=>1);
}
sub header_props {
my $self = shift;
return $self->_header_props_update(\@_,add=>0);
}
# used by header_props and header_add to update the headers
sub _header_props_update {
my $self = shift;
my $data_ref = shift;
my %in = @_;
my @data = @$data_ref;
# First use? Create new __HEADER_PROPS!
$self->{__HEADER_PROPS} = {} unless (exists($self->{__HEADER_PROPS}));
my $props;
# If data is provided, set it!
if (scalar(@data)) {
warn("header_props called while header_type set to 'none', headers will NOT be sent!") if $self->header_type eq 'none';
# Is it a hash, or hash-ref?
if (ref($data[0]) eq 'HASH') {
# Make a copy
%$props = %{$data[0]};
} elsif ((scalar(@data) % 2) == 0) {
# It appears to be a possible hash (even # of elements)
%$props = @data;
} else {
my $meth = $in{add} ? 'add' : 'props';
croak("Odd number of elements passed to header_$meth(). Not a valid hash")
}
# merge in new headers, appending new values passed as array refs
if ($in{add}) {
for my $key_set_to_aref (grep { ref $props->{$_} eq 'ARRAY'} keys %$props) {
my $existing_val = $self->{__HEADER_PROPS}->{$key_set_to_aref};
next unless defined $existing_val;
my @existing_val_array = (ref $existing_val eq 'ARRAY') ? @$existing_val : ($existing_val);
$props->{$key_set_to_aref} = [ @existing_val_array, @{ $props->{$key_set_to_aref} } ];
}
$self->{__HEADER_PROPS} = { %{ $self->{__HEADER_PROPS} }, %$props };
}
# Set new headers, clobbering existing values
else {
$self->{__HEADER_PROPS} = $props;
}
}
# If we've gotten this far, return the value!
return (%{ $self->{__HEADER_PROPS}});
}
sub header_type {
my $self = shift;
my ($header_type) = @_;
my @allowed_header_types = qw(header redirect none);
# First use? Create new __HEADER_TYPE!
$self->{__HEADER_TYPE} = 'header' unless (exists($self->{__HEADER_TYPE}));
# If data is provided, set it!
if (defined($header_type)) {
$header_type = lc($header_type);
croak("Invalid header_type '$header_type'")
unless(grep { $_ eq $header_type } @allowed_header_types);
$self->{__HEADER_TYPE} = $header_type;
}
# If we've gotten this far, return the value!
return $self->{__HEADER_TYPE};
}
sub param {
my $self = shift;
my (@data) = (@_);
# First use? Create new __PARAMS!
$self->{__PARAMS} = {} unless (exists($self->{__PARAMS}));
my $rp = $self->{__PARAMS};
# If data is provided, set it!
if (scalar(@data)) {
# Is it a hash, or hash-ref?
if (ref($data[0]) eq 'HASH') {
# Make a copy, which augments the existing contents (if any)
%$rp = (%$rp, %{$data[0]});
} elsif ((scalar(@data) % 2) == 0) {
# It appears to be a possible hash (even # of elements)
%$rp = (%$rp, @data);
} elsif (scalar(@data) > 1) {
croak("Odd number of elements passed to param(). Not a valid hash");
}
} else {
# Return the list of param keys if no param is specified.
return (keys(%$rp));
}
# If exactly one parameter was sent to param(), return the value
if (scalar(@data) <= 2) {
my $param = $data[0];
return $rp->{$param};
}
return; # Otherwise, return undef
}
sub delete {
my $self = shift;
my ($param) = @_;
#return undef it it isn't defined
return undef if(!defined($param));
#simply delete this param from $self->{__PARAMS}
delete $self->{__PARAMS}->{$param};
}
sub query {
my $self = shift;
my ($query) = @_;
# We're only allowed to set a new query object if one does not yet exist!
unless (exists($self->{__QUERY_OBJ})) {
my $new_query_obj;
# If data is provided, set it! Otherwise, create a new one.
if (defined($query)) {
$new_query_obj = $query;
} else {
$new_query_obj = $self->cgiapp_get_query();
}
$self->{__QUERY_OBJ} = $new_query_obj;
}
return $self->{__QUERY_OBJ};
}
sub run_modes {
my $self = shift;
my (@data) = (@_);
# First use? Create new __RUN_MODES!
$self->{__RUN_MODES} = {} unless (exists($self->{__RUN_MODES}));
my $rr_m = $self->{__RUN_MODES};
# If data is provided, set it!
if (scalar(@data)) {
# Is it a hash, hash-ref, or array-ref?
if (ref($data[0]) eq 'HASH') {
# Make a copy, which augments the existing contents (if any)
%$rr_m = (%$rr_m, %{$data[0]});
} elsif (ref($data[0]) eq 'ARRAY') {
# Convert array-ref into hash table
foreach my $rm (@{$data[0]}) {
$rr_m->{$rm} = $rm;
}
} elsif ((scalar(@data) % 2) == 0) {
# It appears to be a possible hash (even # of elements)
%$rr_m = (%$rr_m, @data);
} else {
croak("Odd number of elements passed to run_modes(). Not a valid hash");
}
}
# If we've gotten this far, return the value!
return (%$rr_m);
}
sub start_mode {
my $self = shift;
my ($start_mode) = @_;
# First use? Create new __START_MODE
$self->{__START_MODE} = 'start' unless (exists($self->{__START_MODE}));
# If data is provided, set it
if (defined($start_mode)) {
$self->{__START_MODE} = $start_mode;
}
return $self->{__START_MODE};
}
sub error_mode {
my $self = shift;
my ($error_mode) = @_;
# First use? Create new __ERROR_MODE
$self->{__ERROR_MODE} = undef unless (exists($self->{__ERROR_MODE}));
# If data is provided, set it.
if (defined($error_mode)) {
$self->{__ERROR_MODE} = $error_mode;
}
return $self->{__ERROR_MODE};
}
sub tmpl_path {
my $self = shift;
my ($tmpl_path) = @_;
# First use? Create new __TMPL_PATH!
$self->{__TMPL_PATH} = '' unless (exists($self->{__TMPL_PATH}));
# If data is provided, set it!
if (defined($tmpl_path)) {
$self->{__TMPL_PATH} = $tmpl_path;
}
# If we've gotten this far, return the value!
return $self->{__TMPL_PATH};
}
sub prerun_mode {
my $self = shift;
my ($prerun_mode) = @_;
# First use? Create new __PRERUN_MODE
$self->{__PRERUN_MODE} = '' unless (exists($self->{__PRERUN_MODE}));
# Was data provided?
if (defined($prerun_mode)) {
# Are we allowed to set prerun_mode?
if (exists($self->{__PRERUN_MODE_LOCKED})) {
# Not allowed! Throw an exception.
croak("prerun_mode() can only be called within cgiapp_prerun()! Error");
} else {
# If data is provided, set it!
$self->{__PRERUN_MODE} = $prerun_mode;
}
}
# If we've gotten this far, return the value!
return $self->{__PRERUN_MODE};
}
sub get_current_runmode {
my $self = shift;
# It's OK if we return undef if this method is called too early
return $self->{__CURRENT_RUNMODE};
}
###########################
#### PRIVATE METHODS ####
###########################
sub _send_headers {
my $self = shift;
my $q = $self->query();
my $header_type = $self->header_type();
if ($header_type eq 'redirect') {
return $q->redirect($self->header_props());
} elsif ($header_type eq 'header' ) {
return $q->header($self->header_props());
}
# croak() if we have an unknown header type
croak ("Invalid header_type '$header_type'") unless ($header_type eq "none");
# Do nothing if header type eq "none".
return "";
}
# Make all hash keys CAPITAL
# although this method is internal, some other extensions
# have come to rely on it, so any changes here should be
# made with great care or avoided.
sub _cap_hash {
my $self = shift;
my $rhash = shift;
my %hash = map {
my $k = $_;
my $v = $rhash->{$k};
$k =~ tr/a-z/A-Z/;
$k => $v;
} keys(%{$rhash});
return \%hash;
}
1;
=pod
=head1 NAME
CGI::Application -
Framework for building reusable web-applications
=head1 SYNOPSIS
# In "WebApp.pm"...
package WebApp;
use base 'CGI::Application';
# ( setup() can even be skipped for common cases. See docs below. )
sub setup {
my $self = shift;
$self->start_mode('mode1');
$self->mode_param('rm');
$self->run_modes(
'mode1' => 'do_stuff',
'mode2' => 'do_more_stuff',
'mode3' => 'do_something_else'
);
}
sub do_stuff { ... }
sub do_more_stuff { ... }
sub do_something_else { ... }
1;
### In "webapp.cgi"...
use WebApp;
my $webapp = WebApp->new();
$webapp->run();
=head1 INTRODUCTION
CGI::Application is intended to make it easier to create sophisticated,
high-performance, reusable web-based applications. This module implements a
methodology which, if followed, will make your web software easier to design,
easier to document, easier to write, and easier to evolve.
CGI::Application judiciously avoids employing technologies and techniques which
would bind a developer to any one set of tools, operating system or web server.
=head1 USAGE EXAMPLE
Imagine you have to write an application to search through a database
of widgets. Your application has three screens:
1. Search form
2. List of results
3. Detail of a single record
To write this application using CGI::Application you will create two files:
1. WidgetView.pm -- Your "Application Module"
2. widgetview.cgi -- Your "Instance Script"
The Application Module contains all the code specific to your
application functionality, and it exists outside of your web server's
document root, somewhere in the Perl library search path.
The Instance Script is what is actually called by your web server. It is
a very small, simple file which simply creates an instance of your
application and calls an inherited method, run(). Following is the
entirety of "widgetview.cgi":
#!/usr/bin/perl -w
use WidgetView;
my $webapp = WidgetView->new();
$webapp->run();
As you can see, widgetview.cgi simply "uses" your Application module
(which implements a Perl package called "WidgetView"). Your Application Module,
"WidgetView.pm", is somewhat more lengthy:
package WidgetView;
use base 'CGI::Application';
use strict;
# Needed for our database connection
use CGI::Application::Plugin::DBH;
sub setup {
my $self = shift;
$self->start_mode('mode1');
$self->run_modes(
'mode1' => 'showform',
'mode2' => 'showlist',
'mode3' => 'showdetail'
);
# Connect to DBI database, with the same args as DBI->connect();
$self->dbh_config();
}
sub teardown {
my $self = shift;
# Disconnect when we're done, (Although DBI usually does this automatically)
$self->dbh->disconnect();
}
sub showform {
my $self = shift;
# Get CGI query object
my $q = $self->query();
my $output = '';
$output .= $q->start_html(-title => 'Widget Search Form');
$output .= $q->start_form();
$output .= $q->textfield(-name => 'widgetcode');
$output .= $q->hidden(-name => 'rm', -value => 'mode2');
$output .= $q->submit();
$output .= $q->end_form();
$output .= $q->end_html();
return $output;
}
sub showlist {
my $self = shift;
# Get our database connection
my $dbh = $self->dbh();
# Get CGI query object
my $q = $self->query();
my $widgetcode = $q->param("widgetcode");
my $output = '';
$output .= $q->start_html(-title => 'List of Matching Widgets');
## Do a bunch of stuff to select "widgets" from a DBI-connected
## database which match the user-supplied value of "widgetcode"
## which has been supplied from the previous HTML form via a
## CGI.pm query object.
##
## Each row will contain a link to a "Widget Detail" which
## provides an anchor tag, as follows:
##
## "widgetview.cgi?rm=mode3&widgetid=XXX"
##
## ...Where "XXX" is a unique value referencing the ID of
## the particular "widget" upon which the user has clicked.
$output .= $q->end_html();
return $output;
}
sub showdetail {
my $self = shift;
# Get our database connection
my $dbh = $self->dbh();
# Get CGI query object
my $q = $self->query();
my $widgetid = $q->param("widgetid");
my $output = '';
$output .= $q->start_html(-title => 'Widget Detail');
## Do a bunch of things to select all the properties of
## the particular "widget" upon which the user has
## clicked. The key id value of this widget is provided
## via the "widgetid" property, accessed via the CGI.pm
## query object.
$output .= $q->end_html();
return $output;
}
1; # Perl requires this at the end of all modules
CGI::Application takes care of implementing the new() and the run()
methods. Notice that at no point do you call print() to send any
output to STDOUT. Instead, all output is returned as a scalar.
CGI::Application's most significant contribution is in managing
the application state. Notice that all which is needed to push
the application forward is to set the value of a HTML form
parameter 'rm' to the value of the "run mode" you wish to handle
the form submission. This is the key to CGI::Application.
=head1 ABSTRACT
The guiding philosophy behind CGI::Application is that a web-based
application can be organized into a specific set of "Run Modes."
Each Run Mode is roughly analogous to a single screen (a form, some
output, etc.). All the Run Modes are managed by a single "Application
Module" which is a Perl module. In your web server's document space
there is an "Instance Script" which is called by the web server as a
CGI (or an Apache::Registry script if you're using Apache + mod_perl).
This methodology is an inversion of the "Embedded" philosophy (ASP, JSP,
EmbPerl, Mason, etc.) in which there are "pages" for each state of the
application, and the page drives functionality. In CGI::Application,
form follows function -- the Application Module drives pages, and the
code for a single application is in one place; not spread out over
multiple "pages". If you feel that Embedded architectures are
confusing, unorganized, difficult to design and difficult to manage,
CGI::Application is the methodology for you!
Apache is NOT a requirement for CGI::Application. Web applications based on
CGI::Application will run equally well on NT/IIS or any other
CGI-compatible environment. CGI::Application-based projects
are, however, ripe for use on Apache/mod_perl servers, as they
naturally encourage Good Programming Practices and will often work
in persistent environments without modification.
For more information on using CGI::Application with mod_perl, please see our
website at http://www.cgi-app.org/, as well as
L, which integates with L.
=head1 DESCRIPTION
CGI::Application is an Object-Oriented Perl module which implements an
Abstract Class. It is not intended that this package be instantiated
directly. Instead, it is intended that your Application Module will be
implemented as a Sub-Class of CGI::Application.
To inherit from CGI::Application, the following code should go at
the beginning of your Application Module, after your package declaration:
use base 'CGI::Application';
B
For the purpose of this document, we will refer to the
following conventions:
WebApp.pm The Perl module which implements your Application Module class.
WebApp Your Application Module class; a sub-class of CGI::Application.
webapp.cgi The Instance Script which implements your Application Module.
$webapp An instance (object) of your Application Module class.
$self Same as $webapp, used in instance methods to pass around the
current object. (Standard Perl Object-Oriented technique)
=head2 Instance Script Methods
By inheriting from CGI::Application you have access to a
number of built-in methods. The following are those which
are expected to be called from your Instance Script.
=over 4
=item new()
The new() method is the constructor for a CGI::Application. It returns
a blessed reference to your Application Module package (class). Optionally,
new() may take a set of parameters as key => value pairs:
my $webapp = App->new(
TMPL_PATH => 'App/',
PARAMS => {
'custom_thing_1' => 'some val',
'another_custom_thing' => [qw/123 456/]
}
);
This method may take some specific parameters:
B - This optional parameter defines a path to a directory of templates.
This is used by the load_tmpl() method (specified below), and may also be used
for the same purpose by other template plugins. This run-time parameter allows
you to further encapsulate instantiating templates, providing potential for
more re-usability. It can be either a scalar or an array reference of multiple
paths.
B - This optional parameter allows you to specify an
already-created CGI.pm query object. Under normal use,
CGI::Application will instantiate its own CGI.pm query object.
Under certain conditions, it might be useful to be able to use
one which has already been created.
B - This parameter, if used, allows you to set a number
of custom parameters at run-time. By passing in different
values in different instance scripts which use the same application
module you can achieve a higher level of re-usability. For instance,
imagine an application module, "Mailform.pm". The application takes
the contents of a HTML form and emails it to a specified recipient.
You could have multiple instance scripts throughout your site which
all use this "Mailform.pm" module, but which set different recipients
or different forms.
One common use of instance scripts is to provide a path to a config file. This
design allows you to define project wide configuration objects used by many
several instance scripts. There are several plugins which simplify the syntax
for this and provide lazy loading. Here's an example using
L, which uses L to support
many configuration file formats.
my $app = WebApp->new(PARAMS => { cfg_file => 'config.pl' });
# Later in your app:
my %cfg = $self->cfg()
# or ... $self->cfg('HTML_ROOT_DIR');
See the list of of plugins below for more config file integration solutions.
=item run()
The run() method is called upon your Application Module object, from
your Instance Script. When called, it executes the functionality
in your Application Module.
my $webapp = WebApp->new();
$webapp->run();
This method first determines the application state by looking at the
value of the CGI parameter specified by mode_param() (defaults to
'rm' for "Run Mode"), which is expected to contain the name of the mode of
operation. If not specified, the state defaults to the value
of start_mode().
Once the mode has been determined, run() looks at the dispatch
table stored in run_modes() and finds the function pointer which
is keyed from the mode name. If found, the function is called and the
data returned is print()'ed to STDOUT and to the browser. If
the specified mode is not found in the run_modes() table, run() will
croak().
B
If the runmode dies for whatever reason, run() will see if you have set a
value for error_mode(). If you have, run() will call that method
as a run mode, passing $@ as the only parameter.
Plugins authors will be interested to know that just before C is
called, the C hook will be executed, with the error message passed in as
the only parameter. This hook is still considered experimental, although it is
unlikely to change.
For a complete integrated logging solution, check out L.
=back
=head2 Sub-classing and Override Methods
CGI::Application implements some methods which are expected to be overridden
by implementing them in your sub-class module. These methods are as follows:
=over 4
=item setup()
This method is called by the inherited new() constructor method. The
setup() method should be used to define the following property/methods:
mode_param() - set the name of the run mode CGI param.
start_mode() - text scalar containing the default run mode.
error_mode() - text scalar containing the error mode.
run_modes() - hash table containing mode => function mappings.
tmpl_path() - text scalar or array refefence containing path(s) to template files.
Your setup() method may call any of the instance methods of your application.
This function is a good place to define properties specific to your application
via the $webapp->param() method.
Your setup() method might be implemented something like this:
sub setup {
my $self = shift;
$self->tmpl_path('/path/to/my/templates/');
$self->start_mode('putform');
$self->error_mode('my_error_rm');
$self->run_modes({
'putform' => 'my_putform_func',
'postdata' => 'my_data_func'
});
$self->param('myprop1');
$self->param('myprop2', 'prop2value');
$self->param('myprop3', ['p3v1', 'p3v2', 'p3v3']);
}
However, often times all that needs to be in setup() is defining your run modes
and your start mode. L allows you to do
this with a simple syntax, using run mode attributes:
use CGI::Application::Plugin::AutoRunmode;
sub show_first : StartRunmode { ... };
sub do_next : Runmode { ... }
=item teardown()
If implemented, this method is called automatically after your application runs. It
can be used to clean up after your operations. A typical use of the
teardown() function is to disconnect a database connection which was
established in the setup() function. You could also use the teardown()
method to store state information about the application to the server.
=item cgiapp_init()
If implemented, this method is called automatically right before the
setup() method is called. This method provides an optional initialization
hook, which improves the object-oriented characteristics of
CGI::Application. The cgiapp_init() method receives, as its parameters,
all the arguments which were sent to the new() method.
An example of the benefits provided by utilizing this hook is
creating a custom "application super-class" from which which all
your web applications would inherit, instead of CGI::Application.
Consider the following:
# In MySuperclass.pm:
package MySuperclass;
use base 'CGI::Application';
sub cgiapp_init {
my $self = shift;
# Perform some project-specific init behavior
# such as to load settings from a database or file.
}
# In MyApplication.pm:
package MyApplication;
use base 'MySuperclass';
sub setup { ... }
sub teardown { ... }
# The rest of your CGI::Application-based follows...
By using CGI::Application and the cgiapp_init() method as illustrated,
a suite of applications could be designed to share certain
characteristics. This has the potential for much cleaner code
built on object-oriented inheritance.
=item cgiapp_prerun()
If implemented, this method is called automatically right before the
selected run mode method is called. This method provides an optional
pre-runmode hook, which permits functionality to be added at the point
right before the run mode method is called. To further leverage this
hook, the value of the run mode is passed into cgiapp_prerun().
Another benefit provided by utilizing this hook is
creating a custom "application super-class" from which all
your web applications would inherit, instead of CGI::Application.
Consider the following:
# In MySuperclass.pm:
package MySuperclass;
use base 'CGI::Application';
sub cgiapp_prerun {
my $self = shift;
# Perform some project-specific init behavior
# such as to implement run mode specific
# authorization functions.
}
# In MyApplication.pm:
package MyApplication;
use base 'MySuperclass';
sub setup { ... }
sub teardown { ... }
# The rest of your CGI::Application-based follows...
By using CGI::Application and the cgiapp_prerun() method as illustrated,
a suite of applications could be designed to share certain
characteristics. This has the potential for much cleaner code
built on object-oriented inheritance.
It is also possible, within your cgiapp_prerun() method, to change the
run mode of your application. This can be done via the prerun_mode()
method, which is discussed elsewhere in this POD.
=item cgiapp_postrun()
If implemented, this hook will be called after the run mode method
has returned its output, but before HTTP headers are generated. This
will give you an opportunity to modify the body and headers before they
are returned to the web browser.
A typical use for this hook is pipelining the output of a CGI-Application
through a series of "filter" processors. For example:
* You want to enclose the output of all your CGI-Applications in
an HTML table in a larger page.
* Your run modes return structured data (such as XML), which you
want to transform using a standard mechanism (such as XSLT).
* You want to post-process CGI-App output through another system,
such as HTML::Mason.
* You want to modify HTTP headers in a particular way across all
run modes, based on particular criteria.
The cgiapp_postrun() hook receives a reference to the output from
your run mode method, in addition to the CGI-App object. A typical
cgiapp_postrun() method might be implemented as follows:
sub cgiapp_postrun {
my $self = shift;
my $output_ref = shift;
# Enclose output HTML table
my $new_output = "";
$new_output .= "| Hello, World! |
";
$new_output .= "| ". $$output_ref ." |
";
$new_output .= "
";
# Replace old output with new output
$$output_ref = $new_output;
}
Obviously, with access to the CGI-App object you have full access to use all
the methods normally available in a run mode. You could, for example, use
load_tmpl() to replace the static HTML in this example with HTML::Template.
You could change the HTTP headers (via header_type() and header_props()
methods) to set up a redirect. You could also use the objects properties
to apply changes only under certain circumstance, such as a in only certain run
modes, and when a param() is a particular value.
=item cgiapp_get_query()
This method is called when CGI::Application retrieves the query object.
The cgiapp_get_query() method loads CGI.pm via "require" and returns a
CGI.pm query object. The implementation is as follows:
sub cgiapp_get_query {
my $self = shift;
require CGI;
return CGI->new();
}
You may override this method if you wish to use a different query
interface instead of CGI.pm. Note, however, that your query interface
must be compatible with CGI.pm, or you must wrap your chosen query
interface in a "wrapper" class to achieve compatibility.
=back
=head2 Application Module Methods
The following methods are inherited from CGI::Application, and are
available to be called by your application within your Application
Module. These functions are listed in alphabetical order.
=over 4
=item delete()
$webapp->delete('my_param');
The delete() method is used to delete a parameter that was previously
stored inside of your application either by using the PARAMS hash that
was passed in your call to new() or by a call to the param() method.
This is similar to the delete() method of CGI.pm. It is useful if your
application makes decisions based on the existence of certain params that
may have been removed in previous sections of your app or simply to
clean-up your param()s.
=item dump()
print STDERR $webapp->dump();
The dump() method is a debugging function which will return a
chunk of text which contains all the environment and web form
data of the request, formatted nicely for human readability.
Useful for outputting to STDERR.
=item dump_html()
my $output = $webapp->dump_html();
The dump_html() method is a debugging function which will return
a chunk of text which contains all the environment and web form
data of the request, formatted nicely for human readability via
a web browser. Useful for outputting to a browser.
=item error_mode()
$webapp->error_mode('my_error_rm');
The error_mode contains the name of a run mode to call in the event that the
planned run mode call fails C. No C is defined by default.
The death of your C run mode is not trapped, so you can also use
it to die in your own special way.
=item get_current_runmode()
$webapp->get_current_runmode();
The get_current_runmode() method will return a text scalar containing
the name of the run mode which is currently being executed. If the
run mode has not yet been determined, such as during setup(), this method
will return undef.
=item header_add()
# add or replace the 'type' header
$webapp->header_add( -type => 'image/png' );
- or -
# add an additional cookie
$webapp->header_add(-cookie=>[$extra_cookie]);
The header_add() method is used to add one or more headers to the outgoing
response headers. The parameters will eventuallly be passed on to the CGI.pm
header() method, so refer to the L docs for exact usage details.
Unlike calling header_props(), header_add() will preserve any existing
headers. If a scalar value is passed to header_add() it will replace
the existing value for that key.
If an array reference is passed as a value to header_add(), values in
that array ref will be appended to any existing values values for that key.
This is primarily useful for setting an additional cookie after one has already
been set.
=item header_props()
$webapp->header_props(-type=>'image/gif',-expires=>'+3d');
The header_props() method expects a hash of CGI.pm-compatible
HTTP header properties. These properties will be passed directly
to CGI.pm's header() or redirect() methods. Refer to L
for exact usage details.
Calling header_props will clobber any existing headers that have
previously set.
To add additional headers later without clobbering the old ones,
see L.
header_props() return a hash of all the headers that have currently
been set.
B
It is through the header_props() and header_add() method that you may modify the outgoing
HTTP headers. This is necessary when you want to set a cookie, set the mime
type to something other than "text/html", or perform a redirect. The
header_props() method works in conjunction with the header_type() method.
The value contained in header_type() determines if we use CGI::header() or
CGI::redirect(). The content of header_props() is passed as an argument to
whichever CGI.pm function is called.
Understanding this relationship is important if you wish to manipulate
the HTTP header properly.
=item header_type([<'header' || 'redirect' || 'none'>])
$webapp->header_type('redirect');
The header_type() method expects to be passed either 'header', 'redirect', or 'none'.
This method specifies the type of HTTP headers which should be sent back to
the browser. If not specified, defaults is 'header'. See the
header section of L for details.
To perform a redirect using CGI::Application (and CGI.pm), you would
do the following:
sub some_redirect_mode {
my $self = shift;
my $new_url = "http://site/path/doc.html";
$self->header_type('redirect');
$self->header_props(-url=>$new_url);
return "Redirecting to $new_url";
}
If you wish to suppress HTTP headers entirely (as might be the case if
you're working in a slightly more exotic environment), you can set
header_type() to "none". This will completely hide headers.
=item load_tmpl()
my $tmpl_obj = $webapp->load_tmpl;
my $tmpl_obj = $webapp->load_tmpl('some.html');
my $tmpl_obj = $webapp->load_tmpl( \$template_content );
my $tmpl_obj = $webapp->load_tmpl( FILEHANDLE );
This method takes the name of a template file, a reference to template data
or a FILEHANDLE and returns an HTML::Template object. If the filename is undefined or missing, CGI::Application will default to trying to use the current run mode name, plus the extension ".html".
If you use the default template naming system, you should also use
L, which simply helps to keep the current
name accurate when you pass control from one run mode to another.
( For integration with other template systems
and automated template names, see "Alternatives to load_tmpl() below. )
When you pass in a filename, the HTML::Template->new_file() constructor
is used for create the object. When you pass in a reference to the template
content, the HTML::Template->new_scalar_ref() constructor is used and
when you pass in a filehandle, the HTML::Template->new_filehandle()
constructor is used.
Refer to L for specific usage of HTML::Template.
If tmpl_path() has been specified, load_tmpl() will set the
HTML::Template C option to the path(s) provided. This further
assists in encapsulating template usage.
The load_tmpl() method will pass any extra parameters sent to it directly to
HTML::Template->new_file() (or new_scalar_ref() or new_filehandle()).
This will allow the HTML::Template object to be further customized:
my $tmpl_obj = $webapp->load_tmpl('some_other.html',
die_on_bad_params => 0,
cache => 1
);
Note that if you want to pass extra arguments but use the default template
name, you still need to provide a name of C:
my $tmpl_obj = $webapp->load_tmpl(undef',
die_on_bad_params => 0,
cache => 1
);
B
If your application requires more specialized behavior than this, you can
always replace it by overriding load_tmpl() by implementing your own
load_tmpl() in your CGI::Application sub-class application module.
First, you may want to check out the template related plugins.
L provides a consistent interface to
HTML::Template, Template Toolkit, and Petal, automatic default file names, and
other features. Check this one out first.
L focuses just on Template Toolkit integration,
and features pre-and-post features, singleton support and more.
L can help if you want to return a stream and
not a file. It features a simple syntax and MIME-type detection.
B
Plugin authors will be intersted to know that you can register a callback that
will be executed just before load_tmpl() returns:
$self->add_callback('load_tmpl',\&your_method);
When C is executed, it will be passed three arguments:
1. A hash reference of the extra params passed into C
2. Followed by a hash reference to template parameters.
With both of these, you can modify them by reference to affect
values that are actually passed to the new() and param() methods of the
template object.
3. The name of the template file.
Here's an example stub for a load_tmpl() callback:
sub my_load_tmpl_callback {
my ($self, $ht_params, $tmpl_params, $tmpl_file) = @_
# modify $ht_params or $tmpl_params by reference...
}
=cut
sub load_tmpl {
my $self = shift;
my ($tmpl_file, @extra_params) = @_;
# add tmpl_path to path array if one is set, otherwise add a path arg
if (my $tmpl_path = $self->tmpl_path) {
my @tmpl_paths = (ref $tmpl_path eq 'ARRAY') ? @$tmpl_path : $tmpl_path;
my $found = 0;
for( my $x = 0; $x < @extra_params; $x += 2 ) {
if ($extra_params[$x] eq 'path' and
ref $extra_params[$x+1] eq 'ARRAY') {
unshift @{$extra_params[$x+1]}, @tmpl_paths;
$found = 1;
last;
}
}
push(@extra_params, path => [ @tmpl_paths ]) unless $found;
}
my %tmpl_params = ();
my %ht_params = @extra_params;
%ht_params = () unless keys %ht_params;
# Define our extension if doesn't already exist;
$self->{__CURRENT_TMPL_EXTENSION} = '.html' unless defined $self->{__CURRENT_TMPL_EXTENSION};
# Define a default templat name based on the current run mode
unless (defined $tmpl_file) {
$tmpl_file = $self->get_current_runmode . $self->{__CURRENT_TMPL_EXTENSION};
}
$self->call_hook('load_tmpl', \%ht_params, \%tmpl_params, $tmpl_file);
require HTML::Template;
# let's check $tmpl_file and see what kind of parameter it is - we
# now support 3 options: scalar (filename), ref to scalar (the
# actual html/template content) and reference to FILEHANDLE
my $t = undef;
if ( ref $tmpl_file eq 'SCALAR' ) {
$t = HTML::Template->new_scalar_ref( $tmpl_file, %ht_params );
} elsif ( ref $tmpl_file eq 'GLOB' ) {
$t = HTML::Template->new_filehandle( $tmpl_file, %ht_params );
} else {
$t = HTML::Template->new_file($tmpl_file, %ht_params);
}
if (keys %tmpl_params) {
$t->param(%tmpl_params);
}
return $t;
}
=pod
=item mode_param()
# Name the CGI form parameter that contains the run mode name.
# This is the the default behavior, and is often sufficient.
$webapp->mode_param('rm');
# Set the run mode name directly from a code ref
$webapp->mode_param(\&some_method);
# Alternate interface, which allows you to set the run
# mode name directly from $ENV{PATH_INFO}.
$webapp->mode_param(
path_info=> 1,
param =>'rm'
);
This accessor/mutator method is generally called in the setup() method.
It is used to help determine the run mode to call. There are three options for calling it.
$webapp->mode_param('rm');
Here, a CGI form parameter is named that will contain the name of the run mode
to use. This is the default behavior, with 'rm' being the parameter named used.
$webapp->mode_param(\&some_method);
Here a code reference is provided. It will return the name of the run mode
to use directly. Example:
sub some_method {
my $self = shift;
return 'run_mode_x';
}
This would allow you to programmatically set the run mode based on arbitrary logic.
$webapp->mode_param(
path_info=> 1,
param =>'rm'
);
This syntax allows you to easily set the run mode from $ENV{PATH_INFO}. It
will try to set the run mode from the first part of $ENV{PATH_INFO} (before the
first "/"). To specify that you would rather get the run mode name from the 2nd
part of $ENV{PATH_INFO}:
$webapp->mode_param( path_info=> 2 );
This also demonstrates that you don't need to pass in the C hash key. It will
still default to C.
You can also set C to a negative value. This works just like a negative
list index: if it is -1 the run mode name will be taken from the last part of
$ENV{PATH_INFO}, if it is -2, the one before that, and so on.
If no run mode is found in $ENV{PATH_INFO}, it will fall back to looking in the
value of a the CGI form field defined with 'param', as described above. This
allows you to use the convenient $ENV{PATH_INFO} trick most of the time, but
also supports the edge cases, such as when you don't know what the run mode
will be ahead of time and want to define it with JavaScript.
B.
Using $ENV{PATH_INFO} to name your run mode creates a clean seperation between
the form variables you submit and how you determine the processing run mode. It
also creates URLs that are more search engine friendly. Let's look at an
example form submission using this syntax: