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