CGI::Prototype, Part Two
Need to create a CGI application? Save time and lines of code with Randal’s new CGI::Prototype.
Wednesday, June 15th, 2005
Last time, I explained why I created my CGI::Prototype framework and I demonstrated the basics of creating an application. This month, let’s pick up where I last left off.
The basic class is fairly spartan:
sub activate {
eval {
my $self = shift;
my $this_page = $self->dispatch;
my $next_page = $this_page->respond;
$next_page->render;
};
$self->error($@) if $@;
}
Here, the application provides a dispatch() method to determine which page object should respond to the incoming parameters. That page object’s respond() method must also return a page object to render() the page that returns the results to the user.
Let’s look at how to implement a classic “two-pass” CGI application with the CGI::Prototype framework.
During the first pass, a CGI script responds with a form to fill out; in the second pass, it processes the submission of that form and generates a result. So, first, create MyApp.pm…
package MyApp;
use base CGI::Prototype;
… and then use it…
#!/usr/bin/perl
use MyApp;
MyApp->activate;
As stated last time, the default behavior simply prints “This page intentionally left blank”. (I’m omitting the 1; from these example modules for brevity.)
To continue, create page objects for the first and second passes. For the first pass, just override the template:
package MyApp::One;
use base MyApp;
sub template { ’the_form.tt’ }