Add some docs about installing X Windows
[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
232da04a 59This tutorial provides a multi-part introduction to the Catalyst Web
60Framework. It seeks to provide a rapid overview of many of its most
d442cc9f 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
8168726b 67authentication and authorization to an existing application, and/or
68form management).
d442cc9f 69
232da04a 70The most recent code for the tutorial is included on the Tutorial Virtual
71Machine you can download from:
d442cc9f 72
014065cb 73L<http://cattut.shadowcat.co.uk/>
74
75See L</STARTING WITH THE TUTORIAL VIRTUAL MACHINE> below for
76instructions getting and using the VM.
77
78Should you wish to download the code directly, you get pull it via the
79following command (note: will probably be switching to git soon):
80
028b4e1a 81 svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial
d442cc9f 82
75b13da6 83This will download the most recent code for each chapter of the
84tutorial into the CatalystTutorial directory on your machine.
d442cc9f 85
81a33ef3 86These reference implementations are provided so that when you follow
232da04a 87the tutorial, you can use the code to ensure that your system is set up
88correctly (which shouldn't be an issue if you use the Tutorial Virtual
89Machine), :-) and that you have not inadvertently made any typographic
81a33ef3 90errors, or accidentally skipped part of the tutorial.
3d9ae335 91
8168726b 92B<NOTE: You can use any Perl-supported OS and environment to run
93Catalyst.> It should make little or no difference to Catalyst's
232da04a 94operation, B<but this tutorial has been written using the Debian-based
81a33ef3 95Tutorial Virtual Machine> that you can download and use to work through
232da04a 96the full tutorial step by step. B<WE STRONGLY RECOMMEND THAT YOU USE
97THE VIRTUAL MACHINE IMAGE TO WORK THROUGH THE TUTORIAL> to avoid issues
98that may crop up if you are working with a different configuration. We
99have tested the Tutorial Virtual Machine to make sure all of the
100examples work correctly, but it is hard to guarantee this on other
101platforms and versions.
102
103If you would prefer to install directly from CPAN and not use the
104Tutorial Virtual machine, you can download the example program and all
105the necessary dependencies to your local machine by installing the
106C<Task::Catalyst::Tutorial> distribution:
d442cc9f 107
108 cpan Task::Catalyst::Tutorial
109
110This will also test to make sure the dependencies are working. If you
111have trouble installing these, please ask for help on the #catalyst
112IRC channel, or the Catalyst mailing list.
113
3533daff 114Subjects covered by the tutorial include:
d442cc9f 115
116=over 4
117
118=item *
119
120A simple application that lists and adds books.
121
122=item *
123
2217b252 124The use of L<DBIx::Class> (DBIC) for the model (including
8168726b 125some of the more advanced techniques you will probably want to use in
acbd7bdd 126your applications).
d442cc9f 127
128=item *
129
130How to write CRUD (Create, Read, Update, and Delete) operations in
131Catalyst.
132
133=item *
134
135Authentication ("auth").
136
137=item *
138
139Role-based authorization ("authz").
140
141=item *
142
8168726b 143Attempts to provide an example showing current (5.9) Catalyst
144practices.
d442cc9f 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
8168726b 158PostgreSQL). (Note: Because we make use of the DBIx::Class Object
444d6b27 159Relational Mapping [ORM] layer, out our application will be database
8168726b 160agnostic and can easily be used by any of the databases supported by
161DBIx::Class.)
d442cc9f 162
163=item *
164
2217b252 165The use of L<HTML::FormFu> or L<HTML::FormHandler>
0abc72ed 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
232da04a 183applications can be found at
184L<http://wiki.catalystframework.org/wiki/resources/catalystexamples> and
185in the C<examples> area of the Catalyst Subversion repository at
d442cc9f 186L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/>.
1390ef0e 187
2e73e2be 188
232da04a 189=head1 STARTING WITH THE TUTORIAL VIRTUAL MACHINE
2e73e2be 190
ec5b24b2 191The steps below briefly outline how you can download the Tutorial
192Virtual Machine. This document uses the term "host machine" to refer to
193the physical machine where you will run the virtualization software and
194boot up the VM. The terms "guest machine" or just "VM" refer to the
195virtual machine itself -- the thing where you actually do the tutorial
f0deaac8 196(and that you boot up on the "host machine").
197B<Note:> Throughout the tutorial, we will shows the UNIX shell prompt
198as "C<$>". If you are using Tutorial VM, the prompt will really be
199"C<root@catalyst:~#>", but we will keep it short (and also use "C<$>" in
200lieu of "C<#>", since "C<#>" looks too much like a Perl comment).
ec5b24b2 201
202
2e73e2be 203=over 4
204
205=item 1
206
014065cb 207Download a Tutorial Virtual Machine image from
208L<http://cattut.shadowcat.co.uk/>
209
81a33ef3 210B<A big thanks to Shadowcat Systems for hosting the virtual machines>
211B<(and everything else they do for the Perl community)!>
2e73e2be 212
213=item 2
214
9be506a8 215Uncompress the image on the "host machine":
232da04a 216
9be506a8 217 MAINCOMPUTER:~$ tar zxvf CatalystTutorial.tgz
2e73e2be 218
219=item 3
220
232da04a 221Boot the virtual machine using a tool like VMWare Player
222L<http://www.vmware.com/products/player> or VirtualBox
223L<http://www.virtualbox.org/>.
2e73e2be 224
225=item 4
226
232da04a 227Once you get a login prompt, enter the username B<root> and a password
228for C<catalyst>. You should now be at a prompt that looks like:
2e73e2be 229
3f97ca7b 230 catalyst login: root
231 Password: catalyst
232 ...
232da04a 233 root@catalyst:~#
2e73e2be 234
235=item 5
236
232da04a 237Type "C<ifconfig>" to get the IP address assigned to the virtual
238machine. You should get output along the lines of:
2e73e2be 239
232da04a 240 eth0 Link encap:Ethernet HWaddr 00:01:22:3b:45:69
ec5b24b2 241 inet addr:192.168.0.12 Bcast:192.168.0.255 Mask:255.255.255.0
2e73e2be 242 ...
243
232da04a 244You want the IP address on the second line below the C<eth0> interface.
245The image it design to automatically use a DHCP-assigned address.
246
232da04a 247
ec5b24b2 248Try to ping this IP address from your "host machine" (main desktop):
2e73e2be 249
2e73e2be 250
9be506a8 251 MAINCOMPUTER:~$ ping 192.168.0.12
ec5b24b2 252 PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
253 64 bytes from 192.168.0.12: icmp_req=1 ttl=255 time=4.97 ms
254 64 bytes from 192.168.0.12: icmp_req=2 ttl=255 time=3.43 ms
255 ...
2e73e2be 256
2e73e2be 257
ec5b24b2 258B<Note:> The ping above is being originated B<from> your B<host machine>
259(main desktop) and going B<to> your guest B<virtual machine>, not the
260other way around.
261
262If you are not seeing a valid IP address or it's not responding to pings
263(for example, you get error messages along the lines of "Request timed
264out", "100% packet loss", or "Destination Host Unreachable"), there
265could be a few network-related issues you might need to sort out. See
014065cb 266the section below L</Sorting Out Virtual Machine Network-Related Issues>
ec5b24b2 267for additional information and troubleshooting advice.
268
81a33ef3 269B<Note:> Remember this IP address... you will be using it throughout the
ec5b24b2 270tutorial.
271
232da04a 272
273=item 6
274
ec5b24b2 275B<From your main desktop machine>, open an SSH client and connect to the
276IP address found in the previous step. You should get a login prompt
277(accept the SSH key if you get a warning message about that). Login
278with the same username and password as we used in Step 4: B<root> /
279B<catalyst>
280
281 catalyst login: root
282 Password: catalyst
283 ...
284 root@catalyst:~#
285
2e73e2be 286
287=item 7
288
232da04a 289B<Using the SSH session>, change to the sample code directory for
290Chapter 3 included with the Tutorial Virtual Machine and start the
291Catalyst Development Server:
2e73e2be 292
ec5b24b2 293 $ cd Final/Chapter03/MyApp
294 $ perl scripts/myapp_server
2e73e2be 295
296=item 8
297
ec5b24b2 298B<From your main desktop machine> (the "host machine"), open a web
299browser and go to B<http://A.B.C.D:3000/>, where C<A.B.C.D> is the IP
300address to your virtual machine that you looked up in Step 5. For
301example, if your virtual machine is using the IP address
302C<192.168.0.12>, you would put the following URL into your web browser:
232da04a 303
ec5b24b2 304 http://192.168.0.12:3000/
232da04a 305
306Make sure you don't forget the B<:3000> to use port 3000 instead of the
307usual port 80 that is used by HTTP by default.
2e73e2be 308
232da04a 309You should get a Catalyst Welcome Screen. If you do, feel free to jump
310right in to L<Chapter 2|Catalyst::Manual::Tutorial::02_CatalystBasics>
311of the tutorial. If you don't go get the Catalyst Welcome Screen, go
312back and carefully check each of the steps above.
313
314=item 9
315
46b6662a 316B<Optional:> Also, to reduce download size, the Tutorial VM just
317includes a minimal command-line environment. You are free to use
318Debian's very capable C<apt> package manager to install other packages.
319You will first want to pull the apt cache files with C<aptitude update>
320(or C<apt-get update> if you prefer apt-get).
321
322The VI/VIM editor is already installed on the Tutorial Virtual Machine.
323In order to reduce the size of the download, Emacs is not pre-installed.
324Since people obviously have very strong opinions about which editor is
325best, :-) fortunately it's very easy to install Emacs:
326
327 $ aptitude update
232da04a 328 $ aptitude install emacs
2e73e2be 329
46b6662a 330In general, it is expected that people will
331boot up the Tutorial VM on their main desktop (the "host machine" using
332the terminology above) and then use that main desktop machine to SSH and
333web browse into the "guest VM" as they work through the tutorial. If
334you wish to install X Windows (or any other packages), just use the
335C<aptitude> (or C<apt-get>) Debian commands. For example, to install
336X Windows with the Gnome desktop manager, you can do:
337
338 $ aptitude update
339 $ aptitude install gnome iceweasel
340
341Or, if you prefer KDE:
342
343 $ aptitude update
344 $ aptitude install kde iceweasel
345
346You can then start X Windows with:
347
348 $ startx
349
350
2e73e2be 351=back
352
353
ec5b24b2 354You may note that the Tutorial Virtual Machine uses L<local::lib> so
e684d796 355that the Perl modules are run from ~/perl5 (in this case, /root/perl5)
356vs. the usual location of your "system Perl". We recommend that you
357also consider using this very handy module. It can greatly ease the
358process of maintaining and testing different combinations or Perl
359modules across development, staging, and production servers. (The
360"relocatable Perl" feature can also be used to to run both the modules
361B<and> Perl itself from your home directory [or any other directory you
362chose]).
363
d35624c1 364B<Note>: Please provide feedback on how the Virtual Machine approach for
365the tutorial works for you. If you have suggestions or comments, you
366can reach the author through the email address at the bottom of this
367page or via an RT ticket at
368L<https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
369
370
e684d796 371
ec5b24b2 372=head2 Sorting Out Virtual Machine Network-Related Issues
373
374In general, using a virtual machine to work through the tutorial is
375*much* easier than trying to do it in other environments, especially if
376you are new to Catalyst (or Perl or CPAN or ...). However, it's
377possible that you could run into a few network-related issues. The good
378news is that there is lots of information about the issue available via
379search engines on the Internet. Here is some background information to
380get you started.
381
382In Step 5 of the prior section above, we assumed that a "Bridged Mode"
383configuration and DHCP will work (it should for most people). If DHCP
384is not working or is not available in your location, most virtual
385machine "host" environments let you select between one of several
386different types of networking between the "guest" and the "host"
387machine.
388
389 1) Bridged
390 2) NAT
391 3) Local host only
392
393The Tutorial Virtual Machine defaults to "Bridged" -- this should result
394in the VM acting like another device on your network that will get a
395different DHCP IP address than the host machine. The advantage of this
396approach, is that you can easily SSH and web browse to the guest virtual
397machine. In general, this is the best option if you want to be able to
398boot up the VM and then use your SSH client and web browser from your
399main machine to connect into the virtual machine.
400
401In some environments, you might have better luck with "NAT" (Network
402Address Translation) mode. With this configuration, the guest VM shares
403the same IP address as the host machine. The downside of this approach
404is that special configuration is required if you want to be able to SSH
405or web browse to the guest VM. The NAT option should automatically
406allow the VM "outbound connection" (e.g., to the Internet if you want to
407install additional Debian packages), but it requires special
408configuration if you want to get "inbound connections" that go from some
409other machine (including the "host machine") into the VM. Some virtual
410machine host environments let you configure a "static NAT" or "port
411forwarding" to reach the guest OS, but others omit this functionality.
412
413"Local host only" mode let's the guest VM and the host machine talk on a
414"private subnet" that other devices in your network cannot reach. This
415can work as long as you don't need to go from the VM to the Internet
416(for example, to install other Debian packages).
417
418Consult the documentation on your virtual machine host environment for
419help configuring the options above. Here are some links that might
420help:
421
422=over 4
423
424=item *
425
81a33ef3 426L<http://vmfaq.com/entry/34/>
ec5b24b2 427
428=item *
429
81a33ef3 430L<http://www.vmware.com/support/pubs/player_pubs.html>
ec5b24b2 431
432=item *
433
81a33ef3 434L<http://www.virtualbox.org/manual/ch06.html>
ec5b24b2 435
436=back
437
438
439
440
d442cc9f 441=head1 VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL
442
443This tutorial was built using the following resources. Please note that
a6b4cff5 444you may need to make adjustments for different environments and versions
445(note that trailing zeros in version numbers are not significant and may
446get dropped with some techniques for viewing them; for example, Catalyst
447v5.80020 might show up as 5.8002):
d442cc9f 448
449=over 4
450
451=item *
452
95968b61 453Debian 6 (Squeeze)
d442cc9f 454
455=item *
456
232da04a 457Catalyst v5.90002
dd88c3b6 458
22a67212 459=item *
dd88c3b6 460
232da04a 461Catalyst::Devel v1.34
d442cc9f 462
463=item *
464
232da04a 465DBIx::Class v0.08195
d442cc9f 466
fce83e5f 467=item *
468
232da04a 469Catalyst::Model::DBIC::Schema v0.54
2e73e2be 470
471=item *
472
95968b61 473Template Toolkit v2.22
fce83e5f 474
2e73e2be 475
d442cc9f 476=item *
477
232da04a 478HTML::FormFu -- v0.09004
2e73e2be 479
d442cc9f 480=item *
481
865d3efb 482B<NOTE:> You can check the versions you have installed with the
95968b61 483following command (note the slash before the space):
484
485 perl -M<_mod_name_>\ 999
486
487or:
865d3efb 488
f63a9a2b 489 perl -M<_mod_name_> -e 'print "$<_mod_name_>::VERSION\n"'
865d3efb 490
491For example:
865d3efb 492
95968b61 493 perl -MCatalyst::Devel\ 999
444d6b27 494
495or:
496
497 perl -MCatalyst::Devel -e 'print "$Catalyst::Devel::VERSION\n";'
d442cc9f 498
499=item *
500
232da04a 501This tutorial will show URLs in the format of C<http://localhost:3000>,
502but if you are running your web browser from outside the Tutorial
503Virtual Machine, you will want to substitute the IP address of your VM
504for the C<localhost> in the URLs (again, you can get the IP address for
505eth0 from the C<ifconfig> command). For example, if your VM has an
ec5b24b2 506IP address of 192.168.0.12, you will want to use a base URL of
507C<http://192.168.0.12:3000>. Note that the development server
232da04a 508defaults to port 3000 (you can change with with the "-p" option on the
509command line.
510
d3b61552 511B<Please Note:> Depending on the web browser you are using, you might
512need to hit C<Shift+Reload> or C<Ctrl+Reload> to pull a fresh page when
8168726b 513testing your application at various points (see
d3b61552 514L<http://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache> for a
515comprehensive list of options for each browser).
516
517Also, the C<-k> B<keepalive option> to the development server can be
518necessary with some browsers (B<especially Internet Explorer>).
0c51850e 519
d442cc9f 520=back
521
1390ef0e 522
d442cc9f 523=head1 DATABASES
524
525This tutorial will primarily focus on SQLite because of its simplicity
526of installation and use; however, modifications in the script required
a6b4cff5 527to support MySQL and PostgreSQL will be presented in the Appendix.
d442cc9f 528
a6b4cff5 529B<Note:> One of the advantages of using tools like Catalyst and DBIC is
530that applications become much more database independent. As such, you
531will notice that only the C<.sql> files used to initialize the database
532change between database systems: most of the code generally remains the
d442cc9f 533same.
534
1390ef0e 535
24acc5d7 536You can jump to the next chapter of the tutorial here:
537L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
538
539
d442cc9f 540=head1 AUTHOR
541
542Kennedy Clark, C<hkclark@gmail.com>
543
bb0999d3 544Feel free to contact the author for any errors or suggestions, but the
545best way to report issues is via the CPAN RT Bug system at
546L<https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
d442cc9f 547
bb0999d3 548Copyright 2006-2011, Kennedy Clark, under the
ec3ef4ad 549Creative Commons Attribution Share-Alike License Version 3.0
865d3efb 550(L<http://creativecommons.org/licenses/by-sa/3.0/us/>).