Some little changes to Plugins.pod before bed. Ran podchecker on all pods and cleaned...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / About.pod
CommitLineData
aa2b0d97 1=head1 NAME
2
3Catalyst::Manual::About - Basic explanation of Catalyst
4
5=head1 DESCRIPTION
6
7This document is a basic introduction to the I<why> of Catalyst. It does
8not teach you how to write Catalyst applications; for an introduction to
9that please see L<Catalyst::Manual::Intro>. Rather, it explains the
10basics of what Catalyst is typically used for, and why you might want
11to use Catalyst to build your applications.
12
13=head2 What is Catalyst? The short summary
14
15Catalyst is a web application framework. This means that you use it to
16help build applications that run on the web, or that run using protocols
17used for the web. Catalyst is designed to make it easy to manage the
18various tasks you need to do to run an application on the web, either by
19doing them itself, or by letting you "plug in" existing Perl modules
20that do what you need. There are a number of things you typically do
21with a web application, for example:
22
23=over 4
24
25=item * Interact with a web server
26
27If you're on the web, you're relying on a web server, a program that
28sends files over the web. There are a number of these, and your
29application has to do the right thing to make sure that your data works
30with the web server you're using. If you change your web server, you
31don't want to have to rewrite your entire application to work with the
32new one.
33
34=item * Do something based on a URI
35
4100e391 36It's typical for web applications to use URIs as a main way for users to
37interact with the rest of the application; various elements of the URI
38will indicate what the application needs to do. Thus,
39C<http://www.mysite.com/add_record.cgi?name=John&title=President> will
40add a person named "John" whose title is "President" to your database,
41and C<http://www.mysite.com/catalog/display/23> will go to a "display"
42of item 23 in your catalog, and
95bb1e81 43C<http://www.mysite.com/order_status/7582> will display the status of
44order 7582, and C<http://www.mysite.com/add_comment/?page=8> will
4100e391 45display a form to add a comment to page 8. Your application needs to
46have a regular way of processing these URIs so it knows what to do
47when such a request comes in.
aa2b0d97 48
49=item * Interact with a data store
50
51You probably use a database to keep track of your information. Your
4100e391 52application needs to interact with your database, so you can create,
53edit, and retrieve your data.
aa2b0d97 54
55=item * Handle forms
56
57When a user submits a form, you process it, and make sure it's been
58filled in properly, and then then do something based on the
59result--submitting an order, updating a record, sending e-mail, or going
60back to the form if there's an error.
61
62=item * Display results
63
64You have an application running on the web, people need to see
65things. You usually want to display things on a web browser; you will
66probably be using a template system to help generate HTML code; you
67might need other kinds of display, such as PDF files or RSS feeds.
68
69=item * Manage users
70
71You might need the concept of a "user", someone who's allowed to use
72your system, and is allowed to do certain things only. Perhaps normal
73users can only view or modify their own information; administrative
74users can view or modify anything; normal users can only order items for
75their own account; normal users can view things but not modify them;
76order-processing users can send records to a different part of the
77system; and so forth. You need a way of ensuring that people are who
78they say they are, and that people only do the things they're allowed to
79do.
80
81=item * Develop the application itself
82
83When you're writing or modifying the application, you want to have
84access to detailed logs of what it is doing. You want to be able to
85write tests to ensure that it does what it's supposed to, and that new
86changes don't break the existing code.
87
88=back
89
90Catalyst makes it easy to do all of these tasks, and many more. It is
91extremely flexible in terms of what it allows you to do, and very fast.
92It has a very large number of "plugins" that interact with existing Perl
4100e391 93modules so that you can easily use them from within your
94application. Interact with a web server? Catalyst lets you use a number
95of different ones, and even comes with a built-in server for testing or
96local deployment. Do something based on a URI? Catalyst has extremely
97flexible systems for figuring out what to do based on a URI. Interact
98with a data store? Catalyst has many plugins for different databases
99and database frameworks, and for other non-database storage
100systems. Handle forms? Catalyst has plugins available for several form
101creation and validation systems that make it easy for the programmer to
102manage. Display results? Catalyst has plugins available for a number of
103template modules and other output packages. Manage users? Catalyst has
104plugins that handle sessions, authentication, and authorization, in any
105way you need. Developing the application? Catalyst has detailed logging
106built-in, which you can configure as necessary, and supports the easy
107creation of new tests--some of which are automatically created when you
108begin writing a new application.
aa2b0d97 109
110=head3 What B<isn't> Catalyst?
111
112Catalyst is not an out-of-the-box solution that allows you to set up a
113complete working e-commerce application in ten minutes. (There are,
114however, several systems built on top of Catalyst that can get you very
115close to a working app.) It is not designed for end users, but for
116working programmers.
117
4100e391 118=head2 Web programming: The Olden Days
119
120Perl has long been favored for web applications. There are a wide
121variety of ways to use Perl on the web, and things have changed over
122time. It's possible to handle everything with very raw Perl code:
123C<print "Content-type: text/html\n\n<center><h1>Hello
124World!</h1></center>";> for example, or
125
126 my @query_elements = split(/&/, $ENV{'QUERY_STRING'});
127 foreach my $element (@query_elements) {
128 my ($name, $value) = split(/=/, $element);
129 # do something with your parameters, or kill yourself
130 # in frustration for having to program like this
131 }
132
133Much better than this is to use Lincoln Stein's great L<CGI> module,
134which smoothly handles a wide variety of common tasks--parameter
135parsing, generating form elements from Perl data structures, printing
136http headers, escaping text, and very many more, all with your choice of
137functional or object-oriented style. While L<CGI> was revolutionary and
138is still widely used, it has various drawbacks that make it unsuitable
139for larger applications: it is slow, your code with it generally
140combines application logic and display code, and it makes it very
141difficult to handle larger applications with complicated control flow.
142
143A variety of frameworks followed, of which the most widely used is
144probably L<CGI::Application>, which encourages the development of
145modular code, with easy-to-understand control-flow handling, the use of
146plugins and templating systems, and the like. Other systems include
147L<AxKit>, which is designed for use with XML running under mod_perl, and
148L<Maypole>--upon which Catalyst was originally based--designed for the
149easy development of powerful web databases. Is it not the purpose of
150this document to criticize or even evaluate these other frameworks; they
151may be useful for you and if so we encourage you to give them a try.
152
aa2b0d97 153=head2 The MVC pattern
154
95bb1e81 155MVC, or Model-View-Controller, is a model currently favored for web
156applications. This design pattern is originally from the Smalltalk
157programming language. The basic idea is that the three main areas of an
158application--handling application flow (Controller), processing
159information (Model), and outputting the results (View)--are kept
160separate, so that it is possible to change or replace any one without
4100e391 161affecting the others, and so that if you're interested in one particular
162aspect, you know where to find it.
95bb1e81 163
164Discussions of MVC often degenerate into nitpicky arguments about the
165history of the pattern, and exactly what "usually" or "should" go into
166the Controller or the Model. We have no interest in joining such a
167debate. In any case, Catalyst does not enforce any particular setup; you
168are free to put any sort of code in any part of your application, and
169this discussion (and others elsewhere in the Catalyst documentation) is
170only a suggestion based on what we think works well. In most Catalyst
171applications, each branch of MVC will be made of up of several Perl
172modules that can handle different needs in your application.
173
174The purpose of the B<Model> is to access and modify data. Typically
175the Model will interact with a relational database, but it's also
176common to use other data sources, such as the L<Plucene> search
177engine, an LDAP server, etc.
178
179The purpose of the B<View> is to present data to the user. Typical Views
180use a templating module to generate HTML code, using L<Template
181Toolkit|Template>, L<Mason|HTML::Mason>, L<HTML::Template>, or the like,
182but it's also possible to generate PDF output, send email, etc., from a
183View. In Catalyst the View is usually a small module, just gluing some
184other module into Catalyst; the display logic is written within the
185template itself.
186
187The Controller is Catalyst itself. When a request is made to Catalyst,
188it will be received by one of your Controller modules; this module
189will figure out what the user is trying to do, gather the necessary
190data from a Model, and send it to a View for display.
191
192=head3 A simple example
193
194The general idea is that you should be able to change things around
195without affecting the rest of your application. Let's look at a very
196simple example (keeping in mind that there are many ways of doing this,
197and what we're discussing is one possible way, not the only
198way). Suppose you have a record to display. It doesn't matter if it's a
199catalog entry, a library book, a music CD, a personnel record, or
200anything else, but let's pretend it's a catalog entry. A user is given a
201URL such as C<http://www.mysite.com/catalog/display/2782>. Now what?
202
203First, Catalyst figures out that you're using the "catalog" Controller
204(how Catalyst figures this out is entirely up to you; URL dispatching is
205I<extremely> flexible in Catalyst). Then Catalyst sees that you want to
206use a "display" method in your Controller. Somewhere in this process,
207it's possible that you'll have authentication and authorization routines
208to make sure that the user is registered and is allowed to display a
209record. The Controller's display method will then extract "2782" as the
210record you want to retrieve, and make a request to a Model for that
211record. The Controller will then look at what the Model returns: if
212there's no record, the Controller will ask the View to display an error
213message, otherwise it will hand the View the record and ask the View to
214display it. In either case, the View will then generate an HTML page,
215which Catalyst will display to the user's browser, using whatever web
216server you've configured.
217
218How does this help you?
219
220In many ways. Suppose you have a small catalog now, and you're using a
221lightweight database such as SQLite, or even a text file. But eventually
222your site grows, and you need to upgrade to something more
223powerful--MySQL or Postgres, or even Oracle or DB2. If your Model is
224separate, you only have to change one thing, the Model; your Controller
225can expect that if it issues a query to the Model, it will get the right
226kind of result back.
227
228What about the View? The idea is that your template is concerned almost
229entirely with display, so that you can hand it off to a designer who
230doesn't have to worry about how to write code. If you get all the data
231in the Controller and then pass it to the View, the template isn't
232responsible for any kind of data processing. And if you want to change
233your output, it's simple: just write a new View. If your Controller is
234already getting the data you need, you pass it in the same way, and
235whether you display the results to a web browser, generate a PDF, or
236e-mail the results back to the user, the Controller hardly changes at
237all.
238
239And throughout the whole process, most of the tools you need are either
240part of Catalyst (the parameter-processing routines that extract "2782"
241from the URL, for example) or are easily plugged into it (the
242authentication routines, the plugins for using Template Toolkit as your
243View).
244
245Now, Catalyst doesn't enforce very much at all. You can connect to a
246database, issue queries, and act on them from within your View, if you
247want. You can handle paging (i.e. retrieving only a portion of the total
248records possible) in your Controller or your Model. It's up to you. In
249some cases there might be very good reasons to do things a certain way
250(issuing database queries from a template seems to defeat the whole
251purpose of separation-of-concerns, and will drive your designer crazy),
252while in others it's just a matter of personal preference (perhaps your
253template, rather than your Controller, is the better place to decide
254what to display if you get an empty result). Catalyst just gives you the
255tools.
256
aa2b0d97 257=head1 AUTHOR
258
259Jesse Sheidlower, C<jester@panix.com>
260
261=head1 SEE ALSO
262
263L<Catalyst>, L<Catalyst::Manual::Intro>
264
265=head1 COPYRIGHT
266
267This program is free software, you can redistribute it and/or modify it
268under the same terms as Perl itself.