[Maypole] CGI::Maypole

Dave Ranney dave@sialia.com
Fri, 5 Mar 2004 01:40:52 -0800


All,

Here's a first draft of a CGI::Maypole. After Simon's latest changes to 
Maypole.pm, there were only a few minor modifications to Apache::MVC 
necessary to create this. Note that it will only work with the latest 
code from CVS.

To use it, you'd just create a simple CGI script, something like this:

     ## file: beer.cgi
     #!/usr/bin/perl -w
     use strict;
     use BeerDB;
     BeerDB->run();

The BeerDB.pm example module that's included with the Maypole 
distribution requires only a couple of tweaks to work under 
CGI::Maypole: subclass from CGI::Maypole instead of Apache::MVC 
(obviously), and change BeerDB->config->{uri_base} to something that 
makes sense, e.g. "http://localhost/cgi-bin/beer.cgi/"

The run() method is intended to be a wrapper around handler() for 
trapping status codes and errors. I envision trapping the common status 
codes in run() and printing appropriate headers. However, at this 
stage, there is still much work to be done to solidify Maypole's error 
handling before run() will really be useful for anything. So for now, 
it just calls handler().

Note also that the default view class, Maypole::View::TT, still relies 
on Apache::Constants, so any condition that results in a "Not Found" 
being returned from that class will likely result in an error. It is a 
simple matter though to create a custom view class that works better 
with CGI::Maypole.

I haven't tested it yet under Apache::Registry, but will soon.

Comments appreciated!

-Dave

############# Code below ##############

package CGI::Maypole;
use base 'Maypole';
use CGI::Simple;
use strict;
use warnings;
our $VERSION = "0.1";

sub run {
	my $self = shift;
	return $self->handler();
}

sub get_request {
	shift->{cgi} = CGI::Simple->new();
}

sub parse_location {
     my $self = shift;
     $self->{path} = $self->{cgi}->url(-absolute=>1, -path_info=>1);
     my $loc = $self->{cgi}->url(-absolute=>1);
     no warnings 'uninitialized';
     $self->{path} =~ s/^($loc)?\///;
     $self->{path} ||= "frontpage";
     my @pi = split /\//, $self->{path};
     shift @pi while @pi and !$pi[0];
     $self->{table} = shift @pi;
     $self->{action} = shift @pi;
     $self->{args} = \@pi;	
     $self->{params} = { $self->{cgi}->Vars };
     $self->{query}  = { $self->{cgi}->Vars };
}

sub send_output {
     my $r = shift;	
	print $r->{cgi}->header(-type => $r->{content_type},
					  -content_length => length $r->{output},
					  );
     print $r->{output};
}

sub get_template_root {
     my $r = shift;
     $r->{cgi}->document_root . "/". $r->{cgi}->url(-relative=>1);
}


1;

=head1 NAME

CGI::Maypole - CGI-based front-end to Maypole

=head1 SYNOPSIS

     package BeerDB;
     use base 'CGI::Maypole;
     BeerDB->setup("dbi:mysql:beerdb");
     BeerDB->config->{uri_base} = "http://your.site/cgi-bin/beer.cgi/";
     BeerDB->config->{display_tables} = [qw[beer brewery pub style]];
     # Now set up your database:
     # has-a relationships
     # untaint columns

     1;

     ## example beer.cgi:
	
     #!/usr/bin/perl -w
     use strict;
     use BeerDB;
     BeerDB->run();

########## End Code ###########

------

Dave Ranney
Ladera Ranch, Orange County, CA
dave@sialia.com
CA Birding Lists Digest: http://www.sialia.com/s/calists.pl