Maypole
view release on metacpan or search on metacpan
lib/Maypole/CLI.pm view on Meta::CPAN
package Maypole::CLI;
use UNIVERSAL::require;
use URI;
use URI::QueryParam;
use Maypole::Constants;
use strict;
use warnings;
my $package;
our $buffer;
# Command line action
CHECK {
if ( ( caller(0) )[1] eq "-e" ) {
$package->handler() == OK and print $buffer;
}
}
sub import {
$package = $_[1];
$package->require;
die "Couldn't require $package - $@" if $@;
no strict 'refs';
unshift @{ $package . "::ISA" }, "Maypole::CLI";
}
sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
sub warn {
my ($self,@args) = @_;
my ($package, $line) = (caller)[0,2];
warn "[$package line $line] ", @args ;
return;
}
sub parse_location {
my $self = shift;
my $url = URI->new( shift @ARGV );
$self->preprocess_location();
(my $uri_base = $self->config->uri_base) =~ s:/$::;
my $root = URI->new( $uri_base )->path;
$self->{path} = $url->path;
$self->{path} =~ s:^$root/?::i if $root;
$self->parse_path;
$self->parse_args($url);
}
sub parse_args {
my ( $self, $url ) = @_;
$self->{params} = $url->query_form_hash;
$self->{query} = $url->query_form_hash;
}
sub send_output { $buffer = shift->{output} }
sub call_url {
my $self = shift;
local @ARGV = @_;
$package->handler() == OK and return $buffer;
}
1;
=head1 NAME
Maypole::CLI - Command line interface to Maypole for testing and debugging
=head1 SYNOPSIS
% setenv MAYPOLE_TEMPLATES /var/www/beerdb/
% perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
=head1 DESCRIPTION
This module is used to test Maypole sites without going through a web
server or modifying them to use a CGI frontend. To use it, you should
first either be in the template root for your Maypole site or set the
environment variable C<MAYPOLE_TEMPLATES> to the right value.
Next, you import the C<Maypole::CLI> module specifying your base Maypole
subclass. The usual way to do this is with the C<-M> flag:
C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
use Maypole::CLI qw(MyApp);
Now Maypole will automatically call your application's handler with the
URL specified as the first command line parameter. This should be the
full URL, starting from whatever you have defined as the C<uri_base> in
( run in 0.888 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )