Perl/CGI
From OpenWetWare
Modules
CGI::Application
- builds upon CGI adding a structure for writing truly reusable Web-applications
- "run-mode" - single screen of an application
- "Mode Parameter" is used to store (and retrieve) the current run-mode of your application
- maps each run-mode to a specific Perl subroutine ("Run-Mode Method") that implements the behavior of a single run-mode
- an "abstract class", and is only used via inheritance
package Your::Web::Application; use base 'CGI::Application';
- setup() method defines a map between run-modes and run-mode methods
- run-mode methods are responsible for setting up the HTTP and HTML output
- run-mode methods should never print() anything to STDOUT
- run() method is singularly responsible for actually sending all HTTP headers and HTML content to the Web browser
- your run-mode method is called by the run() method, and your code is expected to return a scalar containing all your HTML content
- header_type() and header_props() allow to change the default HTTP headers
- "Instance Script" manages a single "instance" of your "Application Module"
- widgetview.cgi - instance script
#!/usr/bin/perl -w use WidgetView; my $app = WidgetView->new(); $app->run();
- WidgetView.pm - application module, must be in Perl's search path (@INC)
package WidgetView; use base 'CGI::Application'; use strict; # Needed for our database connection use DBI; sub setup { my $self = shift; $self->start_mode('mode1'); $self->run_modes( 'mode1' => 'showform', 'mode2' => 'showlist', 'mode3' => 'showdetail' ); # Connect to DBI database $self->param('mydbh' => DBI->connect()); } sub teardown { my $self = shift; # Disconnect when we're done $self->param('mydbh')->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->param('mydbh'); # 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->param('mydbh'); # 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; }
References