4 use base qw[ Catalyst::Base Catalyst::Setup ];
5 use UNIVERSAL::require;
6 use Catalyst::Exception;
12 our $CATALYST_SCRIPT_GEN = 4;
14 __PACKAGE__->mk_classdata($_) for qw/arguments dispatcher engine log/;
16 our $VERSION = '5.24';
21 Catalyst - The Elegant MVC Web Application Framework
25 # use the helper to start a new application
29 # add models, views, controllers
30 script/myapp_create.pl model Something
31 script/myapp_create.pl view Stuff
32 script/myapp_create.pl controller Yada
35 script/myapp_server.pl
37 # command line interface
38 script/myapp_test.pl /yada
43 use Catalyst qw/My::Module My::OtherModule/;
45 use Catalyst '-Debug';
47 use Catalyst qw/-Debug -Engine=CGI/;
49 sub default : Private { $_[1]->res->output('Hello') } );
51 sub index : Path('/index.html') {
52 my ( $self, $c ) = @_;
53 $c->res->output('Hello');
57 sub product : Regex('^product[_]*(\d*).html$') {
58 my ( $self, $c ) = @_;
59 $c->stash->{template} = 'product.tt';
60 $c->stash->{product} = $c->req->snippets->[0];
63 See also L<Catalyst::Manual::Intro>
67 The key concept of Catalyst is DRY (Don't Repeat Yourself).
69 See L<Catalyst::Manual> for more documentation.
71 Catalyst plugins can be loaded by naming them as arguments to the "use Catalyst" statement.
72 Omit the C<Catalyst::Plugin::> prefix from the plugin name,
73 so C<Catalyst::Plugin::My::Module> becomes C<My::Module>.
75 use Catalyst 'My::Module';
77 Special flags like -Debug and -Engine can also be specifed as arguments when
80 use Catalyst qw/-Debug My::Module/;
82 The position of plugins and flags in the chain is important, because they are
83 loaded in exactly the order that they appear.
85 The following flags are supported:
91 enables debug output, i.e.:
93 use Catalyst '-Debug';
95 this is equivalent to:
102 Force Catalyst to use a specific engine.
103 Omit the C<Catalyst::Engine::> prefix of the engine name, i.e.:
105 use Catalyst '-Engine=CGI';
115 Overload to enable debug messages.
123 Returns a hashref containing your applications settings.
128 my ( $class, @arguments ) = @_;
130 my $caller = caller(0);
132 if ( $caller eq 'main' ) {
136 # Prepare inheritance
137 unless ( $caller->isa($class) ) {
139 push @{"$caller\::ISA"}, $class;
142 if ( $caller->engine ) {
143 $caller->log->warn( qq/Attempt to re-initialize "$caller"/ );
150 foreach (@arguments) {
153 $flags->{log} = ( $flags->{log} ) ? 'debug,' . $flags->{log} : 'debug';
155 elsif (/^-(\w+)=?(.*)$/) {
156 $flags->{ lc $1 } = $2;
159 push @{ $flags->{plugins} }, $_;
163 $caller->setup_log ( delete $flags->{log} );
164 $caller->setup_plugins ( delete $flags->{plugins} );
165 $caller->setup_dispatcher ( delete $flags->{dispatcher} );
166 $caller->setup_engine ( delete $flags->{engine} );
167 $caller->setup_home ( delete $flags->{home} );
169 for my $flag ( sort keys %{ $flags } ) {
171 if ( my $code = $caller->can( 'setup_' . $flag ) ) {
172 &$code( $caller, delete $flags->{$flag} );
175 $caller->log->warn(qq/Unknown flag "$flag"/);
179 $caller->log->warn( "You are running an old helper script! "
180 . "Please update your scripts by regenerating the "
181 . "application and copying over the new scripts." )
182 if ( $ENV{CATALYST_SCRIPT_GEN}
183 && ( $ENV{CATALYST_SCRIPT_GEN} < $CATALYST_SCRIPT_GEN ) );
186 if ( $caller->debug ) {
192 @plugins = grep { /^Catalyst::Plugin/ } @{"$caller\::ISA"};
196 my $t = Text::ASCIITable->new;
197 $t->setOptions( 'hide_HeadRow', 1 );
198 $t->setOptions( 'hide_HeadLine', 1 );
199 $t->setCols('Class');
200 $t->setColWidth( 'Class', 75, 1 );
201 $t->addRow($_) for @plugins;
202 $caller->log->debug( "Loaded plugins:\n" . $t->draw );
205 my $dispatcher = $caller->dispatcher;
206 my $engine = $caller->engine;
207 my $home = $caller->config->{home};
209 $caller->log->debug(qq/Loaded dispatcher "$dispatcher"/);
210 $caller->log->debug(qq/Loaded engine "$engine"/);
214 ? $caller->log->debug(qq/Found home "$home"/)
215 : $caller->log->debug(qq/Home "$home" doesn't exist/)
216 : $caller->log->debug(q/Couldn't find home/);
222 Contains the engine class.
226 Contains the logging object. Unless it is already set Catalyst sets this up with a
227 C<Catalyst::Log> object. To use your own log class:
229 $c->log( MyLogger->new );
230 $c->log->info("now logging with my own logger!");
232 Your log class should implement the methods described in the C<Catalyst::Log>
235 =item $c->plugin( $name, $class, @args )
237 Instant plugins for Catalyst.
238 Classdata accessor/mutator will be created, class loaded and instantiated.
240 MyApp->plugin( 'prototype', 'HTML::Prototype' );
242 $c->prototype->define_javascript_functions;
247 my ( $class, $name, $plugin, @args ) = @_;
250 if ( my $error = $UNIVERSAL::require::ERROR ) {
251 Catalyst::Exception->throw(
252 message => qq/Couldn't load instant plugin "$plugin", "$error"/
256 eval { $plugin->import };
257 $class->mk_classdata($name);
259 eval { $obj = $plugin->new(@args) };
262 Catalyst::Exception->throw(
263 message => qq/Couldn't instantiate instant plugin "$plugin", "$@"/
268 $class->log->debug(qq/Initialized instant plugin "$plugin" as "$name"/)
286 # Initialize our data structure
287 $class->components( {} );
289 $class->setup_components;
291 if ( $class->debug ) {
292 my $t = Text::ASCIITable->new;
293 $t->setOptions( 'hide_HeadRow', 1 );
294 $t->setOptions( 'hide_HeadLine', 1 );
295 $t->setCols('Class');
296 $t->setColWidth( 'Class', 75, 1 );
297 $t->addRow($_) for sort keys %{ $class->components };
298 $class->log->debug( "Loaded components:\n" . $t->draw )
299 if ( @{ $t->{tbl_rows} } );
302 # Add our self to components, since we are also a component
303 $class->components->{$class} = $class;
305 $class->setup_actions;
307 if ( $class->debug ) {
308 my $name = $class->config->{name} || 'Application';
309 $class->log->info("$name powered by Catalyst $Catalyst::VERSION");
317 mod_perl2 support is considered experimental and may contain bugs.
323 Join #catalyst on irc.perl.org.
327 http://lists.rawmode.org/mailman/listinfo/catalyst
328 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
332 http://catalyst.perl.org
338 =item L<Catalyst::Manual> - The Catalyst Manual
340 =item L<Catalyst::Engine> - Core Engine
342 =item L<Catalyst::Log> - The Log Class.
344 =item L<Catalyst::Request> - The Request Object
346 =item L<Catalyst::Response> - The Response Object
348 =item L<Catalyst::Test> - The test suite.
354 Sebastian Riedel, C<sri@oook.de>
358 Andy Grundman, Andrew Ford, Andrew Ruthven, Autrijus Tang, Christian Hansen,
359 Christopher Hicks, Dan Sully, Danijel Milicevic, David Naughton,
360 Gary Ashton Jones, Geoff Richards, Jesse Sheidlower, Jody Belka,
361 Johan Lindstrom, Juan Camacho, Leon Brocard, Marcus Ramberg,
362 Tatsuhiko Miyagawa and all the others who've helped.
366 This library is free software . You can redistribute it and/or modify it under
367 the same terms as perl itself.