x
Loading
 Loading
Hello, Guest | Login | Register

CGI::Prototype, Part Two

Need to create a CGI application? Save time and lines of code with Randal’s new CGI::Prototype.

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’ }
If there are any incoming paramaters, dispatch to the second pass. Because Template Toolkit code can access the CGI object (more on this later), there’s still no code to write! So, the second pass looks…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Helpful Tools for Software Developers
  2. The Github Hall of Fame
  3. Book'em, Github.
  4. This Week on Github: Stupid Ruby Tricks
  5. A Veritable Scatter Shot!
Follow Linux Magazine
Rackspace