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 ) {
137 $caller->log->warn( qq/Attempt to re-initialize "$caller"/ );
144 foreach (@arguments) {
149 elsif (/^-(\w+)=?(.*)$/) {
150 $flags->{ lc $1 } = $2;
153 push @{ $flags->{plugins} }, $_;
157 $caller->setup_log ( delete $flags->{log} );
158 $caller->setup_plugins ( delete $flags->{plugins} );
159 $caller->setup_dispatcher ( delete $flags->{dispatcher} );
160 $caller->setup_engine ( delete $flags->{engine} );
161 $caller->setup_home ( delete $flags->{home} );
163 for my $flag ( sort keys %{ $flags } ) {
165 if ( my $code = $caller->can( 'setup_' . $flag ) ) {
166 &$code( $caller, delete $flags->{$flag} );
169 $caller->log->warn(qq/Unknown flag "$flag"/);
173 $caller->log->warn( "You are running an old helper script! "
174 . "Please update your scripts by regenerating the "
175 . "application and copying over the new scripts." )
176 if ( $ENV{CATALYST_SCRIPT_GEN}
177 && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) );
180 if ( $caller->debug ) {
186 @plugins = grep { /^Catalyst::Plugin/ } @{"$caller\::ISA"};
190 my $t = Text::ASCIITable->new;
191 $t->setOptions( 'hide_HeadRow', 1 );
192 $t->setOptions( 'hide_HeadLine', 1 );
193 $t->setCols('Class');
194 $t->setColWidth( 'Class', 75, 1 );
195 $t->addRow($_) for @plugins;
196 $caller->log->debug( 'Loaded plugins', $t->draw );
199 my $dispatcher = $caller->dispatcher;
200 my $engine = $caller->engine;
201 my $home = $caller->config->{home};
203 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/);
204 $caller->log->debug(qq/Loaded engine "$engine"/);
208 ? $caller->log->debug(qq/Found home "$home"/)
209 : $caller->log->debug(qq/Home "$home" doesn't exist/)
210 : $caller->log->debug(q/Couldn't find home/);
216 Contains the engine class.
220 Contains the logging object. Unless it is already set Catalyst sets this up with a
221 C<Catalyst::Log> object. To use your own log class:
223 $c->log( MyLogger->new );
224 $c->log->info("now logging with my own logger!");
226 Your log class should implement the methods described in the C<Catalyst::Log>
229 =item $c->plugin( $name, $class, @args )
231 Instant plugins for Catalyst.
232 Classdata accessor/mutator will be created, class loaded and instantiated.
234 MyApp->plugin( 'prototype', 'HTML::Prototype' );
236 $c->prototype->define_javascript_functions;
241 my ( $class, $name, $plugin, @args ) = @_;
244 if ( my $error = $UNIVERSAL::require::ERROR ) {
245 Catalyst::Exception->throw(
246 message => qq/Couldn't load instant plugin "$plugin", "$error"/
250 eval { $plugin->import };
251 $class->mk_classdata($name);
253 eval { $obj = $plugin->new(@args) };
256 Catalyst::Exception->throw(
257 message => qq/Couldn't instantiate instant plugin "$plugin", "$@"/
262 $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/)
266 =item $c->setup_dispatcher
270 sub setup_dispatcher {
271 my ( $class, $dispatcher ) = @_;
274 $dispatcher = 'Catalyst::Dispatcher::' . $dispatcher;
277 if ( $ENV{CATALYST_DISPATCHER} ) {
278 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{CATALYST_DISPATCHER};
281 if ( $ENV{ uc($class) . '_DISPATCHER' } ) {
282 $dispatcher = 'Catalyst::Dispatcher::' . $ENV{ uc($class) . '_DISPATCHER' };
285 unless ( $dispatcher ) {
286 $dispatcher = 'Catalyst::Dispatcher';
289 $dispatcher->require;
292 Catalyst::Exception->throw(
293 message => qq/Couldn't load dispatcher "$dispatcher", "$@"/
299 push @{"$class\::ISA"}, $dispatcher;
302 $class->dispatcher($dispatcher);
305 =item $c->setup_engine
310 my ( $class, $engine ) = @_;
313 $engine = 'Catalyst::Engine::' . $engine;
316 if ( $ENV{CATALYST_ENGINE} ) {
317 $engine = 'Catalyst::Engine::' . $ENV{CATALYST_ENGINE};
320 if ( $ENV{ uc($class) . '_ENGINE' } ) {
321 $engine = 'Catalyst::Engine::' . $ENV{ uc($class) . '_ENGINE' };
324 if ( ! $engine && $ENV{MOD_PERL} ) {
326 my ( $software, $version ) = $ENV{MOD_PERL} =~ /^(\S+)\/(\d+(?:[\.\_]\d+)+)/;
329 $version =~ s/(\.[^.]+)\./$1/g;
331 if ( $software eq 'mod_perl') {
333 if ( $version >= 1.99922 ) {
335 $engine = 'Catalyst::Engine::Apache::MP20';
337 if ( Apache2::Request->require ) {
338 $engine = 'Catalyst::Engine::Apache::MP20::Apreq';
342 elsif ( $version >= 1.9901 ) {
344 $engine = 'Catalyst::Engine::Apache::MP19';
346 if ( Apache::Request->require ) {
347 $engine = 'Catalyst::Engine::Apache::MP19::Apreq';
351 elsif ( $version >= 1.24 ) {
353 $engine = 'Catalyst::Engine::Apache::MP13';
355 if ( Apache::Request->require ) {
356 $engine = 'Catalyst::Engine::Apache::MP13::Apreq';
361 Catalyst::Exception->throw(
362 message => qq/Unsupported mod_perl version: $ENV{MOD_PERL}/
367 elsif ( $software eq 'Zeus-Perl' ) {
368 $engine = 'Catalyst::Engine::Zeus';
372 Catalyst::Exception->throw(
373 message => qq/Unsupported mod_perl: $ENV{MOD_PERL}/
379 $engine = 'Catalyst::Engine::CGI';
385 Catalyst::Exception->throw(
386 message => qq/Couldn't load engine "$engine", "$@"/
392 push @{"$class\::ISA"}, $engine;
395 $class->engine($engine);
403 my ( $class, $home ) = @_;
405 if ( $ENV{CATALYST_HOME} ) {
406 $home = $ENV{CATALYST_HOME};
409 if ( $ENV{ uc($class) . '_HOME' } ) {
410 $home = $ENV{ uc($class) . '_HOME' };
414 $home = Catalyst::Utils::home($class);
418 $class->config->{home} = $home;
419 $class->config->{root} = dir($home)->subdir('root');
428 my ( $class, $debug ) = @_;
430 unless ( $class->log ) {
431 $class->log( Catalyst::Log->new );
434 if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($class) . '_DEBUG' } || $debug ) {
436 *{"$class\::debug"} = sub { 1 };
437 $class->log->debug('Debug messages enabled');
441 =item $c->setup_plugins
446 my ( $class, $plugins ) = @_;
448 for my $plugin ( @$plugins ) {
450 $plugin = "Catalyst::Plugin::$plugin";
455 Catalyst::Exception->throw(
456 message => qq/Couldn't load plugin "$plugin", "$@"/
462 push @{"$class\::ISA"}, $plugin;
471 mod_perl2 support is considered experimental and may contain bugs.
477 Join #catalyst on irc.perl.org.
481 http://lists.rawmode.org/mailman/listinfo/catalyst
482 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
486 http://catalyst.perl.org
492 =item L<Catalyst::Manual> - The Catalyst Manual
494 =item L<Catalyst::Engine> - Core Engine
496 =item L<Catalyst::Log> - The Log Class.
498 =item L<Catalyst::Request> - The Request Object
500 =item L<Catalyst::Response> - The Response Object
502 =item L<Catalyst::Test> - The test suite.
508 Sebastian Riedel, C<sri@oook.de>
512 Andy Grundman, Andrew Ford, Andrew Ruthven, Autrijus Tang, Christian Hansen,
513 Christopher Hicks, Dan Sully, Danijel Milicevic, David Naughton,
514 Gary Ashton Jones, Geoff Richards, Jesse Sheidlower, Jody Belka,
515 Johan Lindstrom, Juan Camacho, Leon Brocard, Marcus Ramberg,
516 Tatsuhiko Miyagawa and all the others who've helped.
520 This library is free software . You can redistribute it and/or modify it under
521 the same terms as perl itself.