fixed helper
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
CommitLineData
fc7ec1d9 1package Catalyst;
2
3use strict;
4use base 'Class::Data::Inheritable';
5use UNIVERSAL::require;
6use Catalyst::Log;
7
8__PACKAGE__->mk_classdata($_) for qw/_config log/;
9
0556eb49 10our $VERSION = '4.30';
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.
73Omit the C<Catalyst::Plugin::> prefix from the plugin name,
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
128sub config {
129 my $self = shift;
130 $self->_config( {} ) unless $self->_config;
131 if ( $_[0] ) {
132 my $config = $_[1] ? {@_} : $_[0];
133 while ( my ( $key, $val ) = each %$config ) {
134 $self->_config->{$key} = $val;
135 }
136 }
137 return $self->_config;
138}
139
140sub import {
141 my ( $self, @options ) = @_;
142 my $caller = caller(0);
143
144 # Class
145 {
146 no strict 'refs';
147 *{"$caller\::handler"} =
148 sub { Catalyst::Engine::handler( $caller, @_ ) };
149 push @{"$caller\::ISA"}, $self;
150 }
151 $self->log( Catalyst::Log->new );
152
153 # Options
154 my $engine =
155 $ENV{MOD_PERL} ? 'Catalyst::Engine::Apache' : 'Catalyst::Engine::CGI';
156 foreach (@options) {
157 if (/^\-Debug$/) {
158 no warnings;
159 no strict 'refs';
160 *{"$self\::debug"} = sub { 1 };
161 $caller->log->debug('Debug messages enabled');
162 }
163 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
164 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
165 else {
166 my $plugin = "Catalyst::Plugin::$_";
167
168 # Plugin caller should be our application class
169 eval "package $caller; require $plugin";
170 if ($@) {
171 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
172 }
173 else {
174 $caller->log->debug(qq/Loaded plugin "$plugin"/)
175 if $caller->debug;
502619e5 176 no strict 'refs';
177 push @{"$caller\::ISA"}, $plugin;
fc7ec1d9 178 }
179 }
180 }
181
182 # Engine
183 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
184 if $ENV{CATALYST_ENGINE};
185 $engine->require;
186 die qq/Couldn't load engine "$engine", "$@"/ if $@;
502619e5 187 {
188 no strict 'refs';
189 push @{"$caller\::ISA"}, $engine;
190 }
fc7ec1d9 191 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
192}
193
23f9d934 194=back
195
3cb1db8c 196=head1 SUPPORT
197
198IRC:
199
200 Join #catalyst on irc.perl.org.
201
202Mailing-Lists:
203
204 http://lists.rawmode.org/mailman/listinfo/catalyst
205 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
206
fc7ec1d9 207=head1 SEE ALSO
208
209L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
210L<Catalyst::Response>, L<Catalyst::Engine>
211
212=head1 AUTHOR
213
214Sebastian Riedel, C<sri@oook.de>
215
216=head1 THANK YOU
217
bc024080 218Andrew Ford, Andrew Ruthven, Christian Hansen, Christopher Hicks,
219Danijel Milicevic, David Naughton, Gary Ashton Jones, Jesse Sheidlower,
220Johan Lindstrom, Marcus Ramberg, Tatsuhiko Miyagawa and all the others
221who've helped.
fc7ec1d9 222
223=head1 LICENSE
224
225This library is free software . You can redistribute it and/or modify it under
226the same terms as perl itself.
227
228=cut
229
2301;