Added CDBI helper and added some documentation
[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
03a53815 10our $VERSION = '4.01';
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
20 catalyst MyApp
21 cd MyApp
22
23 # add models, views, controllers
24 bin/create model Something
25 bin/create view Stuff
26 bin/create controller Yada
27
28 # built in testserver
29 bin/server
30
31 # command line interface
32 bin/test /yada
33
34
35 See also L<Catalyst::Manual::Intro>
36
37
38 use Catalyst;
39
40 use Catalyst qw/My::Module My::OtherModule/;
41
42 use Catalyst '-Debug';
43
44 use Catalyst qw/-Debug -Engine=CGI/;
45
46 __PACKAGE__->action( '!default' => sub { $_[1]->res->output('Hello') } );
47
48 __PACKAGE__->action(
49 'index.html' => sub {
50 my ( $self, $c ) = @_;
51 $c->res->output('Hello');
52 $c->forward('_foo');
53 }
54 );
55
56 __PACKAGE__->action(
57 '/^product[_]*(\d*).html$/' => sub {
58 my ( $self, $c ) = @_;
59 $c->stash->{template} = 'product.tt';
60 $c->stash->{product} = $c->req->snippets->[0];
61 }
62 );
63
64=head1 DESCRIPTION
65
66Catalyst is based upon L<Maypole>, which you should consider for smaller
67projects.
68
69The key concept of Catalyst is DRY (Don't Repeat Yourself).
70
71See L<Catalyst::Manual> for more documentation.
72
73Omit the Catalyst::Plugin:: prefix from plugins.
74So Catalyst::Plugin::My::Module becomes My::Module.
75
76 use Catalyst 'My::Module';
77
78You can also set special flags like -Debug and -Engine.
79
80 use Catalyst qw/-Debug My::Module/;
81
82The position of plugins and flags in the chain is important,
83because they are loaded in the same order they appear.
84
85=head2 -Debug
86
87 use Catalyst '-Debug';
88
89is equivalent to
90
91 use Catalyst;
92 sub debug { 1 }
93
94=head2 -Engine
95
96Force Catalyst to use a specific engine.
97Omit the Catalyst::Engine:: prefix.
98
99 use Catalyst '-Engine=CGI';
100
101=head2 METHODS
102
103=head3 debug
104
105Overload to enable debug messages.
106
107=cut
108
109sub debug { 0 }
110
111=head3 config
112
113Returns a hashref containing your applications settings.
114
115=cut
116
117sub config {
118 my $self = shift;
119 $self->_config( {} ) unless $self->_config;
120 if ( $_[0] ) {
121 my $config = $_[1] ? {@_} : $_[0];
122 while ( my ( $key, $val ) = each %$config ) {
123 $self->_config->{$key} = $val;
124 }
125 }
126 return $self->_config;
127}
128
129sub import {
130 my ( $self, @options ) = @_;
131 my $caller = caller(0);
132
133 # Class
134 {
135 no strict 'refs';
136 *{"$caller\::handler"} =
137 sub { Catalyst::Engine::handler( $caller, @_ ) };
138 push @{"$caller\::ISA"}, $self;
139 }
140 $self->log( Catalyst::Log->new );
141
142 # Options
143 my $engine =
144 $ENV{MOD_PERL} ? 'Catalyst::Engine::Apache' : 'Catalyst::Engine::CGI';
145 foreach (@options) {
146 if (/^\-Debug$/) {
147 no warnings;
148 no strict 'refs';
149 *{"$self\::debug"} = sub { 1 };
150 $caller->log->debug('Debug messages enabled');
151 }
152 elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" }
153 elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) }
154 else {
155 my $plugin = "Catalyst::Plugin::$_";
156
157 # Plugin caller should be our application class
158 eval "package $caller; require $plugin";
159 if ($@) {
160 $caller->log->error(qq/Couldn't load plugin "$plugin", "$@"/);
161 }
162 else {
163 $caller->log->debug(qq/Loaded plugin "$plugin"/)
164 if $caller->debug;
165 unshift @ISA, $plugin;
166 }
167 }
168 }
169
170 # Engine
171 $engine = "Catalyst::Engine::$ENV{CATALYST_ENGINE}"
172 if $ENV{CATALYST_ENGINE};
173 $engine->require;
174 die qq/Couldn't load engine "$engine", "$@"/ if $@;
175 push @ISA, $engine;
176 $caller->log->debug(qq/Loaded engine "$engine"/) if $caller->debug;
177}
178
179=head1 SEE ALSO
180
181L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
182L<Catalyst::Response>, L<Catalyst::Engine>
183
184=head1 AUTHOR
185
186Sebastian Riedel, C<sri@oook.de>
187
188=head1 THANK YOU
189
afdca3a3 190Danijel Milicevic, David Naughton, Gary Ashton Jones, Jesse Sheidlower,
191Marcus Ramberg and all the others who've helped.
fc7ec1d9 192
193=head1 LICENSE
194
195This library is free software . You can redistribute it and/or modify it under
196the same terms as perl itself.
197
198=cut
199
2001;