3 Catalyst::Manual::Tutorial::01_Intro - Catalyst Tutorial - Chapter 1: Introduction
8 This is B<Chapter 1 of 10> for the Catalyst tutorial.
10 L<Tutorial Overview|Catalyst::Manual::Tutorial>
20 L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
24 L<More Catalyst Basics|Catalyst::Manual::Tutorial::03_MoreCatalystBasics>
28 L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>
32 L<Authentication|Catalyst::Manual::Tutorial::05_Authentication>
36 L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
40 L<Debugging|Catalyst::Manual::Tutorial::07_Debugging>
44 L<Testing|Catalyst::Manual::Tutorial::08_Testing>
48 L<Advanced CRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD>
52 L<Appendices|Catalyst::Manual::Tutorial::10_Appendices>
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.
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, and/or
70 You can obtain the code for all the tutorial examples from the
71 catalyst subversion repository by issuing the command:
73 svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial
75 This will download the most recent code for each chapter of the
76 tutorial into the CatalystTutorial directory on your machine.
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.>
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 6 Live
87 CD> because that represents a quick and easy way for most people to try
88 out Catalyst with virtually zero setup time and hassles. Also, the
89 tutorial has been tested to work correctly with the versions of Catalyst
90 and all the supporting modules in Debian 6 (see "VERSIONS AND
91 CONVENTIONS USED IN THIS TUTORIAL" below for the specific versions for
92 some of the key modules), so B<if you think you might be running into an
93 issue related to versions> (for example, a module changed its behavior
94 in a newer version or a bug was introduced), B<it might be worth giving
97 If you plan to follow along with Debian 6, you can jump down to the
98 "Debian" section under L</"CATALYST INSTALLATION"> below and it will
99 walk you though the setup of a fully functional Catalyst environment. If
100 you would prefer to install directly from CPAN, you can download the
101 example program and all the necessary dependencies to your local machine
102 by installing the C<Task::Catalyst::Tutorial> distribution:
104 cpan Task::Catalyst::Tutorial
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.
110 Subjects covered by the tutorial include:
116 A simple application that lists and adds books.
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
126 How to write CRUD (Create, Read, Update, and Delete) operations in
131 Authentication ("auth").
135 Role-based authorization ("authz").
139 Attempts to provide an example showing current (5.9) Catalyst
144 The use of Template Toolkit (TT).
148 Useful techniques for troubleshooting and debugging Catalyst
153 The use of SQLite as a database (with code also provided for MySQL and
154 PostgreSQL). (Note: Because we make use of the DBIx::Class Object
155 Relational Mapping [ORM] layer, out our application will be database
156 agnostic and can easily be used by any of the databases supported by
161 The use of L<HTML::FormFu|HTML::FormFu> or L<HTML::FormHandler|HTML::FormHandler>
162 for automated form processing and validation.
166 This tutorial makes the learning process its main priority. For
167 example, the level of comments in the code found here would likely be
168 considered excessive in a "normal project." Because of their contextual
169 value, this tutorial will generally favor inline comments over a
170 separate discussion in the text. It also deliberately tries to
171 demonstrate multiple approaches to various features (in general, you
172 should try to be as consistent as possible with your own production
175 Furthermore, this tutorial tries to minimize the number of controllers,
176 models, TT templates, and database tables. Although this does result in
177 things being a bit contrived at times, the concepts should be applicable
178 to more complex environments. More complete and complicated example
179 applications can be found in the C<examples> area of the Catalyst
180 Subversion repository at
181 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/>.
182 ***Todo: update link above?
187 For those who want to get going quickly, here is a short "cookbook-style
188 recipe" to quickly get you up and running. Although there are many
189 different ways to get a Catalyst environment going, this tutorial has
190 been written with and tested against the Debian 6 Live CD, using the
191 steps in this Quick Start.
193 If you want, you can follow the directions in this section and then jump
194 right to L<Chapter 2|Catalyst::Manual::Tutorial::02_CatalystBasics> of
195 the tutorial. However, it would be a good idea to come back and read the
196 sections below the Quick Start when you have time. Or, continue reading
197 those other sections for suggestions if you do not wish to use the
204 Download the C<debian-live-6.0.1-i386-rescue.iso> image from
205 L<http://cdimage.debian.org/cdimage/release/current-live/i386/iso-hybrid/>.
209 Boot this disk, either in a physical machine, or possibly some sort
210 of virtual machine (using a VM can be a very handy way to practice).
214 Select "C<Live>" from the initial boot menu.
218 At the "C<user@debian:~$>" prompt, type:
220 sudo aptitude -y install subversion
224 If you want to be able to remotely SSH to this system, set a
225 password for root (you might want to do "dpkg -l | grep openssh-server"
226 to be sure the ISO image you downloaded has the SSH daemon installed...
227 if it's missing, do a "sudo aptitude -y install openssh-server"):
234 Add the "unstable" Debian package repository:
236 sudo vi /etc/apt/sources.list
238 Add the following line to the bottom of this file:
240 deb http://ftp.us.debian.org/debian/ unstable main
244 Install Catalyst and related libraries:
247 sudo aptitude -y install sqlite3 libdbd-sqlite3-perl libcatalyst-perl \
248 libcatalyst-modules-perl libdbix-class-timestamp-perl \
249 libdatetime-format-sqlite-perl libconfig-general-perl \
250 libhtml-formfu-model-dbic-perl libterm-readline-perl-perl \
251 libdbix-class-encodedcolumn-perl libperl6-junction-perl \
261 svn co http://dev.catalystframework.org/repos/Catalyst/trunk/examples/Tutorial/MyApp_Chapter8
262 cd MyApp_Chapter8/MyApp
263 CATALYST_DEBUG=0 prove -wl t
269 =head1 VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL
271 This tutorial was built using the following resources. Please note that
272 you may need to make adjustments for different environments and versions
273 (note that trailing zeros in version numbers are not significant and may
274 get dropped with some techniques for viewing them; for example, Catalyst
275 v5.80020 might show up as 5.8002):
285 Catalyst v5.80020 (note: may show up as '5.8002' without the trailing zero)
289 Catalyst::Devel v1.26
297 Catalyst::Model::DBIC::Schema v0.40
301 Template Toolkit v2.20
308 The plugins used in this tutorial all have sufficiently stable APIs that
309 you shouldn't need to worry about versions. However, there could be
310 cases where the tutorial is affected by what version of plugins you
311 use. This tutorial has been tested against the following set of plugins:
317 Catalyst::Plugin::Authentication -- v0.10016
321 Catalyst::Plugin::Authorization::Roles -- v0.08
325 Catalyst::Plugin::ConfigLoader -- v0.27
329 Catalyst::Plugin::Session -- v0.29
333 Catalyst::Plugin::Session::State::Cookie -- v0.17
337 Catalyst::Plugin::Session::Store::File -- v0.18
341 Catalyst::Plugin::StackTrace -- v0.11
345 Catalyst::Plugin::Static::Simple -- v0.29
351 HTML::FormFu -- v0.06001
355 B<NOTE:> You can check the versions you have installed with the
358 perl -M<_mod_name_> -e 'print "$<_mod_name_>::VERSION\n"'
362 perl -MCatalyst -e 'print "$Catalyst::VERSION\n";'
366 perl -MCatalyst::Devel -e 'print "$Catalyst::Devel::VERSION\n";'
370 This tutorial will assume that the web browser is located on the same
371 system where the Catalyst development server is running, and therefore
372 use a URL of C<http://localhost:3000> (the Catalyst development server
373 defaults to port 3000, but can be changed with the "-p" option to the
374 development server). If you are running Perl on a different box than
375 where your web browser is located (or using a different port number via
376 the C<-p> I<port_number> option to the development server), then you
377 will need to update the URL you use accordingly.
379 Please Note: Depending on the web browser you are using, you might need
380 to hit C<Shift+Reload> or C<Ctrl+Reload> to pull a fresh page when
381 testing your application at various points (see
382 L<http://en.wikipedia.org/wiki/Bypass_your_cache> for a comprehensive
383 list of options for each browser). Also, the C<-k> keepalive option to
384 the development server can be necessary with some browsers (especially
385 Internet Explorer). ***Todo: is this still true?
390 =head1 CATALYST INSTALLATION
392 Although Catalyst installation has been a challenge in the past, the
393 good news is that there are a growing number of options to eliminate (or
394 at least dramatically simplify) this concern. Although a compelling
395 strength of Catalyst is that it makes use of many of the modules in the
396 vast repository that is CPAN, this can complicate the installation
397 process if you approach it in the wrong way. Consider the following
398 suggestions on the most common ways to get started with a Catalyst
399 development environment:
407 The Debian 6 Live CD represents a great way for newcomers to experiment
408 with Catalyst. As a "live CD," you can simple boot from the CD, run a
409 few commands, and in a matter of minutes you should have a fully
410 function environment in which do this tutorial. B<The tutorial was fully
411 tested to work under Debian 6. Although it SHOULD work under any
412 Catalyst installation method you might choose, it can be hard to
419 Download one of the ISO files from
420 L<http://cdimage.debian.org/cdimage/release/current-live/i386/iso-hybrid/>
421 (the current version at the time this was written was 6.0.1). You can
422 pick any one of the live CD variations will work, but you may wish to
423 consider the following points:
429 "C<debian-live-6.0.1-i386-rescue.iso>" is probably the best all-around
430 option for most people because it includes many extra tools such as the
431 GCC compiler, therefore saving RAM (every package you need to install
432 when running from a Live CD consumes memory because RAM disk is being
433 used in lieu of real disk space). When initially booting under this
434 image, you may see some cryptic warning messages having to do with
435 various diagnostic tools it tries to load or enable, but you should be
436 able to safely ignore these.
440 "C<debian-live-6.0.1-i386-standard.iso>" is a great option because of
441 its compact size, but you will probably need approximately 1 GB of RAM
442 in the computer where you will run the tutorial. Because the "standard"
443 live CD comes with with a minimal set of tools, we will have to install
444 extra packages (such as the GCC compiler), all of which will require RAM
445 when running from a live CD.
449 The other ISO images include different flavors of X-Windows desktop
450 managers. You can select one of these if you don't mind the larger
451 download size and prefer a graphical environment. Be aware that these
452 disks do not come with the extra tools found on the "rescue" image, so
453 you will need adequate RAM to be able to install them just as you would
454 under the "standard" image. B<Use one of the "graphical" ISO images if
455 you want a graphical web browser on the same machine as where you will
456 run the tutorial.> (If you are using one of the non- graphical images
457 discussed above, you can still use a graphical web browser from another
458 machine and point it to your Catalyst development machine.)
468 Select "C<Live>" from the initial boot menu.
472 Once the system has booted to a "C<user@debian:~$>" prompt, first
473 install the Subversion client in case you want to check out the
474 completed chapter example code:
476 sudo aptitude -y install subversion
478 If you want to be able to remotely SSH to this system, set a
484 Then enter the following command to add the more current "unstable"
485 package repository so we get the latest versions of Catalyst and related
488 sudo vi /etc/apt/sources.list
490 Add the following line to the bottom of this file:
492 deb http://ftp.us.debian.org/debian/ unstable main
494 If you are not familiar with VI, you can move to the bottom of this file
495 and press the "o" key to insert a new line and type the line above.
496 Then press the "Esc" key followed by a colon (":"), the letters "wq" and
497 then the "Enter" key. The rest of the tutorial will assume that you
498 know how to use some editor that is available from the Linux
499 command-line environment.
506 sudo aptitude -y install sqlite3 libdbd-sqlite3-perl libcatalyst-perl \
507 libcatalyst-modules-perl libdbix-class-timestamp-perl \
508 libdatetime-format-sqlite-perl libconfig-general-perl \
509 libhtml-formfu-model-dbic-perl libterm-readline-perl-perl \
510 libdbix-class-encodedcolumn-perl libperl6-junction-perl \
513 Let it install (normally about a 30 to 90-second operaton) and you are
514 done. (Note the '\' above. Depending on your environment, you might
515 be able to cut and paste the text as shown or need to remove the '\'
516 characters to that the command is all on a single line.)
518 If you are using an image other than the "rescue" ISO, you will also need
519 to run the following command to install additional packages:
521 sudo aptitude -y install gcc make libc6-dev
523 If you are running from the Live CD, you probably also want to free up
524 some RAM disk space with the following:
528 NOTE: While the instructions above mention the Live CD because that
529 makes it easy for people new to Linux, you can obviously pick a
530 different Debian ISO image and install it to your hard drive. Although
531 there are many different ways to download and install Debian, the
532 "netinst" ISO image (such as "C<debian-500-i386-netinst.iso>" represents
533 a great option because it keeps your initial download small (but still
534 lets you install anything you want "over the network").
536 Here are some tips if you are running from a live CD and are running
537 out of disk space (which really means you are running out of RAM):
543 Always run "C<aptitude clean>" after you install new packages to delete
544 the original .deb files (the files installed B<by> the .deb package
545 B<will> remain available, just the .deb package itself is deleted).
549 If you are installing modules from CPAN, you can free up some space with
550 "C<rm -rf /root/.cpan/*>" (change "/root/" in the previous command to
551 match your home directory or the location where CPAN has been configured
552 to perform build operations).
556 If necessary, you can remove the cached package information with the
557 command "C<rm -f /var/lib/apt/lists/*>". You can later pull this
558 information again via the command "C<aptitude update>".
562 You can save a small amount of space by commenting out the lines in
563 C</etc/apt/sources.list> that reference "deb-src" and
564 "security.debian.org". If you have already done an "C<aptitude update>"
565 with these repositories enabled, you can use the tip in the previous
566 bullet to free the space up (and then do another "C<aptitude update>").
570 Although you can free up space by removing packages you installed since
571 you last booted (check out "C<aptitude remove _pkg_name_>"), don't bother
572 trying to remove packages already available at the time of boot. Instead
573 of freeing up space, it will actual consume I<more> space. (The live CD
574 uses these "burn in" packages right from the CD disk vs. first loading
575 them on the virtual RAM disk. However, if you remove them, the system
576 has to update various files, something that I<does> consume some space
577 on the virtual RAM disk.)
587 Ubuntu is a popular offshoot of Debian. It provides cutting edge
588 versions of many common tools, application and libraries in an
589 easy-to-run live CD configuration (and because a single download option
590 can be used for both live CD and install-to-disk usage, it keeps your
591 download options nice and simple). As with Debian 6, you should be able
592 to generate a fully function Catalyst environment in a matter of
593 minutes. Here are quick instructions on how to use Ubuntu to prepare
600 Download the Ubuntu Desktop edition and boot from the CD and/or image
601 file, select your language, and then "Try Ubuntu without any changes to
606 Open a terminal session (click "Applications" in the upper-left corner,
607 then "Accessories," then "Terminal").
611 Add the 'universe' repositories:
613 sudo gedit /etc/apt/sources.list
615 And remove the comments from the lines under the comments about the
616 'universe' repositories.
623 sudo aptitude install libdbd-sqlite3-perl libcatalyst-perl libcatalyst-modules-perl libconfig-general-perl
625 Accept all of the dependencies. Done.
627 If you are running from the Live CD, you probably also want to free up
628 some disk space with the following:
632 NOTE: While the instructions above mention the live CD because that
633 makes it easy for people new to Linux, you can obviously also use one of
634 the options to install Ubuntu on your drive.
640 Matt Trout's C<cat-install>
642 Available at L<http://www.shadowcat.co.uk/static/cat-install>,
643 C<cat-install> can be a fairly painless way to get Catalyst up and
644 running. Just download the script from the link above and type
645 C<perl cat-install>. Depending on the speed of your Internet connection
646 and your computer, it will probably take 30 to 60 minutes to install
647 because it downloads, makes, compiles, and tests every module. But this
648 is an excellent way to automate the installation of all the latest
649 modules used by Catalyst from CPAN.
662 The 2008 Advent Day 4 entry has more information on using OpenBSD
663 packages to quickly build a system:
664 L<http://www.catalystframework.org/calendar/2008/4>.
668 NetBSD Package Collection on Solaris
670 The 2008 Advent Day 15 entry has more information on using C<pkgsrc> and
671 NetBSD packages on Solaris:
672 L<http://www.catalystframework.org/calendar/2008/15>.
678 You can get more information at
679 L<http://www.catalystframework.org/calendar/2008/7> or
680 L<Perl::Dist::CatInABox|Perl::Dist::CatInABox>.
684 Frank Speiser's Amazon EC2 Catalyst SDK
686 There are currently two flavors of publicly available Amazon Machine
687 Images (AMI) that include all the elements you'd need to begin
688 developing in a fully functional Catalyst environment within minutes.
689 See L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>
696 For additional information and recommendations on Catalyst installation,
698 L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>.
703 This tutorial will primarily focus on SQLite because of its simplicity
704 of installation and use; however, modifications in the script required
705 to support MySQL and PostgreSQL will be presented in the Appendix.
707 B<Note:> One of the advantages of using tools like Catalyst and DBIC is
708 that applications become much more database independent. As such, you
709 will notice that only the C<.sql> files used to initialize the database
710 change between database systems: most of the code generally remains the
714 =head1 WHERE TO GET WORKING CODE
716 Each chapter of the tutorial has complete code available as a tarball in
717 the main Catalyst Subversion repository (see the note at the beginning
718 of each part for the appropriate svn command to use).
720 B<NOTE:> You can run the test cases for the final code through Chapter 8
721 with the following commands:
723 svn co http://dev.catalystframework.org/repos/Catalyst/trunk/examples/Tutorial/MyApp_Chapter8
724 cd MyApp_Chapter8/MyApp
725 CATALYST_DEBUG=0 prove -wl t
727 If you wish to include the L<HTML::FormFu|HTML::FormFu> section in your tests,
728 substitute C<MyApp_Chapter9_FormFu> for C<MyApp_Chapter8> in the URL
729 above (don't forget to "cd" out of the Ch8 directory if you ran the code above).
731 svn co http://dev.catalystframework.org/repos/Catalyst/trunk/examples/Tutorial/MyApp_Chapter9_FormFu
732 cd MyApp_Chapter9_FormFu/MyApp
733 CATALYST_DEBUG=0 prove -wl t
735 You can also fire up the application under the development server that is conveniently
736 built in to Catalyst. Just issue this command from the C<MyApp> directory where you
737 ran the test suite above:
739 script/myapp_server.pl
741 And the application will start. You can try out the application by
742 pulling up C<http://localhost:3000> in your web browser (as mentioned
743 earlier, change C<localhost> to a different IP address or DNS name if
744 you are running your web browser and your Catalyst development on
745 different boxes). We will obviously see more about how to use the
746 application as we go through the remaining chapters of the tutorial, but
747 for now you can log in using the username "test01" and a password of
753 Kennedy Clark, C<hkclark@gmail.com>
755 Please report any errors, issues or suggestions to the author. The
756 most recent version of the Catalyst Tutorial can be found at
757 L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
759 Copyright 2006-2010, Kennedy Clark, under the
760 Creative Commons Attribution Share-Alike License Version 3.0
761 (L<http://creativecommons.org/licenses/by-sa/3.0/us/>).