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;
30 $self->parse_args($url);
34 my ( $self, $url ) = @_;
35 $self->{params} = $url->query_form_hash;
36 $self->{query} = $url->query_form_hash;
39 sub send_output { $buffer = shift->{output} }
44 $package->handler() == OK and return $buffer;
49 if ( ( caller(0) )[1] eq "-e" ) {
50 $package->handler() == OK and print $buffer;
58 Maypole::CLI - Command line interface to Maypole for testing and debugging
62 % setenv MAYPOLE_TEMPLATES /var/www/beerdb/
63 % perl -MMaypole::CLI=BeerDB -e1 http://localhost/beerdb/brewery/frontpage
67 This module is used to test Maypole sites without going through a web
68 server or modifying them to use a CGI frontend. To use it, you should
69 first either be in the template root for your Maypole site or set the
70 environment variable C<MAYPOLE_TEMPLATES> to the right value.
72 Next, you import the C<Maypole::CLI> module specifying your base Maypole
73 subclass. The usual way to do this is with the C<-M> flag:
74 C<perl -MMaypole::CLI=MyApp>. This is equivalent to:
76 use Maypole::CLI qw(MyApp);
78 Now Maypole will automatically call your application's handler with the
79 URL specified as the first command line parameter. This should be the
80 full URL, starting from whatever you have defined as the C<uri_base> in
81 your application's configuration, and may include query parameters.
83 The Maypole HTML output should then end up on standard output.
85 =head1 Support for testing
87 The module can also be used as part of a test script.
89 When used programmatically, rather than from the command line, its
90 behaviour is slightly different.
92 Although the URL is taken from C<@ARGV> as normal, your application's
93 C<handler> method is not called automatically, as it is when used on the
94 command line; you need to call it manually. Additionally, when
95 C<handler> is called, the output is not printed to standard output but
96 stored in C<$Maypole::CLI::buffer>, to allow you to check the contents
99 For instance, a test script could look like this:
101 use Test::More tests => 5;
102 use Maypole::CLI qw(BeerDB);
103 use Maypole::Constants;
104 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
106 # Hack because isa_ok only supports object isa not class isa
107 isa_ok( (bless {},"BeerDB") , "Maypole");
109 @ARGV = ("http://localhost/beerdb/");
110 is(BeerDB->handler, OK, "OK");
111 like($Maypole::CLI::buffer, qr/frontpage/, "Got the front page");
113 @ARGV = ("http://localhost/beerdb/beer/list");
114 is(BeerDB->handler, OK, "OK");
115 like($Maypole::CLI::buffer, qr/Organic Best/, "Found a beer in the list");