4 use base 'Catalyst::Base';
5 use UNIVERSAL::require;
6 use Catalyst::Exception;
11 our $CATALYST_SCRIPT_GEN = 4;
13 __PACKAGE__->mk_classdata($_) for qw/dispatcher engine log/;
15 our $VERSION = '5.24';
20 Catalyst - The Elegant MVC Web Application Framework
24 # use the helper to start a new application
28 # add models, views, controllers
29 script/myapp_create.pl model Something
30 script/myapp_create.pl view Stuff
31 script/myapp_create.pl controller Yada
34 script/myapp_server.pl
36 # command line interface
37 script/myapp_test.pl /yada
42 use Catalyst qw/My::Module My::OtherModule/;
44 use Catalyst '-Debug';
46 use Catalyst qw/-Debug -Engine=CGI/;
48 sub default : Private { $_[1]->res->output('Hello') } );
50 sub index : Path('/index.html') {
51 my ( $self, $c ) = @_;
52 $c->res->output('Hello');
56 sub product : Regex('^product[_]*(\d*).html$') {
57 my ( $self, $c ) = @_;
58 $c->stash->{template} = 'product.tt';
59 $c->stash->{product} = $c->req->snippets->[0];
62 See also L<Catalyst::Manual::Intro>
66 The key concept of Catalyst is DRY (Don't Repeat Yourself).
68 See L<Catalyst::Manual> for more documentation.
70 Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
71 Omit the C<Catalyst::Plugin::> prefix from the plugin name,
72 so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
74 use Catalyst 'My::Module';
76 Special flags like -Debug and -Engine can also be specifed as arguments when
79 use Catalyst qw/-Debug My::Module/;
81 The position of plugins and flags in the chain is important, because they are
82 loaded in exactly the order that they appear.
84 The following flags are supported:
90 enables debug output, i.e.:
92 use Catalyst '-Debug';
94 this is equivalent to:
101 Force Catalyst to use a specific engine.
102 Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
104 use Catalyst '-Engine=CGI';
114 Overload to enable debug messages.
122 Returns a hashref containing your applications settings.
127 my ( $class, @arguments ) = @_;
128 my $caller = caller(0);
130 # Prepare inheritance
131 unless ( $caller->isa($class) ) {
133 push @{"$caller\::ISA"}, $class;
136 if ( $caller->engine ) {
138 unless ( $caller eq 'main' ) {
139 $caller->log->warn( qq/Attempt to re-initialize "$caller"/ );
148 foreach (@arguments) {
153 elsif (/^-(\w+)=?(.*)$/) {
154 $flags->{ lc $1 } = $2;
157 push @{ $flags->{plugins} }, $_;
161 $caller->setup_log ( delete $flags->{log} );
162 $caller->setup_plugins ( delete $flags->{plugins} );
163 $caller->setup_dispatcher ( delete $flags->{dispatcher} );
164 $caller->setup_engine ( delete $flags->{engine} );
165 $caller->setup_home ( delete $flags->{home} );
167 for my $flag ( sort keys %{ $flags } ) {
169 if ( my $code = $caller->can( 'setup_' . $flag ) ) {
170 &$code( $caller, delete $flags->{$flag} );
173 $caller->log->warn(qq/Unknown flag "$flag"/);
177 $caller->log->warn( "You are running an old helper script! "
178 . "Please update your scripts by regenerating the "
179 . "application and copying over the new scripts." )
180 if ( $ENV{CATALYST_SCRIPT_GEN}
181 && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) );
184 if ( $caller->debug ) {
190 @plugins = grep { /^Catalyst::Plugin/ } @{"$caller\::ISA"};
194 my $t = Text::ASCIITable->new;
195 $t->setOptions( 'hide_HeadRow', 1 );
196 $t->setOptions( 'hide_HeadLine', 1 );
197 $t->setCols('Class');
198 $t->setColWidth( 'Class', 75, 1 );
199 $t->addRow($_) for @plugins;
200 $caller->log->debug( 'Loaded plugins', $t->draw );
203 my $dispatcher = $caller->dispatcher;
204 my $engine = $caller->engine;
205 my $home = $caller->config->{home};
207 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/);
208 $caller->log->debug(qq/Loaded engine "$engine"/);
212 ? $caller->log->debug(qq/Found home "$home"/)
213 : $caller->log->debug(qq/Home "$home" doesn't exist/)
214 : $caller->log->debug(q/Couldn't find home/);
220 Contains the engine class.
224 Contains the logging object. Unless it is already set Catalyst sets this up with a
225 C<Catalyst::Log> object. To use your own log class:
227 $c->log( MyLogger->new );
228 $c->log->info("now logging with my own logger!");
230 Your log class should implement the methods described in the C<Catalyst::Log>
233 =item $c->plugin( $name, $class, @args )
235 Instant plugins for Catalyst.
236 Classdata accessor/mutator will be created, class loaded and instantiated.
238 MyApp->plugin( 'prototype', 'HTML::Prototype' );
240 $c->prototype->define_javascript_functions;
245 my ( $class, $name, $plugin, @args ) = @_;
248 if ( my $error = $UNIVERSAL::require::ERROR ) {
249 Catalyst::Exception->throw(
250 message => qq/Couldn't load instant plugin "$plugin", "$error"/
254 eval { $plugin->import };
255 $class->mk_classdata($name);
257 eval { $obj = $plugin->new(@args) };
260 Catalyst::Exception->throw(
261 message => qq/Couldn't instantiate instant plugin "$plugin", "$@"/
266 $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/)
270 =item $c->setup_dispatcher
274 sub setup_dispatcher {
275 my ( $class, $dispatcher ) = @_;
278 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
281 if ( $ENV{CATALYST_DISPATCHER} ) {
282 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
285 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
286 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
289 unless ( $dispatcher ) {
290 $dispatcher = 'Catalyst::Dispatcher';
293 $dispatcher->require;
296 Catalyst::Exception->throw(
297 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
303 push @{"$class\::ISA"}, $dispatcher;
306 $class->dispatcher($dispatcher);
309 =item $c->setup_engine
314 my ( $class, $engine ) = @_;
317 $engine = 'Catalyst::Engine::' . $engine;
320 if ( $ENV{CATALYST_ENGINE} ) {
321 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
324 if ( $ENV{ uc($class) . '_ENGINE' } ) {
325 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
328 if ( ! $engine && $ENV{MOD_PERL} ) {
330 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
333 $version =~ s/(\.[^.]+)\./$1/g;
335 if ( $software eq 'mod_perl') {
337 if ( $version >= 1.99922 ) {
339 $engine = 'Catalyst::Engine::Apache::MP20';
341 if ( Apache2::Request->require ) {
342 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
346 elsif ( $version >= 1.9901 ) {
348 $engine = 'Catalyst::Engine::Apache::MP19';
350 if ( Apache::Request->require ) {
351 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
355 elsif ( $version >= 1.24 ) {
357 $engine = 'Catalyst::Engine::Apache::MP13';
359 if ( Apache::Request->require ) {
360 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
365 Catalyst::Exception->throw(
366 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
371 elsif ( $software eq 'Zeus-Perl' ) {
372 $engine = 'Catalyst::Engine::Zeus';
376 Catalyst::Exception->throw(
377 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
383 $engine = 'Catalyst::Engine::CGI';
389 Catalyst::Exception->throw(
390 message => qq/Couldn't load engine "$engine", "$@"/
396 push @{"$class\::ISA"}, $engine;
399 $class->engine($engine);
407 my ( $class, $home ) = @_;
409 if ( $ENV{CATALYST_HOME} ) {
410 $home = $ENV{CATALYST_HOME};
413 if ( $ENV{ uc($class) . '_HOME' } ) {
414 $home = $ENV{ uc($class) . '_HOME' };
418 $home = Catalyst::Utils::home($class);
422 $class->config->{home} = $home;
423 $class->config->{root} = dir($home)->subdir('root');
432 my ( $class, $debug ) = @_;
434 unless ( $class->log ) {
435 $class->log( Catalyst::Log->new );
438 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
440 *{"$class\::debug"} = sub { 1 };
441 $class->log->debug('Debug messages enabled');
445 =item $c->setup_plugins
450 my ( $class, $plugins ) = @_;
452 for my $plugin ( @$plugins ) {
454 $plugin = "Catalyst::Plugin::$plugin";
459 Catalyst::Exception->throw(
460 message => qq/Couldn't load plugin "$plugin", "$@"/
466 push @{"$class\::ISA"}, $plugin;
475 mod_perl2 support is considered experimental and may contain bugs.
481 Join #catalyst on irc.perl.org.
485 http://lists.rawmode.org/mailman/listinfo/catalyst
486 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
490 http://catalyst.perl.org
496 =item L<Catalyst::Manual> - The Catalyst Manual
498 =item L<Catalyst::Engine> - Core Engine
500 =item L<Catalyst::Log> - The Log Class.
502 =item L<Catalyst::Request> - The Request Object
504 =item L<Catalyst::Response> - The Response Object
506 =item L<Catalyst::Test> - The test suite.
512 Sebastian Riedel, C<sri@oook.de>
516 Andy Grundman, Andrew Ford, Andrew Ruthven, Autrijus Tang, Christian Hansen,
517 Christopher Hicks, Dan Sully, Danijel Milicevic, David Naughton,
518 Gary Ashton Jones, Geoff Richards, Jesse Sheidlower, Jody Belka,
519 Johan Lindstrom, Juan Camacho, Leon Brocard, Marcus Ramberg,
520 Tatsuhiko Miyagawa and all the others who've helped.
524 This library is free software . You can redistribute it and/or modify it under
525 the same terms as perl itself.