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