updated absolute/relative tests/classes to global/local.
[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
424b2705 9__PACKAGE__->mk_classdata($_) for qw/dispatcher 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
424b2705 148 my $engine = 'Catalyst::Engine::CGI';
149 my $dispatcher = 'Catalyst::Dispatcher';
6dc87a0f 150
151 if ( $ENV{MOD_PERL} ) {
152
153 require mod_perl;
154
155 if ( $mod_perl::VERSION >= 1.99 ) {
91dc9907 156 $engine = 'Catalyst::Engine::Apache::MP2';
6dc87a0f 157 }
158 else {
91dc9907 159 $engine = 'Catalyst::Engine::Apache::MP1';
6dc87a0f 160 }
161 }
1985c30b 162
937fcdd8 163 my @plugins;
fc7ec1d9 164 foreach (@options) {
165 if (/^\-Debug$/) {
1985c30b 166 next if $caller->debug;
fc7ec1d9 167 no strict 'refs';
1c99e125 168 *{"$caller\::debug"} = sub { 1 };
fc7ec1d9 169 $caller->log->debug('Debug messages enabled');
170 }
424b2705 171 elsif (/^-Dispatcher=(.*)$/) {
172 $dispatcher = "Catalyst::Dispatcher::$1";
173 }
fc7ec1d9 174 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
175 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
176 else {
177 my $plugin = "Catalyst::Plugin::$_";
178
c4695f3a 179 $plugin->require;
91dc9907 180
fc7ec1d9 181 if ($@) {
182 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
183 }
184 else {
f5f84847 185 push @plugins, $plugin;
502619e5 186 no strict 'refs';
187 push @{"$caller\::ISA"}, $plugin;
fc7ec1d9 188 }
189 }
190 }
d2d570d4 191 my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } );
0f7ecc53 192 $t->setCols('Class');
0822f9a4 193 $t->setColWidth( 'Class', 75, 1 );
cd677e12 194 $t->addRow($_) for @plugins;
0f7ecc53 195 $caller->log->debug( 'Loaded plugins', $t->draw )
937fcdd8 196 if ( @plugins && $caller->debug );
fc7ec1d9 197
198 # Engine
199 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
200 if $ENV{CATALYST_ENGINE};
d96e14c2 201
fc7ec1d9 202 $engine->require;
203 die qq/Couldn't load engine "$engine", "$@"/ if $@;
502619e5 204 {
205 no strict 'refs';
970cc51d 206 push @{"$caller\::ISA"}, $engine;
502619e5 207 }
70cb38f0 208 $caller->engine($engine);
fc7ec1d9 209 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
424b2705 210
211 # Dispatcher
212 $dispatcher = "Catalyst::Dispatcher::$ENV{CATALYST_DISPATCHER}"
213 if $ENV{CATALYST_DISPATCHER};
214
215 $dispatcher->require;
216 die qq/Couldn't load dispatcher "$dispatcher", "$@"/ if $@;
217 {
218 no strict 'refs';
219 push @{"$caller\::ISA"}, $dispatcher;
220 }
221 $caller->dispatcher($dispatcher);
222 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/) if $caller->debug;
223
fc7ec1d9 224}
225
70cb38f0 226=item $c->engine
227
228Contains the engine class.
229
145074c2 230=item $c->log
231
232Contains the logging object. Unless it is already set Catalyst sets this up with a
233C<Catalyst::Log> object. To use your own log class:
234
235 $c->log( MyLogger->new );
236 $c->log->info("now logging with my own logger!");
237
238Your log class should implement the methods described in the C<Catalyst::Log>
239man page.
240
241
23f9d934 242=back
243
3cb1db8c 244=head1 SUPPORT
245
246IRC:
247
248 Join #catalyst on irc.perl.org.
249
250Mailing-Lists:
251
252 http://lists.rawmode.org/mailman/listinfo/catalyst
253 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
1985c30b 254
fc7ec1d9 255=head1 SEE ALSO
256
61b1e958 257=over 4
258
259=item L<Catalyst::Manual> - The Catalyst Manual
260
261=item L<Catalyst::Engine> - Core Engine
262
263=item L<Catalyst::Log> - The Log Class.
264
265=item L<Catalyst::Request> - The Request Object
266
267=item L<Catalyst::Response> - The Response Object
268
269=item L<Catalyst::Test> - The test suite.
270
271=back
fc7ec1d9 272
273=head1 AUTHOR
274
275Sebastian Riedel, C<sri@oook.de>
276
277=head1 THANK YOU
278
bc024080 279Andrew Ford, Andrew Ruthven, Christian Hansen, Christopher Hicks,
b0b7c5e0 280Dan Sully, Danijel Milicevic, David Naughton, Gary Ashton Jones,
281Jesse Sheidlower, Johan Lindstrom, Marcus Ramberg, Tatsuhiko Miyagawa
282and all the others who've helped.
fc7ec1d9 283
284=head1 LICENSE
285
286This library is free software . You can redistribute it and/or modify it under
287the same terms as perl itself.
288
289=cut
290
2911;