.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Catalyst::Manual::Tutorial::01_Intro 3pm" .TH Catalyst::Manual::Tutorial::01_Intro 3pm "2017-08-02" "perl v5.26.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Catalyst::Manual::Tutorial::01_Intro \- Catalyst Tutorial \- Chapter 1: Introduction .SH "OVERVIEW" .IX Header "OVERVIEW" This is \fBChapter 1 of 10\fR for the Catalyst tutorial. .PP Tutorial Overview .IP "1." 4 \&\fB01_Introduction\fR .IP "2." 4 Catalyst Basics .IP "3." 4 More Catalyst Basics .IP "4." 4 Basic \s-1CRUD\s0 .IP "5." 4 Authentication .IP "6." 4 Authorization .IP "7." 4 Debugging .IP "8." 4 Testing .IP "9." 4 Advanced \s-1CRUD\s0 .IP "10." 4 Appendices .SH "DESCRIPTION" .IX Header "DESCRIPTION" This tutorial provides a multi-part introduction to the Catalyst Web Framework. It seeks to provide a rapid overview of many of its most commonly used features. The focus is on the real-world best practices required in the construction of nearly all Catalyst applications. .PP Although the primary target of the tutorial is users new to the Catalyst framework, experienced users may wish to review specific sections (for example, how to use \s-1DBIC\s0 for their model classes, how to add authentication and authorization to an existing application, and/or form management). .PP The most recent code for the tutorial is included on the Tutorial Virtual Machine you can download from: .PP .PP See \*(L"\s-1STARTING WITH THE TUTORIAL VIRTUAL MACHINE\*(R"\s0 below for instructions getting and using the \s-1VM.\s0 .PP Should you wish to download the code directly, you get pull it via the following command (note: will probably be switching to git soon): .PP .Vb 1 \& svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial .Ve .PP This will download the most recent code for each chapter of the tutorial into the CatalystTutorial directory on your machine. .PP These reference implementations are provided so that when you follow the tutorial, you can use the code to ensure that your system is set up correctly (which shouldn't be an issue if you use the Tutorial Virtual Machine), :\-) and that you have not inadvertently made any typographic errors, or accidentally skipped part of the tutorial. .PP \&\fB\s-1NOTE:\s0 You can use any Perl-supported \s-1OS\s0 and environment to run Catalyst.\fR It should make little or no difference to Catalyst's operation, \fBbut this tutorial has been written using the Debian-based Tutorial Virtual Machine\fR that you can download and use to work through the full tutorial step by step. \fB\s-1WE STRONGLY RECOMMEND THAT YOU USE THE VIRTUAL MACHINE IMAGE TO WORK THROUGH THE TUTORIAL\s0\fR to avoid issues that may crop up if you are working with a different configuration. We have tested the Tutorial Virtual Machine to make sure all of the examples work correctly, but it is hard to guarantee this on other platforms and versions. .PP If you would prefer to install directly from \s-1CPAN\s0 and not use the Tutorial Virtual machine, you can download the example program and all the necessary dependencies to your local machine by installing the \&\f(CW\*(C`Task::Catalyst::Tutorial\*(C'\fR distribution: .PP .Vb 1 \& cpan Task::Catalyst::Tutorial .Ve .PP This will also test to make sure the dependencies are working. If you have trouble installing these, please ask for help on the #catalyst \&\s-1IRC\s0 channel, or the Catalyst mailing list. .PP Subjects covered by the tutorial include: .IP "\(bu" 4 A simple application that lists and adds books. .IP "\(bu" 4 The use of DBIx::Class (\s-1DBIC\s0) for the model (including some of the more advanced techniques you will probably want to use in your applications). .IP "\(bu" 4 How to write \s-1CRUD\s0 (Create, Read, Update, and Delete) operations in Catalyst. .IP "\(bu" 4 Authentication (\*(L"auth\*(R"). .IP "\(bu" 4 Role-based authorization (\*(L"authz\*(R"). .IP "\(bu" 4 Attempts to provide an example showing current (5.9) Catalyst practices. .IP "\(bu" 4 The use of Template Toolkit (\s-1TT\s0). .IP "\(bu" 4 Useful techniques for troubleshooting and debugging Catalyst applications. .IP "\(bu" 4 The use of SQLite as a database (with code also provided for MySQL and PostgreSQL). (Note: Because we make use of the DBIx::Class Object Relational Mapping [\s-1ORM\s0] layer, out our application will be database agnostic and can easily be used by any of the databases supported by DBIx::Class.) .IP "\(bu" 4 The use of HTML::FormFu or HTML::FormHandler for automated form processing and validation. .PP This tutorial makes the learning process its main priority. For example, the level of comments in the code found here would likely be considered excessive in a \*(L"normal project.\*(R" Because of their contextual value, this tutorial will generally favor inline comments over a separate discussion in the text. It also deliberately tries to demonstrate multiple approaches to various features (in general, you should try to be as consistent as possible with your own production code). .PP Furthermore, this tutorial tries to minimize the number of controllers, models, \s-1TT\s0 templates, and database tables. Although this does result in things being a bit contrived at times, the concepts should be applicable to more complex environments. More complete and complicated example applications can be found at and in the \f(CW\*(C`examples\*(C'\fR area of the Catalyst Subversion repository at . .SH "STARTING WITH THE TUTORIAL VIRTUAL MACHINE" .IX Header "STARTING WITH THE TUTORIAL VIRTUAL MACHINE" The steps below briefly outline how you can download the Tutorial Virtual Machine. This document uses the term \*(L"host machine\*(R" to refer to the physical machine where you will run the virtualization software and boot up the \s-1VM.\s0 The terms \*(L"guest machine\*(R" or just \*(L"\s-1VM\*(R"\s0 refer to the virtual machine itself \*(-- the thing where you actually do the tutorial (and that you boot up on the \*(L"host machine\*(R"). .PP \&\fBNote:\fR Throughout the tutorial, we will shows the \s-1UNIX\s0 shell prompt as "\f(CW\*(C`$\*(C'\fR\*(L". If you are using the Tutorial \s-1VM,\s0 the prompt will really be \&\*(R"\f(CW\*(C`catalyst@catalyst:~$\*(C'\fR\*(L" (where \*(R"\f(CW\*(C`~"\*(C'\fR will change to show your current directory), but we will keep it short and just use "\f(CW\*(C`$\*(C'\fR". .IP "1." 4 Download a Tutorial Virtual Machine image from .Sp \&\fBA big thanks to Shadowcat Systems for hosting the virtual machines\fR \&\fB(and everything else they do for the Perl community)!\fR .IP "2." 4 Uncompress the image on the \*(L"host machine\*(R": .Sp .Vb 1 \& MAINCOMPUTER:~$ tar zxvf CatalystTutorial.tgz .Ve .IP "3." 4 Boot the virtual machine using a tool like VMWare Player or VirtualBox . .IP "4." 4 Once you get a login prompt, enter the username \fBcatalyst\fR and a password for \f(CW\*(C`catalyst\*(C'\fR. You should now be at a prompt that looks like: .Sp .Vb 4 \& catalyst login: catalyst \& Password: catalyst \& ... \& catalyst@catalyst:~$ .Ve .IP "5." 4 Type "\f(CW\*(C`ifconfig\*(C'\fR" to get the \s-1IP\s0 address assigned to the virtual machine. You should get output along the lines of: .Sp .Vb 3 \& eth0 Link encap:Ethernet HWaddr 00:01:22:3b:45:69 \& inet addr:192.168.0.12 Bcast:192.168.0.255 Mask:255.255.255.0 \& ... .Ve .Sp You want the \s-1IP\s0 address on the second line below the \f(CW\*(C`eth0\*(C'\fR interface. The image it design to automatically use a DHCP-assigned address. .Sp Try to ping this \s-1IP\s0 address from your \*(L"host machine\*(R" (main desktop): .Sp .Vb 5 \& MAINCOMPUTER:~$ ping 192.168.0.12 \& PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data. \& 64 bytes from 192.168.0.12: icmp_req=1 ttl=255 time=4.97 ms \& 64 bytes from 192.168.0.12: icmp_req=2 ttl=255 time=3.43 ms \& ... .Ve .Sp \&\fBNote:\fR The ping above is being originated \fBfrom\fR your \fBhost machine\fR (main desktop) and going \fBto\fR your guest \fBvirtual machine\fR, not the other way around. .Sp If you are not seeing a valid \s-1IP\s0 address or it's not responding to pings (for example, you get error messages along the lines of \*(L"Request timed out\*(R", \*(L"100% packet loss\*(R", or \*(L"Destination Host Unreachable\*(R"), there could be a few network-related issues you might need to sort out. See the section below \*(L"Sorting Out Virtual Machine Network-Related Issues\*(R" for additional information and troubleshooting advice. .Sp \&\fBNote:\fR Remember this \s-1IP\s0 address... you will be using it throughout the tutorial. .IP "6." 4 \&\fBFrom your main desktop machine\fR, open an \s-1SSH\s0 client and connect to the \&\s-1IP\s0 address found in the previous step. You should get a login prompt (accept the \s-1SSH\s0 key if you get a warning message about that). Login with the same username and password as we used in Step 4: \fBcatalyst\fR / \&\fBcatalyst\fR .Sp .Vb 4 \& catalyst login: catalyst \& Password: catalyst \& ... \& catalyst@catalyst:~$ .Ve .IP "7." 4 \&\fBUsing the \s-1SSH\s0 session\fR, change to the sample code directory for Chapter 3 included with the Tutorial Virtual Machine and start the Catalyst Development Server: .Sp .Vb 2 \& $ cd Final/Chapter03/MyApp \& $ perl script/myapp_server.pl .Ve .IP "8." 4 \&\fBFrom your main desktop machine\fR (the \*(L"host machine\*(R"), open a web browser and go to \fBhttp://A.B.C.D:3000/\fR, where \f(CW\*(C`A.B.C.D\*(C'\fR is the \s-1IP\s0 address to your virtual machine that you looked up in Step 5. For example, if your virtual machine is using the \s-1IP\s0 address \&\f(CW192.168.0.12\fR, you would put the following \s-1URL\s0 into your web browser: .Sp .Vb 1 \& http://192.168.0.12:3000/ .Ve .Sp Make sure you don't forget the \fB:3000\fR to use port 3000 instead of the usual port 80 that is used by \s-1HTTP\s0 by default. .Sp You should get a Catalyst Welcome Screen. If you do, feel free to jump right in to Chapter 2 of the tutorial. If you don't go get the Catalyst Welcome Screen, go back and carefully check each of the steps above. .IP "9." 4 \&\fBOptional:\fR Also, to reduce download size, the Tutorial \s-1VM\s0 just includes a minimal command-line environment. You are free to use Debian's very capable \f(CW\*(C`apt\*(C'\fR package manager to install other packages. You will first want to pull the apt cache files with \f(CW\*(C`aptitude update\*(C'\fR (or \f(CW\*(C`apt\-get update\*(C'\fR if you prefer apt-get). .Sp The \s-1VI/VIM\s0 editor is already installed on the Tutorial Virtual Machine. In order to reduce the size of the download, Emacs is not pre-installed. Since people obviously have very strong opinions about which editor is best, :\-) fortunately it's very easy to install Emacs: .Sp .Vb 2 \& $ sudo aptitude update \& $ sudo aptitude install emacs .Ve .Sp In general, it is expected that people will boot up the Tutorial \s-1VM\s0 on their main desktop (the \*(L"host machine\*(R" using the terminology above) and then use that main desktop machine to \s-1SSH\s0 and web browse into the \*(L"guest \s-1VM\*(R"\s0 as they work through the tutorial. If you wish to install X Windows (or any other packages), just use the \&\f(CW\*(C`aptitude\*(C'\fR (or \f(CW\*(C`apt\-get\*(C'\fR) Debian commands. .Sp For example, to install X Windows with Fluxbox (a lightweight WindowManager \*(-- it is great for things like this tutorial since it's about 1/10th the size of other common X Windows environments), you can do: .Sp .Vb 2 \& $ sudo aptitude update \& $ sudo aptitude install xorg fluxbox iceweasel .Ve .Sp And then start X Windows from the \fB\s-1VM\s0 Console\fR with this command: .Sp .Vb 1 \& $ startx .Ve .Sp Note that if you want to start Fluxbox from an \s-1SSH\s0 session, you can use the \f(CW\*(C`sudo dpkg\-reconfigure x11\-common\*(C'\fR and select \*(L"anybody\*(R" from the menu. Otherwise, you will need to be on the actual \*(L"\s-1VM\s0 console\*(R" to start it. .Sp If you have a preference for the Gnome desktop environment, you can do: .Sp .Vb 5 \& $ sudo aptitude update \& $ sudo aptitude install gnome iceweasel \& $ \& $ # You can reboot or start with \*(Aqstartx\*(Aq, we will just reboot here \& $ reboot .Ve .Sp For \s-1KDE,\s0 just substitute the package name "\f(CW\*(C`kde\*(C'\fR\*(L" for \*(R"\f(CW\*(C`gnome\*(C'\fR" above. .Sp .Vb 1 \& $ sudo aptitude install kde iceweasel .Ve .Sp Note that \f(CW\*(C`iceweasel\*(C'\fR is basically used to install Firefox on Debian boxes. You can start it under X Windows with either the \f(CW\*(C`firefox\*(C'\fR command or the \f(CW\*(C`iceweasel\*(C'\fR command (or use the menus). You can get more information on Iceweasel at . .Sp Also, you might need to add more memory to your virtual machine if you want to run X Windows (or other tools that might require additional memory). Consult the documentation of your virtualization software for instructions on how to do this (it's usually pretty simple). .PP You may note that the Tutorial Virtual Machine uses local::lib so that the Perl modules are run from ~/perl5 (in this case, /home/catalyst/perl5) vs. the usual location of your \*(L"system Perl\*(R". We recommend that you also consider using this very handy module. It can greatly ease the process of maintaining and testing different combinations or Perl modules across development, staging, and production servers. (The \*(L"relocatable Perl\*(R" feature can also be used to run both the modules \fBand\fR Perl itself from your home directory [or any other directory you chose]). .PP \&\fBNote\fR: Please provide feedback on how the Virtual Machine approach for the tutorial works for you. If you have suggestions or comments, you can reach the author through the email address at the bottom of this page or via an \s-1RT\s0 ticket at . .SS "Sorting Out Virtual Machine Network-Related Issues" .IX Subsection "Sorting Out Virtual Machine Network-Related Issues" In general, using a virtual machine to work through the tutorial is *much* easier than trying to do it in other environments, especially if you are new to Catalyst (or Perl or \s-1CPAN\s0 or ...). However, it's possible that you could run into a few network-related issues. The good news is that there is lots of information about the issue available via search engines on the Internet. Here is some background information to get you started. .PP In Step 5 of the prior section above, we assumed that a \*(L"Bridged Mode\*(R" configuration and \s-1DHCP\s0 will work (it should for most people). If \s-1DHCP\s0 is not working or is not available in your location, most virtual machine \*(L"host\*(R" environments let you select between one of several different types of networking between the \*(L"guest\*(R" and the \*(L"host\*(R" machine. .PP .Vb 3 \& 1) Bridged \& 2) NAT \& 3) Local host only .Ve .PP The Tutorial Virtual Machine defaults to \*(L"Bridged\*(R" \*(-- this should result in the \s-1VM\s0 acting like another device on your network that will get a different \s-1DHCP IP\s0 address than the host machine. The advantage of this approach, is that you can easily \s-1SSH\s0 and web browse to the guest virtual machine. In general, this is the best option if you want to be able to boot up the \s-1VM\s0 and then use your \s-1SSH\s0 client and web browser from your main machine to connect into the virtual machine. .PP In some environments, you might have better luck with \*(L"\s-1NAT\*(R"\s0 (Network Address Translation) mode. With this configuration, the guest \s-1VM\s0 shares the same \s-1IP\s0 address as the host machine. The downside of this approach is that special configuration is required if you want to be able to \s-1SSH\s0 or web browse to the guest \s-1VM.\s0 The \s-1NAT\s0 option should automatically allow the \s-1VM\s0 \*(L"outbound connection\*(R" (e.g., to the Internet if you want to install additional Debian packages), but it requires special configuration if you want to get \*(L"inbound connections\*(R" that go from some other machine (including the \*(L"host machine\*(R") into the \s-1VM.\s0 Some virtual machine host environments let you configure a \*(L"static \s-1NAT\*(R"\s0 or \*(L"port forwarding\*(R" to reach the guest \s-1OS,\s0 but others omit this functionality. .PP Note: \s-1NAT\s0 mode can work fine if you install X Windows and do the whole tutorial locally on the actual \s-1VM\s0 vs. using \s-1SSH\s0 and a web browser from your host machine. .PP \&\*(L"Local host only\*(R" mode let's the guest \s-1VM\s0 and the host machine talk on a \&\*(L"private subnet\*(R" that other devices in your network cannot reach. This can work as long as you don't need to go from the \s-1VM\s0 to the Internet (for example, to install other Debian packages). .PP Consult the documentation on your virtual machine host environment for help configuring the options above. Here are some links that might help: .IP "\(bu" 4 .IP "\(bu" 4 .IP "\(bu" 4 .SH "VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL" .IX Header "VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL" This tutorial was built using the following resources. Please note that you may need to make adjustments for different environments and versions (note that trailing zeros in version numbers are not significant and may get dropped with some techniques for viewing them; for example, Catalyst v5.80020 might show up as 5.8002): .IP "\(bu" 4 Debian 6 (Squeeze) .IP "\(bu" 4 Catalyst v5.90002 .IP "\(bu" 4 Catalyst::Devel v1.34 .IP "\(bu" 4 DBIx::Class v0.08195 .IP "\(bu" 4 Catalyst::Model::DBIC::Schema v0.54 .IP "\(bu" 4 Template Toolkit v2.22 .IP "\(bu" 4 HTML::FormFu \*(-- v0.09004 .IP "\(bu" 4 \&\fB\s-1NOTE:\s0\fR You can check the versions you have installed with the following command (note the slash before the space): .Sp .Vb 1 \& perl \-M<_mod_name_>\e 999 .Ve .Sp or: .Sp .Vb 1 \& perl \-M<_mod_name_> \-e \*(Aqprint "$<_mod_name_>::VERSION\en"\*(Aq .Ve .Sp For example: .Sp .Vb 1 \& perl \-MCatalyst::Devel\e 999 .Ve .Sp or: .Sp .Vb 1 \& perl \-MCatalyst::Devel \-e \*(Aqprint "$Catalyst::Devel::VERSION\en";\*(Aq .Ve .IP "\(bu" 4 This tutorial will show URLs in the format of \f(CW\*(C`http://localhost:3000\*(C'\fR, but if you are running your web browser from outside the Tutorial Virtual Machine, you will want to substitute the \s-1IP\s0 address of your \s-1VM\s0 for the \f(CW\*(C`localhost\*(C'\fR in the URLs (again, you can get the \s-1IP\s0 address for eth0 from the \f(CW\*(C`ifconfig\*(C'\fR command). For example, if your \s-1VM\s0 has an \&\s-1IP\s0 address of 192.168.0.12, you will want to use a base \s-1URL\s0 of \&\f(CW\*(C`http://192.168.0.12:3000\*(C'\fR. Note that the development server defaults to port 3000 (you can change with the \*(L"\-p\*(R" option on the command line). .Sp \&\fBPlease Note:\fR Depending on the web browser you are using, you might need to hit \f(CW\*(C`Shift+Reload\*(C'\fR or \f(CW\*(C`Ctrl+Reload\*(C'\fR to pull a fresh page when testing your application at various points (see for a comprehensive list of options for each browser). .Sp Also, the \f(CW\*(C`\-k\*(C'\fR \fBkeepalive option\fR to the development server can be necessary with some browsers (\fBespecially Internet Explorer\fR). .SH "DATABASES" .IX Header "DATABASES" This tutorial will primarily focus on SQLite because of its simplicity of installation and use; however, modifications in the script required to support MySQL and PostgreSQL will be presented in the Appendix. .PP \&\fBNote:\fR One of the advantages of using tools like Catalyst and \s-1DBIC\s0 is that applications become much more database independent. As such, you will notice that only the \f(CW\*(C`.sql\*(C'\fR files used to initialize the database change between database systems: most of the code generally remains the same. .PP You can jump to the next chapter of the tutorial here: Catalyst Basics .SH "AUTHOR" .IX Header "AUTHOR" Kennedy Clark, \f(CW\*(C`hkclark@gmail.com\*(C'\fR .PP Feel free to contact the author for any errors or suggestions, but the best way to report issues is via the \s-1CPAN RT\s0 Bug system at . .PP Copyright 2006\-2011, Kennedy Clark, under the Creative Commons Attribution Share-Alike License Version 3.0 ().