4 use base 'Catalyst::Base';
5 use UNIVERSAL::require;
10 our $CATALYST_SCRIPT_GEN = 4;
12 __PACKAGE__->mk_classdata($_) for qw/dispatcher engine log/;
14 our $VERSION = '5.20';
19 Catalyst - The Elegant MVC Web Application Framework
23 # use the helper to start a new application
27 # add models, views, controllers
28 script/create.pl model Something
29 script/create.pl view Stuff
30 script/create.pl controller Yada
35 # command line interface
41 use Catalyst qw/My::Module My::OtherModule/;
43 use Catalyst '-Debug';
45 use Catalyst qw/-Debug -Engine=CGI/;
47 sub default : Private { $_[1]->res->output('Hello') } );
49 sub index : Path('/index.html') {
50 my ( $self, $c ) = @_;
51 $c->res->output('Hello');
55 sub product : Regex('^product[_]*(\d*).html$') {
56 my ( $self, $c ) = @_;
57 $c->stash->{template} = 'product.tt';
58 $c->stash->{product} = $c->req->snippets->[0];
61 See also L<Catalyst::Manual::Intro>
65 Catalyst is based upon L<Maypole>, which you should consider for smaller
68 The key concept of Catalyst is DRY (Don't Repeat Yourself).
70 See L<Catalyst::Manual> for more documentation.
72 Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
73 Omit the C<Catalyst::Plugin::> prefix from the plugin name,
74 so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
76 use Catalyst 'My::Module';
78 Special flags like -Debug and -Engine can also be specifed as arguments when
81 use Catalyst qw/-Debug My::Module/;
83 The position of plugins and flags in the chain is important, because they are
84 loaded in exactly the order that they appear.
86 The following flags are supported:
92 enables debug output, i.e.:
94 use Catalyst '-Debug';
96 this is equivalent to:
103 Force Catalyst to use a specific engine.
104 Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
106 use Catalyst '-Engine=CGI';
116 Overload to enable debug messages.
124 Returns a hashref containing your applications settings.
129 my ( $self, @options ) = @_;
130 my $caller = caller(0);
132 # Prepare inheritance
133 unless ( $caller->isa($self) ) {
135 push @{"$caller\::ISA"}, $self;
138 if ( $caller->engine ) {
139 return; # Catalyst is already initialized
142 unless ( $caller->log ) {
143 $caller->log( Catalyst::Log->new );
147 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($caller) . '_DEBUG' } ) {
149 *{"$caller\::debug"} = sub { 1 };
150 $caller->log->debug('Debug messages enabled');
153 my $engine = 'Catalyst::Engine::CGI';
154 my $dispatcher = 'Catalyst::Dispatcher';
157 if ( $ENV{MOD_PERL} ) {
161 if ( $ENV{MOD_PERL_API_VERSION} == 2 ) {
162 $engine = 'Catalyst::Engine::Apache::MP20';
164 elsif ( $mod_perl::VERSION >= 1.99 ) {
165 $engine = 'Catalyst::Engine::Apache::MP19';
168 $engine = 'Catalyst::Engine::Apache::MP13';
172 $caller->log->info( "You are running an old helper script! "
173 . "Please update your scripts by regenerating the "
174 . "application and copying over the new scripts." )
175 if ( $ENV{CATALYST_SCRIPT_GEN}
176 && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) );
183 next if $caller->debug;
185 *{"$caller\::debug"} = sub { 1 };
186 $caller->log->debug('Debug messages enabled');
189 elsif (/^-Dispatcher=(.*)$/) {
190 $dispatcher = "Catalyst::Dispatcher::$1";
193 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
194 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
197 my $plugin = "Catalyst::Plugin::$_";
201 if ($@) { die qq/Couldn't load plugin "$plugin", "$@"/ }
203 push @plugins, $plugin;
205 push @{"$caller\::ISA"}, $plugin;
212 my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } );
213 $t->setCols('Class');
214 $t->setColWidth( 'Class', 75, 1 );
215 $t->addRow($_) for @plugins;
216 $caller->log->debug( 'Loaded plugins', $t->draw )
217 if ( @plugins && $caller->debug );
220 $dispatcher = "Catalyst::Dispatcher::$ENV{CATALYST_DISPATCHER}"
221 if $ENV{CATALYST_DISPATCHER};
222 my $appdis = $ENV{ uc($caller) . '_DISPATCHER' };
223 $dispatcher = "Catalyst::Dispatcher::$appdis" if $appdis;
225 $dispatcher->require;
226 die qq/Couldn't load dispatcher "$dispatcher", "$@"/ if $@;
229 push @{"$caller\::ISA"}, $dispatcher;
231 $caller->dispatcher($dispatcher);
232 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/) if $caller->debug;
235 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
236 if $ENV{CATALYST_ENGINE};
237 my $appeng = $ENV{ uc($caller) . '_ENGINE' };
238 $engine = "Catalyst::Engine::$appeng" if $appeng;
241 die qq/Couldn't load engine "$engine", "$@"/ if $@;
244 push @{"$caller\::ISA"}, $engine;
246 $caller->engine($engine);
247 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
250 my $home = Catalyst::Utils::home($caller);
251 if ( $caller->debug ) {
254 ? $caller->log->debug(qq/Found home "$home"/)
255 : $caller->log->debug(qq/Home "$home" doesn't exist/)
256 : $caller->log->debug(q/Couldn't find home/);
258 $caller->config->{home} = $home || '';
259 $caller->config->{root} = defined $home ? dir($home)->subdir('root') : '';
264 Contains the engine class.
268 Contains the logging object. Unless it is already set Catalyst sets this up with a
269 C<Catalyst::Log> object. To use your own log class:
271 $c->log( MyLogger->new );
272 $c->log->info("now logging with my own logger!");
274 Your log class should implement the methods described in the C<Catalyst::Log>
277 =item $c->plugin( $name, $class, @args )
279 Instant plugins for Catalyst.
280 Classdata accessor/mutator will be created, class loaded and instantiated.
282 MyApp->plugin( 'prototype', 'HTML::Prototype' );
284 $c->prototype->define_javascript_functions;
289 my ( $class, $name, $plugin, @args ) = @_;
291 my $error = $UNIVERSAL::require::ERROR;
292 die qq/Couldn't load instant plugin "$plugin", "$error"/ if $error;
293 eval { $plugin->import };
294 $class->mk_classdata($name);
296 eval { $obj = $plugin->new(@args) };
297 die qq/Couldn't instantiate instant plugin "$plugin", "$@"/ if $@;
299 $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/)
307 mod_perl2 support is considered experimental and may contain bugs.
313 Join #catalyst on irc.perl.org.
317 http://lists.rawmode.org/mailman/listinfo/catalyst
318 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
322 http://catalyst.perl.org
328 =item L<Catalyst::Manual> - The Catalyst Manual
330 =item L<Catalyst::Engine> - Core Engine
332 =item L<Catalyst::Log> - The Log Class.
334 =item L<Catalyst::Request> - The Request Object
336 =item L<Catalyst::Response> - The Response Object
338 =item L<Catalyst::Test> - The test suite.
344 Sebastian Riedel, C<sri@oook.de>
348 Andy Grundman, Andrew Ford, Andrew Ruthven, Autrijus Tang, Christian Hansen,
349 Christopher Hicks, Dan Sully, Danijel Milicevic, David Naughton,
350 Gary Ashton Jones, Geoff Richards, Jesse Sheidlower, Jody Belka,
351 Johan Lindstrom, Leon Brocard, Marcus Ramberg, Tatsuhiko Miyagawa
352 and all the others who've helped.
356 This library is free software . You can redistribute it and/or modify it under
357 the same terms as perl itself.