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