#!/usr/bin/perl
# Use our self-styled application module built on the CGI::Application framework
#
use self;
# Create constructor for our application module (self->new()) and execute 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 mode_param() method of this module (see self-styled CGI::Application module 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 application 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 input tags
#
$self->mode_param('stage');
# Set the matched run-mode / subroutine functions to determine which function should be executed for a given run-mode - This method is that which allows reusable code to be easily implemented for separate run-mode.
#
# In this example, the subroutines 'display_form' and 'display_results' have been specified to run for run-modes '1' and '2' respectively. The subroutines can be defined as either a hard reference to the 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 referenced 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-modes via CGI::Application param() method
#
$self->param('dbh' => DBI->connect());
};
##
##
sub teardown {
my ($self) = shift;
# Disconnect the (persistent, with Apache::DBI) DBI handle created 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 file, page.html
#
my ($html) = $self->load_tmpl('page.html');
print STDOUT $html->output; # printing template output directly to STDOUT (*BAD*)
return $html->output; # return template output to CGI::Application framework for display (*GOOD*)
};