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