new release
[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
3803e98f 10our $VERSION = '4.24';
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
72Omit the Catalyst::Plugin:: prefix from plugins.
73So Catalyst::Plugin::My::Module becomes My::Module.
74
75 use Catalyst 'My::Module';
76
77You can also set special flags like -Debug and -Engine.
78
79 use Catalyst qw/-Debug My::Module/;
80
81The position of plugins and flags in the chain is important,
82because they are loaded in the same order they appear.
83
84=head2 -Debug
85
86 use Catalyst '-Debug';
87
88is equivalent to
89
90 use Catalyst;
91 sub debug { 1 }
92
93=head2 -Engine
94
95Force Catalyst to use a specific engine.
96Omit the Catalyst::Engine:: prefix.
97
98 use Catalyst '-Engine=CGI';
99
100=head2 METHODS
101
102=head3 debug
103
104Overload to enable debug messages.
105
106=cut
107
108sub debug { 0 }
109
110=head3 config
111
112Returns a hashref containing your applications settings.
113
114=cut
115
116sub config {
117 my $self = shift;
118 $self->_config( {} ) unless $self->_config;
119 if ( $_[0] ) {
120 my $config = $_[1] ? {@_} : $_[0];
121 while ( my ( $key, $val ) = each %$config ) {
122 $self->_config->{$key} = $val;
123 }
124 }
125 return $self->_config;
126}
127
128sub import {
129 my ( $self, @options ) = @_;
130 my $caller = caller(0);
131
132 # Class
133 {
134 no strict 'refs';
135 *{"$caller\::handler"} =
136 sub { Catalyst::Engine::handler( $caller, @_ ) };
137 push @{"$caller\::ISA"}, $self;
138 }
139 $self->log( Catalyst::Log->new );
140
141 # Options
142 my $engine =
143 $ENV{MOD_PERL} ? 'Catalyst::Engine::Apache' : 'Catalyst::Engine::CGI';
144 foreach (@options) {
145 if (/^\-Debug$/) {
146 no warnings;
147 no strict 'refs';
148 *{"$self\::debug"} = sub { 1 };
149 $caller->log->debug('Debug messages enabled');
150 }
151 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
152 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
153 else {
154 my $plugin = "Catalyst::Plugin::$_";
155
156 # Plugin caller should be our application class
157 eval "package $caller; require $plugin";
158 if ($@) {
159 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
160 }
161 else {
162 $caller->log->debug(qq/Loaded plugin "$plugin"/)
163 if $caller->debug;
164 unshift @ISA, $plugin;
165 }
166 }
167 }
168
169 # Engine
170 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
171 if $ENV{CATALYST_ENGINE};
172 $engine->require;
173 die qq/Couldn't load engine "$engine", "$@"/ if $@;
174 push @ISA, $engine;
175 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
176}
177
3cb1db8c 178=head1 SUPPORT
179
180IRC:
181
182 Join #catalyst on irc.perl.org.
183
184Mailing-Lists:
185
186 http://lists.rawmode.org/mailman/listinfo/catalyst
187 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
188
fc7ec1d9 189=head1 SEE ALSO
190
191L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
192L<Catalyst::Response>, L<Catalyst::Engine>
193
194=head1 AUTHOR
195
196Sebastian Riedel, C<sri@oook.de>
197
198=head1 THANK YOU
199
3803e98f 200Andrew Ruthven, Danijel Milicevic, David Naughton, Gary Ashton Jones,
201Jesse Sheidlower, Johan Lindstrom, Marcus Ramberg and all the others
202who've helped.
fc7ec1d9 203
204=head1 LICENSE
205
206This library is free software . You can redistribute it and/or modify it under
207the same terms as perl itself.
208
209=cut
210
2111;