Merge from depluralization branch
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / Intro.pod
CommitLineData
d442cc9f 1=head1 NAME
2
4b4d3884 3Catalyst::Manual::Tutorial::Intro - Catalyst Tutorial - Chapter 1: Introduction
d442cc9f 4
5
6=head1 OVERVIEW
7
4b4d3884 8This is B<Chapter 1 of 10> for the Catalyst tutorial.
d442cc9f 9
10L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12=over 4
13
14=item 1
15
16B<Introduction>
17
18=item 2
19
20L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
21
22=item 3
23
1def4a20 24L<More Catalyst Basics|Catalyst::Manual::Tutorial::MoreCatalystBasics>
d442cc9f 25
26=item 4
27
1def4a20 28L<Basic CRUD|Catalyst::Manual::Tutorial::BasicCRUD>
d442cc9f 29
30=item 5
31
1def4a20 32L<Authentication|Catalyst::Manual::Tutorial::Authentication>
d442cc9f 33
34=item 6
35
1def4a20 36L<Authorization|Catalyst::Manual::Tutorial::Authorization>
d442cc9f 37
38=item 7
39
1def4a20 40L<Debugging|Catalyst::Manual::Tutorial::Debugging>
d442cc9f 41
42=item 8
43
1def4a20 44L<Testing|Catalyst::Manual::Tutorial::Testing>
d442cc9f 45
46=item 9
47
1def4a20 48L<Advanced CRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
49
50=item 10
51
d442cc9f 52L<Appendices|Catalyst::Manual::Tutorial::Appendices>
53
54=back
55
1def4a20 56
d442cc9f 57=head1 DESCRIPTION
58
ffeb7448 59This tutorial provides a multi-part introduction to the Catalyst web
d442cc9f 60framework. It seeks to provide a rapid overview of many of its most
61commonly used features. The focus is on the real-world best practices
62required in the construction of nearly all Catalyst applications.
63
64Although the primary target of the tutorial is users new to the Catalyst
65framework, experienced users may wish to review specific sections (for
1def4a20 66example, how to use DBIC for their model classes, how to add
67authentication and authorization to an existing application, or form
68management).
d442cc9f 69
70You can obtain the code for all the tutorial examples from the
71catalyst subversion repository by issuing the command:
72
028b4e1a 73 svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial
d442cc9f 74
4b4d3884 75This will download the most recent tarball for each chapter of the
028b4e1a 76tutorial into the CatalystTutorial directory on your machine.
d442cc9f 77
3d9ae335 78B<These reference implementations are provided so that when you follow
79the tutorial, you can use the code from the subversion repository to
80ensure that your system is set up correctly, and that you have not
81inadvertently made any typographic errors, or accidentally skipped
82part of the tutorial.>
83
ffeb7448 84B<NOTE: You can use any Perl-supported OS and environment to run
3533daff 85Catalyst.> It should make little or no difference to Catalyst's
acbd7bdd 86operation, B<but this tutorial has been written using the Debian 5
87live CD> because that represents a quick and easy for most people to
88try out Catalyst with virtually zero setup time and hassles. Also,
89the tutorial has been tested to work correctly with the versions of
90Catalyst and all the supporting modules in Debian 5 (see "VERSIONS
91AND CONVENTIONS USED IN THIS TUTORIAL" below for the specific versions
92for some of the key modules), so B<if you think you might be running
93into an issue related to versions> (for example, a module changed its
94behavior in a newer version or a bug was introduced), B<it might be
95worth giving Debian 5 a try>. See the "CATALYST INSTALLATION"
96section below for more information.
3533daff 97
98If you're reading this manual online, you can download the example
99program and all the necessary dependencies to your local machine by
100installing the C<Task::Catalyst::Tutorial> distribution from CPAN:
d442cc9f 101
102 cpan Task::Catalyst::Tutorial
103
104This will also test to make sure the dependencies are working. If you
105have trouble installing these, please ask for help on the #catalyst
106IRC channel, or the Catalyst mailing list.
107
3533daff 108Subjects covered by the tutorial include:
d442cc9f 109
110=over 4
111
112=item *
113
114A simple application that lists and adds books.
115
116=item *
117
acbd7bdd 118The use of L<DBIx::Class|DBIx::Class> (DBIC) for the model (including
119some of the more advanced techniques you will probably want to use in
120your applications).
d442cc9f 121
122=item *
123
124How to write CRUD (Create, Read, Update, and Delete) operations in
125Catalyst.
126
127=item *
128
129Authentication ("auth").
130
131=item *
132
133Role-based authorization ("authz").
134
135=item *
136
137Attempts to provide an example showing current (5.7XXX) Catalyst
138practices. For example, the use of
139L<Catalyst::Action::RenderView|Catalyst::Action::RenderView>,
140DBIC, L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader>
45d511e0 141with C<myapp.conf>, the use of C<lib/MyApp/Controller/Root.pm>
d442cc9f 142vs. C<lib/MyApp.pm>, etc.
143
144=item *
145
1390ef0e 146The use of Template Toolkit (TT).
d442cc9f 147
148=item *
149
150Useful techniques for troubleshooting and debugging Catalyst
151applications.
152
153=item *
154
155The use of SQLite as a database (with code also provided for MySQL and
156PostgreSQL).
157
158=item *
159
1def4a20 160The use of L<HTML::FormFu|HTML::FormFu> for automated form processing
d442cc9f 161and validation.
162
163=back
164
165This tutorial makes the learning process its main priority. For
166example, the level of comments in the code found here would likely be
1def4a20 167considered excessive in a "normal project." Because of their contextual
d442cc9f 168value, this tutorial will generally favor inline comments over a
169separate discussion in the text. It also deliberately tries to
170demonstrate multiple approaches to various features (in general, you
171should try to be as consistent as possible with your own production
172code).
173
174Furthermore, this tutorial tries to minimize the number of controllers,
175models, TT templates, and database tables. Although this does result in
176things being a bit contrived at times, the concepts should be applicable
177to more complex environments. More complete and complicated example
178applications can be found in the C<examples> area of the Catalyst
179Subversion repository at
180L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/>.
181
1390ef0e 182
d442cc9f 183=head1 VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL
184
185This tutorial was built using the following resources. Please note that
186you may need to make adjustments for different environments and
187versions:
188
189=over 4
190
191=item *
192
acbd7bdd 193Debian 5 (Lenny)
d442cc9f 194
195=item *
196
acbd7bdd 197Catalyst v5.71000
dd88c3b6 198
22a67212 199=item *
dd88c3b6 200
acbd7bdd 201Catalyst::Devel v1.08
d442cc9f 202
203=item *
204
acbd7bdd 205DBIx::Class v0.08012
d442cc9f 206
207=item *
208
209Catalyst Plugins
210
211The plugins used in this tutorial all have sufficiently stable APIs that
212you shouldn't need to worry about versions. However, there could be
213cases where the tutorial is affected by what version of plugins you
214use. This tutorial has been tested against the following set of plugins:
215
216=over 4
217
218=item *
219
865d3efb 220Catalyst::Plugin::Authentication -- v0.10006
d442cc9f 221
222=item *
223
1def4a20 224Catalyst::Plugin::Authorization::Roles -- v0.05
d442cc9f 225
226=item *
227
865d3efb 228Catalyst::Plugin::ConfigLoader -- v0.20
d442cc9f 229
230=item *
231
865d3efb 232Catalyst::Plugin::Session -- v0.19
d442cc9f 233
234=item *
235
865d3efb 236Catalyst::Plugin::Session::State::Cookie -- v0.09
d442cc9f 237
238=item *
239
865d3efb 240Catalyst::Plugin::Session::Store::FastMmap -- v0.05
d442cc9f 241
242=item *
243
865d3efb 244Catalyst::Plugin::StackTrace -- v0.08
d442cc9f 245
246=item *
247
1def4a20 248Catalyst::Plugin::Static::Simple -- v0.20
d442cc9f 249
250=back
251
252=item *
253
865d3efb 254B<NOTE:> You can check the versions you have installed with the
255following command:
256
896ec184 257 perl -M<_mod_name_> -e '"print $<_mod_name_>::VERSION\n"'
865d3efb 258
259For example:
260 perl -MCatalyst::Plugin::StackTrace -e 'print "$Catalyst::Plugin::StackTrace::VERSION\n"'
261
d442cc9f 262Since the web browser is being used on the same box where Perl and the
263Catalyst development server is running, the URL of
264C<http://localhost:3000> will be used (the Catalyst development server
265defaults to port 3000). If you are running Perl on a different box than
266where your web browser is located (or using a different port number via
267the C<-p> I<port_number> option to the development server), then you
268will need to update the URL you use accordingly.
269
270=item *
271
1390ef0e 272Depending on the web browser you are using, you might need to hit
273C<Shift+Reload> or C<Ctrl+Reload> to pull a fresh page when testing
274your application at various points (see
275L<http://en.wikipedia.org/wiki/Bypass_your_cache> for a comprehensive
276list of options for each browser). Also, the C<-k> keepalive option
277to the development server can be necessary with some browsers
278(especially Internet Explorer).
d442cc9f 279
280=back
281
1390ef0e 282
d442cc9f 283=head1 CATALYST INSTALLATION
284
acbd7bdd 285Although Catalyst installation has been a challenge in the past, the
286good news is that there are a growing number of options to eliminate
287(or at least dramatically simplify) this concern. Although a
288compelling strength of Catalyst is that it makes use of many of the
289modules in the vast repository that is CPAN, this can complicate the
290installation process if you approach it in the wrong way. Consider
291the following suggestions on the most common ways to get started with
292a Catalyst development environment:
1def4a20 293
294=over 4
295
296=item *
297
acbd7bdd 298Debian
299
300The Debian 5 live CD represents a great way for newcomers to
301experiment with Catalyst. As a "live CD," you can simple boot from
302the CD, run a few commands, and in a matter of minutes you should have
303a fully function environment in which do this tutorial. B<The tutorial
304was fully tested to work under Debian 5. Although it SHOULD work
305under any Catalyst installation method you might choose, it can be
306hard to guarantee this.>
307
308=over 4
309
310=item *
311
312Download one of the ISO files from
313L<http://cdimage.debian.org/cdimage/release/current-live/i386/iso-cd/>.
314You can pick any one of the live CD variations will work, but
315you may wish to consider the following points:
316
317=over 4
318
319=item *
320
321"C<debian-live-500-i386-rescue.iso>" is probably the best all-around
322option for most people because it includes many extra tools such as
323the GCC compiler, therefore saving RAM (every package you need to
324install when running from live CD consumes memory because RAM disk is
325being used in lieu of real disk space). When initially booting under
326this image, you may see some cryptic warning messages having to do
327with various diagnostic tools it tries to load or enable, but you
328should be able to safely ignore these.
329
330=item *
331
332"C<debian-live-500-i386-standard.iso>" is a great option because of
333its compact size, but you will probably need approximately 1 GB of RAM
334in the computer where you will run the tutorial. Because the
335"standard" live CD comes with with a minimal set of tools, we will
336have to install extra packages (such as the GCC compiler), all of
337which will require RAM when running from a live CD.
338
339=item *
340
341The other ISO images include different flavors of X-Windows desktop
342managers. You can select one of these if you don't mind the larger
343download size and prefer a graphical environment. Be aware that these
344disks do not come with the extra tools found on the "rescue" image, so
345you will need adequate RAM to be able to install them just as you
346would under the "standard" image. B<Use one of the "graphical" ISO
347images if you want a graphical web browser on the same machine as
348where you will run the tutorial.> (If you are using one of the non-
349graphical images discussed above, you can still use a graphical web
350browser from another machine and point it to your Catalyst development
351machine.)
352
353=back
354
355=item *
356
357Boot off the CD.
358
359=item *
360
361Select "C<Live>" from the initial boot menu.
362
363=item *
364
365Once the system has booted to a "C<user@debian:~$>" prompt, enter the
366following command to add the more current "unstable" package
367repository:
368
369 sudo vi /etc/apt/sources.list
370
371Add the following line to the bottom of this file:
372
373 deb http://ftp.us.debian.org/debian/ unstable main
374
375If you are not familiar with VI, you can move to the bottom of this
376file and press the "o" key to insert a new line and type the line
377above. Then press the "Esc" key followed by a colon (":"), the
378letters "wq" and then the "Enter" key. The rest of the tutorial will
379assume that you know how to use some editor that is available from the
380Linux command-line environment.
381
382=item *
383
384Install Catalyst:
385
386 sudo aptitude update
387 sudo aptitude -y install sqlite3 libdbd-sqlite3-perl libcatalyst-perl \
388 libcatalyst-modules-perl libconfig-general-perl libsql-translator-perl \
389 libdatetime-perl libdatetime-format-mysql-perl libio-all-perl \
390 libperl6-junction-perl
391
392Let it install (normally about a 30-second operaton) and you are
393done.
394
395If you are using an image other than the "rescue" ISO, you will also need
396to run the following command to install additional packages:
397
398 sudo aptitude -y install gcc make libc6-dev
399
400If you are running from the Live CD, you probably also want to free up
401some RAM disk space with the following:
402
403 sudo aptitude clean
404
405NOTE: While the instructions above mention the Live CD because that
406makes it easy for people new to Linux, you can obviously pick a
407different Debian ISO image and install it to your hard drive.
408Although there are many different ways to download and install Debian,
409the "netinst" ISO image (such as "C<debian-500-i386-netinst.iso>"
410represents a great option because it keeps your initial download small
411(but still let's you install anything you want "over the network").
412
413Here are some tips if you are running from a live CD and are running
414out of disk space (which really means you are running out of RAM):
415
416=over 4
417
418=item *
419
420Always run "C<aptitude clean>" after you install new packages to
421delete the original .deb files (the files installed B<by> the .deb
422package B<will> remain available, just the .deb package itself is
423deleted).
424
425=item *
426
427If you are installing modules from CPAN, you can free up some space
428with "C<rm -rf /root/.cpan/*>".
429
430=item *
431
432If necessary, you can remove the cached package information with the
433command "C<rm -f /var/lib/apt/lists/*>". You can later pull this
434information again via the command "C<aptitude update>".
435
436=item *
437
438You can save a small amount of space by commenting out the lines in
439C</etc/apt/sources.list> that reference "deb-src" and
440"security.debian.org". If you have already done an "C<aptitude
441update>" with these repositories enabled, you can use the tip in the
442previous bullet to free the space up (and then do another "C<aptitude
443update>").
444
445=item *
446
447Although you can free up space by removing packages you installed
448since you last booted (check out "C<aptitude remove _pkg_name>"),
449don't bother trying to remove packages already available at the time
450of boot. Instead of freeing up space, it will actual I<consume> some
451space. (The live CD uses these "burn in" packages right from the CD
452disk vs. first loading them on the virtual RAM disk. However, if you
453remove them, the system has to update various files, something that
454I<does> consume some space on the virtual RAM disk.)
455
456=back
457
458=back
459
460=item *
461
1def4a20 462Ubuntu
463
acbd7bdd 464Ubuntu is an extremely popular offshoot of Debian. It provides
465cutting edge versions of many common tools, application and libraries
466in an easy-to-run live CD configuration (and because a single download
467option can be used for both live CD and install-to-disk usage, it
468keeps your download options nice and simple). As with Debian 5, you
469should be able to generate a fully function Catalyst environment in a
470matter of minutes. Here are quick instructions on how to use Ubuntu
471to prepare for the tutorial:
d442cc9f 472
473=over 4
474
475=item *
476
acbd7bdd 477Download the Ubuntu Desktop edition and boot from the CD and/or image
478file, select your language, and then "Try Ubuntu without any changes
479to your computer."
1def4a20 480
481=item *
482
3533daff 483Open a terminal session (click "Applications" in the upper-left
484corner, then "Accessories," then "Terminal").
1def4a20 485
486=item *
487
488Add the 'universe' repositories:
489
3533daff 490 sudo gedit /etc/apt/sources.list
1def4a20 491
492And remove the comments from the lines under the comments about the
493'universe' repositories.
494
495=item *
496
497Install Catalyst:
498
acbd7bdd 499 sudo aptitude update
500 sudo aptitude install libdbd-sqlite3-perl libcatalyst-perl libcatalyst-modules-perl libconfig-general-perl
1def4a20 501
2b75577c 502Accept all of the dependencies. Done.
503
504If you are running from the Live CD, you probably also want to free up
505some disk space with the following:
506
acbd7bdd 507 sudo aptitude clean
2b75577c 508
acbd7bdd 509NOTE: While the instructions above mention the live CD because that
2b75577c 510makes it easy for people new to Linux, you can obviously also use one
511of the options to install Ubuntu on your drive.
3533daff 512
1def4a20 513=back
514
515=item *
516
d442cc9f 517Matt Trout's C<cat-install>
518
ffeb7448 519Available at L<http://www.shadowcatsystems.co.uk/static/cat-install>,
520C<cat-install> can be a fairly painless way to get Catalyst up and
521running. Just download the script from the link above and type C<perl
522cat-install>. Depending on the speed of your Internet connection and
523your computer, it will probably take 30 to 60 minutes to install because
524it downloads, makes, compiles, and tests every module. But this is an
525excellent way to automate the installation of all the latest modules
526used by Catalyst from CPAN.
527
d442cc9f 528
529=item *
530
ffeb7448 531Other Possibilities
532
533=over 4
534
535=item *
536
537OpenBSD Packages
538
539The 2008 Advent Day 4 entry has more information on using OpenBSD
540packages to quickly build a system:
541L<http://www.catalystframework.org/calendar/2008/4>.
542
543=item *
d442cc9f 544
ffeb7448 545NetBSD Package Collection on Solaris
546
547The 2008 Advent Day 15 entry has more information on using C<pkgsrc> and
548NetBSD packages on Solaris:
1435672d 549L<http://www.catalystframework.org/calendar/2008/15>.
d442cc9f 550
551=item *
552
ffeb7448 553CatInABox
554
555You can get more information at
556L<http://www.catalystframework.org/calendar/2008/7>
557or L<Perl::Dist::CatInABox|Perl::Dist::CatInABox>.
558
0c51850e 559=item *
560
561Frank Speiser's Amazon EC2 Catalyst SDK
562
563There are currently two flavors of publicly available Amazon Machine
564Images (AMI) that include all the elements you'd need to begin
565developing in a fully functional Catalyst environment within minutes.
566See L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>
567for more details.
568
d442cc9f 569=back
570
ffeb7448 571=back
572
d442cc9f 573For additional information and recommendations on Catalyst installation,
574please refer to
575L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>.
576
1390ef0e 577
d442cc9f 578=head1 DATABASES
579
580This tutorial will primarily focus on SQLite because of its simplicity
581of installation and use; however, modifications in the script required
acbd7bdd 582to support MySQL and PostgreSQL will be presented in Appendix.
d442cc9f 583
584B<Note:> One of the advantages of the MVC design patterns is that
585applications become much more database independent. As such, you will
586notice that only the C<.sql> files used to initialize the database
587change between database systems: the Catalyst code generally remains the
588same.
589
1390ef0e 590
d442cc9f 591=head1 WHERE TO GET WORKING CODE
592
4b4d3884 593Each chapter of the tutorial has complete code available as a tarball in
028b4e1a 594the main Catalyst Subversion repository (see the note at the beginning
595of each part for the appropriate svn command to use).
d442cc9f 596
4b4d3884 597B<NOTE:> You can run the test cases for the final code through Chapter 8
028b4e1a 598with the following commands:
d442cc9f 599
acbd7bdd 600 sudo cpan Catalyst::Model::DBIC::Schema Time::Warp DBICx::TestDatabase \
efdaddec 601 DBIx::Class::DynamicDefault DBIx::Class::TimeStamp DBIx::Class::EncodedColumn
4b4d3884 602 wget http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/MyApp_Chapter8.tgz
603 tar zxvf MyApp_Chapter8.tgz
d442cc9f 604 cd MyApp
a46b474e 605 CATALYST_DEBUG=0 prove --lib lib t
d442cc9f 606
acbd7bdd 607If you wish to include the L<HTML::FormFu|HTML::FormFu> section in
4b4d3884 608your tests, substitute C<MyApp_Chapter9_FormFu.tgz> for
609C<MyApp_Chapter8.tgz> in the URL above. However, you will also need to
acbd7bdd 610run the following additional commands:
611
612 sudo aptitude -y install libhtml-formfu-perl libmoose-perl \
613 libregexp-assemble-perl libhtml-formfu-model-dbic-perl
614 sudo aptitude clean
615 sudo cpan Catalyst::Component::InstancePerContext Catalyst::Controller::HTML::FormFu
616
617You can also fire up the application under the development server that is conveniently
618built in to Catalyst. Just issue this command from the C<MyApp> directory where you
619ran the test suite above:
620
621 script/myapp_server.pl
622
623And the application will start. You can try out the application by
624pulling up C<http://localhost:3000> in your web browser (as mentioned
625earlier, change C<localhost> to a different IP address or DNS name if
626you are running your web browser and your Catalyst development on
627different boxes). We will obviously see more about how to use the
4b4d3884 628application as we go through the remaining chapters of the tutorial, but
acbd7bdd 629for now you can log in using the username "test01" and a password of
630"mypass".
631
d442cc9f 632
633=head1 AUTHOR
634
635Kennedy Clark, C<hkclark@gmail.com>
636
637Please report any errors, issues or suggestions to the author. The
638most recent version of the Catalyst Tutorial can be found at
82ab4bbf 639L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.70/trunk/lib/Catalyst/Manual/Tutorial/>.
d442cc9f 640
45c7830f 641Copyright 2006-2008, Kennedy Clark, under Creative Commons License
865d3efb 642(L<http://creativecommons.org/licenses/by-sa/3.0/us/>).