2 use UNIVERSAL::require;
5 use Maypole::Constants;
14 if ( ( caller(0) )[1] eq "-e" ) {
15 $package->handler() == OK and print $buffer;
22 die "Couldn't require $package - $@" if $@;
24 unshift @{ $package . "::ISA" }, "Maypole::CLI";
27 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
30 my ($self,@args) = @_;
31 my ($package, $line) = (caller)[0,2];
32 warn "[$package line $line] ", @args ;
38 my $url = URI->new( shift @ARGV );
39 (my $uri_base = $self->config->uri_base) =~ s:/$::;
40 my $root = URI->new( $uri_base )->path;
41 $self->{path} = $url->path;
42 $self->{path} =~ s:^$root/?::i if $root;
44 $self->parse_args($url);
48 my ( $self, $url ) = @_;
49 $self->{params} = $url->query_form_hash;
50 $self->{query} = $url->query_form_hash;
53 sub send_output { $buffer = shift->{output} }
58 $package->handler() == OK and return $buffer;
66 Maypole::CLI - Command line interface to Maypole for testing and debugging
70 % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
71 % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
75 This module is used to test Maypole sites without going through a web
76 server or modifying them to use a CGI frontend. To use it, you should
77 first either be in the template root for your Maypole site or set the
78 environment variable C<MAYPOLE_TEMPLATES> to the right value.
80 Next, you import the C<Maypole::CLI> module specifying your base Maypole
81 subclass. The usual way to do this is with the C<-M> flag:
82 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
84 use Maypole::CLI qw(MyApp);
86 Now Maypole will automatically call your application's handler with the
87 URL specified as the first command line parameter. This should be the
88 full URL, starting from whatever you have defined as the C<uri_base> in
89 your application's configuration, and may include query parameters.
91 The Maypole HTML output should then end up on standard output.
93 =head1 Support for testing
95 The module can also be used as part of a test script.
97 When used programmatically, rather than from the command line, its
98 behaviour is slightly different.
100 Although the URL is taken from C<@ARGV> as normal, your application's
101 C<handler> method is not called automatically, as it is when used on the
102 command line; you need to call it manually. Additionally, when
103 C<handler> is called, the output is not printed to standard output but
104 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
107 For instance, a test script could look like this:
109 use Test::More tests => 3;
110 use Maypole::CLI qw(BeerDB);
111 use Maypole::Constants;
112 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
114 # Hack because isa_ok only supports object isa not class isa
115 isa_ok( (bless {},"BeerDB") , "Maypole");
117 like(BeerDB->call_url("http://localhost/beerdb/"), qr/frontpage/, "Got the front page");
119 like(BeerDB->call_url("http://localhost/beerdb/beer/list"), qr/Organic Best/, "Found a beer in the list");
127 for use in scripts. takes an url as argument, and returns the buffer.
132 =head1 Implementation
134 This class overrides a set of methods in the base Maypole class to provide it's
135 functionality. See L<Maypole> for these:
139 =item get_template_root