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