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