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