Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The following is a small script framework that I have used for a couple of smallish CGI applications. While it doesn't incorporate session handling in any sense more than hidden HTML fields, this could easily be changed through the use of Apache::Session and instead of returning the parameters retrieved so far into hidden fields of the template markup, the single session id could be that passed into these fields.

Also too, with regard to templating engine, there is nothing to stop your usage of Template::Toolkit with CGI::Application, an aspect of usage which I commented on here.

The code including some comments follows ...

#!/usr/bin/perl use constant DATABASE => 'database'; use constant USERNAME => 'user'; use constant PASSWORD => 'pass'; use constant HOST => 'localhost'; use DBI; use Data::FormValidator; use strict; # The following is the "configurable" portion of this # script where templates, behaviour and subroutines can # be modified. It also incorporates the definitions of # mode_param, run_modes and start_mode which correspond # to the respective definitions within the # CGI::Application module - These definitions have been # included here so that the CGI::Application-inherited # class should only need to be edited for the # introduction of new behaviours and subroutines. my $app = Self->new( # File path of template files location 'TMPL_PATH' => '../templates', 'PARAMS' => { # Database handle if required - This can be any # name as the teardown routine will iterate # through all parameters and call disconnect on # DBI handles. 'dbh' => DBI->connect( "DBI:mysql:database=@{[ DATABASE ]};host=@{[ HOST ]}", USERNAME, PASSWORD ), # HTML templates which correspond to each run_mode 'html' => { '1' => 'stage1.tmpl', '2' => 'stage2.tmpl', '3' => 'stage3.tmpl', }, # See comments above - Of special note is the # fact that dispatch subroutines are defined by # their name rather than code references. Eg. # \&form_entry, \&form_submission - This # functionality requires CGI::Application version # 1.3 or greater (current version is 2.1). With # older versions of CGI::Application, code # references can still be employed however, the # full class name of the reference must be # employed because this definition is taking # place outside of the Self class where the # methods reside. Eg. \&Self::form_entry, # \&Self::form_submission. See PM node 137024 'mode_param' => 'stage', 'run_modes' => { 'AUTOLOAD' => 'form_entry', '1' => 'form_entry', '2' => 'form_entry', '3' => 'form_submission' }, 'start_mode' => '1', # The following parameter is a # Data::FormValidator (or HTML::FormValidator) # object which if defined is called for the # validation and vetting of parameters from each # run-mode - Note that if this is employed the # first run-mode (start_mode) must have a null- # hash assigned to it so that no validation is # performed (as presumably no information has as # yet been entered - This is to be addressed as # pre- and post-validation definitions) 'validator' => Data::FormValidator->new({ '1' => {}, '2' => { 'optional' => [ qw/ organisation / ], 'required' => [ qw/ email firstname lastname / ] +, 'constraints' => { 'email' => 'email', 'firstname' => '/^[\w\s\-]+$/', 'lastname' => '/^[\w\s\-]+$/' }, 'filters' => [ qw/ trim / ] } }) } ); $app->run; exit 0; # The following is an example CGI::Application-inherited # class employed by this script - The form-entry method # provides the multiple form display and validation while # the setup and teardown methods handle most of the # definition and cleanup work. The final database # handling would be incorporated into the form_submission # routine. package Self; use base qw/ CGI::Application /; use strict; sub setup { my $self = shift; # Maintain CGI::Application default parameters if # none provided $self->run_modes( $self->param('run_modes'} || { 'start' => 'dump_ +html' } ); $self->mode_param( $self->param('mode_param') || 'rm' ); $self->start_mode( $self->param('start_mode') || 'start' ); } sub teardown { my $self = shift; # DBI handle clean up - See PM node 136736 foreach my $param ($self->params()) { $self->param($param)->disconnect if ref $self->param($param) e +q 'DBI'; } } sub form_entry { my $self = shift; my $cgi = $self->query; # The following definition of the mode_param is # required in cases where this method may be called # via the AUTOLOAD dispatch run mode. $cgi->param( -name => $self->param('mode_param'), -value => $self->param('start_mode') ) unless defined $cgi->param($self->param('mode_param')); # Although the default template module with # CGI::Application is HTML::Template, there is # nothing to prevent the use of Template Toolkit - # The easiest way is to initiate the Template object # in place of the CGI::Application/HTML::Template # load_tmpl method and returning the result of # $template->output( $file, $params ) in place of # $html->output. See PM node 142580. my $html = load_tmpl( $self->param('html')->{ $cgi->param($self->p +aram('mode_param')) }, { 'die_on_bad_params' => 0 }); my %param = map { $_ => $cgi->param($_) } grep { !/$self->param('m +ode_param')/ } $cgi->param(); my ($error_missing, $error_invalid) = ($self->param('validator')-> +validate(\%param, $cgi->param($self->param('mode_param'))))[1,2]; if (scalar ( @{$error_missing}, @{$error_invalid} )) { # Custom error dispatch for invalid or missing # parameters can be incorporated here - Normally # I have an error HTML template which push a # sorted list of friendly names for the erroreous # form fields into. } $html->param( $_ => $cgi->param($_) ) foreach $cgi->param(); return $html->output; } sub form_submission { my $self = shift; # Database handling and processing of completed # submission information would reside in this # subroutine } 1; __END__

 


In reply to Re: Re: Re: More on Web Application Frameworks by rob_au
in thread More on Web Application Frameworks by impossiblerobot

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found