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