added dependency to Text::ASCITable for some eyecandy in logs,
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
CommitLineData
fc7ec1d9 1package Catalyst;
2
3use strict;
ac733264 4use base 'Catalyst::Base';
fc7ec1d9 5use UNIVERSAL::require;
6use Catalyst::Log;
0f7ecc53 7use Text::ASCIITable;
fc7ec1d9 8
ac733264 9__PACKAGE__->mk_classdata($_) for qw/engine log/;
fc7ec1d9 10
87e67021 11our $VERSION = '5.00';
fc7ec1d9 12our @ISA;
13
14=head1 NAME
15
16Catalyst - The Elegant MVC Web Application Framework
17
18=head1 SYNOPSIS
19
20 # use the helper to start a new application
91864987 21 catalyst.pl MyApp
fc7ec1d9 22 cd MyApp
23
24 # add models, views, controllers
d01df17d 25 script/create.pl model Something
26 script/create.pl view Stuff
27 script/create.pl controller Yada
fc7ec1d9 28
29 # built in testserver
d01df17d 30 script/server.pl
fc7ec1d9 31
32 # command line interface
d01df17d 33 script/test.pl /yada
fc7ec1d9 34
35
fc7ec1d9 36 use Catalyst;
37
38 use Catalyst qw/My::Module My::OtherModule/;
39
40 use Catalyst '-Debug';
41
42 use Catalyst qw/-Debug -Engine=CGI/;
43
5a8ed4fe 44 sub default : Private { $_[1]->res->output('Hello') } );
45
e3dc9d78 46 sub index : Path('/index.html') {
5a8ed4fe 47 my ( $self, $c ) = @_;
48 $c->res->output('Hello');
49 $c->forward('_foo');
50 }
51
52 sub product : Regex('/^product[_]*(\d*).html$/') {
53 my ( $self, $c ) = @_;
54 $c->stash->{template} = 'product.tt';
55 $c->stash->{product} = $c->req->snippets->[0];
56 }
fc7ec1d9 57
3803e98f 58See also L<Catalyst::Manual::Intro>
59
fc7ec1d9 60=head1 DESCRIPTION
61
62Catalyst is based upon L<Maypole>, which you should consider for smaller
63projects.
64
65The key concept of Catalyst is DRY (Don't Repeat Yourself).
66
67See L<Catalyst::Manual> for more documentation.
68
23f9d934 69Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
1985c30b 70Omit the C<Catalyst::Plugin::> prefix from the plugin name,
23f9d934 71so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
fc7ec1d9 72
73 use Catalyst 'My::Module';
74
23f9d934 75Special flags like -Debug and -Engine can also be specifed as arguments when
76Catalyst is loaded:
fc7ec1d9 77
78 use Catalyst qw/-Debug My::Module/;
79
23f9d934 80The position of plugins and flags in the chain is important, because they are
81loaded in exactly the order that they appear.
fc7ec1d9 82
23f9d934 83The following flags are supported:
84
85=over 4
86
87=item -Debug
88
89enables debug output, i.e.:
fc7ec1d9 90
91 use Catalyst '-Debug';
92
23f9d934 93this is equivalent to:
fc7ec1d9 94
95 use Catalyst;
96 sub debug { 1 }
97
23f9d934 98=item -Engine
fc7ec1d9 99
100Force Catalyst to use a specific engine.
23f9d934 101Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
fc7ec1d9 102
103 use Catalyst '-Engine=CGI';
104
23f9d934 105=back
fc7ec1d9 106
23f9d934 107=head1 METHODS
108
109=over 4
110
111=item debug
fc7ec1d9 112
113Overload to enable debug messages.
114
115=cut
116
117sub debug { 0 }
118
23f9d934 119=item config
fc7ec1d9 120
121Returns a hashref containing your applications settings.
122
123=cut
124
fc7ec1d9 125sub import {
126 my ( $self, @options ) = @_;
127 my $caller = caller(0);
128
22402712 129 unless ( $caller->isa($self) ) {
fc7ec1d9 130 no strict 'refs';
22402712 131 push @{"$caller\::ISA"}, $self;
1c99e125 132 }
133
d96e14c2 134 if ( $caller->engine ) {
ac733264 135 return; # Catalyst is allready initialized
d96e14c2 136 }
137
32620e3e 138 unless ( $caller->log ) {
139 $caller->log( Catalyst::Log->new );
fc7ec1d9 140 }
fc7ec1d9 141
1985c30b 142 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($caller) . '_DEBUG' } ) {
143 no strict 'refs';
144 *{"$caller\::debug"} = sub { 1 };
145 $caller->log->debug('Debug messages enabled');
146 }
147
91dc9907 148 my $engine = 'Catalyst::Engine::CGI';
6dc87a0f 149
150 if ( $ENV{MOD_PERL} ) {
151
152 require mod_perl;
153
154 if ( $mod_perl::VERSION >= 1.99 ) {
91dc9907 155 $engine = 'Catalyst::Engine::Apache::MP2';
6dc87a0f 156 }
157 else {
91dc9907 158 $engine = 'Catalyst::Engine::Apache::MP1';
6dc87a0f 159 }
160 }
1985c30b 161
937fcdd8 162 my @plugins;
fc7ec1d9 163 foreach (@options) {
164 if (/^\-Debug$/) {
1985c30b 165 next if $caller->debug;
fc7ec1d9 166 no strict 'refs';
1c99e125 167 *{"$caller\::debug"} = sub { 1 };
fc7ec1d9 168 $caller->log->debug('Debug messages enabled');
169 }
170 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
171 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
172 else {
173 my $plugin = "Catalyst::Plugin::$_";
174
c4695f3a 175 $plugin->require;
91dc9907 176
fc7ec1d9 177 if ($@) {
178 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
179 }
180 else {
91dc9907 181 push @plugins, " + $plugin";
502619e5 182 no strict 'refs';
183 push @{"$caller\::ISA"}, $plugin;
fc7ec1d9 184 }
185 }
186 }
0f7ecc53 187 my $t = Text::ASCIITable->new;
188 $t->setCols('Class');
189 $t->addRow($_) for @plugins;
190 $caller->log->debug( 'Loaded plugins', $t->draw )
937fcdd8 191 if ( @plugins && $caller->debug );
fc7ec1d9 192
193 # Engine
194 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
195 if $ENV{CATALYST_ENGINE};
d96e14c2 196
fc7ec1d9 197 $engine->require;
198 die qq/Couldn't load engine "$engine", "$@"/ if $@;
502619e5 199 {
200 no strict 'refs';
970cc51d 201 push @{"$caller\::ISA"}, $engine;
502619e5 202 }
70cb38f0 203 $caller->engine($engine);
fc7ec1d9 204 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
205}
206
70cb38f0 207=item $c->engine
208
209Contains the engine class.
210
145074c2 211=item $c->log
212
213Contains the logging object. Unless it is already set Catalyst sets this up with a
214C<Catalyst::Log> object. To use your own log class:
215
216 $c->log( MyLogger->new );
217 $c->log->info("now logging with my own logger!");
218
219Your log class should implement the methods described in the C<Catalyst::Log>
220man page.
221
222
23f9d934 223=back
224
3cb1db8c 225=head1 SUPPORT
226
227IRC:
228
229 Join #catalyst on irc.perl.org.
230
231Mailing-Lists:
232
233 http://lists.rawmode.org/mailman/listinfo/catalyst
234 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
1985c30b 235
fc7ec1d9 236=head1 SEE ALSO
237
238L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
239L<Catalyst::Response>, L<Catalyst::Engine>
240
241=head1 AUTHOR
242
243Sebastian Riedel, C<sri@oook.de>
244
245=head1 THANK YOU
246
bc024080 247Andrew Ford, Andrew Ruthven, Christian Hansen, Christopher Hicks,
b0b7c5e0 248Dan Sully, Danijel Milicevic, David Naughton, Gary Ashton Jones,
249Jesse Sheidlower, Johan Lindstrom, Marcus Ramberg, Tatsuhiko Miyagawa
250and all the others who've helped.
fc7ec1d9 251
252=head1 LICENSE
253
254This library is free software . You can redistribute it and/or modify it under
255the same terms as perl itself.
256
257=cut
258
2591;