2 use UNIVERSAL::require;
5 use Maypole::Constants;
15 die "Couldn't require $package - $@" if $@;
17 unshift @{ $package . "::ISA" }, "Maypole::CLI";
21 sub get_template_root { $ENV{MAYPOLE_TEMPLATES} || "." }
25 my $url = URI->new( shift @ARGV );
26 my $root = URI->new( $self->config->uri_base )->path;
27 $self->{path} = $url->path;
28 $self->{path} =~ s/^$root//i if $root;
31 $self->parse_args($url);
35 my ( $self, $url ) = @_;
36 $self->{params} = $url->query_form_hash;
37 $self->{query} = $url->query_form_hash;
40 sub send_output { $buffer = shift->{output} }
45 $package->handler() == OK and return $buffer;
50 if ( ( caller(0) )[1] eq "-e" ) {
51 $package->handler() == OK and print $buffer;
59 Maypole::CLI - Command line interface to Maypole for testing and debugging
63 % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
64 % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
68 This module is used to test Maypole sites without going through a web
69 server or modifying them to use a CGI frontend. To use it, you should
70 first either be in the template root for your Maypole site or set the
71 environment variable C<MAYPOLE_TEMPLATES> to the right value.
73 Next, you import the C<Maypole::CLI> module specifying your base Maypole
74 subclass. The usual way to do this is with the C<-M> flag:
75 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
77 use Maypole::CLI qw(MyApp);
79 Now Maypole will automatically call your application's handler with the
80 URL specified as the first command line parameter. This should be the
81 full URL, starting from whatever you have defined as the C<uri_base> in
82 your application's configuration, and may include query parameters.
84 The Maypole HTML output should then end up on standard output.
86 =head1 Support for testing
88 The module can also be used as part of a test script.
90 When used programmatically, rather than from the command line, its
91 behaviour is slightly different.
93 Although the URL is taken from C<@ARGV> as normal, your application's
94 C<handler> method is not called automatically, as it is when used on the
95 command line; you need to call it manually. Additionally, when
96 C<handler> is called, the output is not printed to standard output but
97 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
100 For instance, a test script could look like this:
102 use Test::More tests => 5;
103 use Maypole::CLI qw(BeerDB);
104 use Maypole::Constants;
105 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
107 # Hack because isa_ok only supports object isa not class isa
108 isa_ok( (bless {},"BeerDB") , "Maypole");
110 @ARGV = ("http://localhost/beerdb/");
111 is(BeerDB->handler, OK, "OK");
112 like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
114 @ARGV = ("http://localhost/beerdb/beer/list");
115 is(BeerDB->handler, OK, "OK");
116 like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");