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