NAME

Catalyst::Manual::Tutorial::01_Intro - Catalyst Tutorial - Chapter 1: Introduction

OVERVIEW

This is Chapter 1 of 10 for the Catalyst tutorial.

Tutorial Overview

  1. 01_Introduction

  2. Catalyst Basics

  3. More Catalyst Basics

  4. Basic CRUD

  5. Authentication

  6. Authorization

  7. Debugging

  8. Testing

  9. Advanced CRUD

  10. Appendices

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.

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 DBIC for their model classes, how to add authentication and authorization to an existing application, and/or form management).

The most recent code for the tutorial is included on the Tutorial Virtual Machine you can download from:

http://cattut.shadowcat.co.uk/

See "STARTING WITH THE TUTORIAL VIRTUAL MACHINE" below for instructions getting and using the VM.

Should you wish to download the code directly, you get pull it via the following command (note: will probably be switching to git soon):

    svn co http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/ CatalystTutorial

This will download the most recent code for each chapter of the tutorial into the CatalystTutorial directory on your machine.

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.

NOTE: You can use any Perl-supported OS and environment to run Catalyst. It should make little or no difference to Catalyst's operation, but this tutorial has been written using the Debian-based Tutorial Virtual Machine that you can download and use to work through the full tutorial step by step. WE STRONGLY RECOMMEND THAT YOU USE THE VIRTUAL MACHINE IMAGE TO WORK THROUGH THE TUTORIAL 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.

If you would prefer to install directly from CPAN 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 Task::Catalyst::Tutorial distribution:

     cpan Task::Catalyst::Tutorial

This will also test to make sure the dependencies are working. If you have trouble installing these, please ask for help on the #catalyst IRC channel, or the Catalyst mailing list.

Subjects covered by the tutorial include:

  • A simple application that lists and adds books.

  • The use of DBIx::Class (DBIC) for the model (including some of the more advanced techniques you will probably want to use in your applications).

  • How to write CRUD (Create, Read, Update, and Delete) operations in Catalyst.

  • Authentication ("auth").

  • Role-based authorization ("authz").

  • Attempts to provide an example showing current (5.9) Catalyst practices.

  • The use of Template Toolkit (TT).

  • Useful techniques for troubleshooting and debugging Catalyst applications.

  • 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 [ORM] layer, out our application will be database agnostic and can easily be used by any of the databases supported by DBIx::Class.)

  • The use of HTML::FormFu or HTML::FormHandler for automated form processing and validation.

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 "normal project." 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).

Furthermore, this tutorial tries to minimize the number of controllers, models, TT 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 http://wiki.catalystframework.org/wiki/resources/catalystexamples and in the examples area of the Catalyst Subversion repository at http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/.

STARTING WITH THE TUTORIAL VIRTUAL MACHINE

The steps below briefly outline how you can download the Tutorial Virtual Machine. This document uses the term "host machine" to refer to the physical machine where you will run the virtualization software and boot up the VM. The terms "guest machine" or just "VM" refer to the virtual machine itself -- the thing where you actually do the tutorial (and that you boot up on the "host machine").

Note: Throughout the tutorial, we will shows the UNIX shell prompt as "$". If you are using the Tutorial VM, the prompt will really be "catalyst@catalyst:~$" (where "~" will change to show your current directory), but we will keep it short and just use "$".

  1. Download a Tutorial Virtual Machine image from http://cattut.shadowcat.co.uk/

    A big thanks to Shadowcat Systems for hosting the virtual machines (and everything else they do for the Perl community)!

  2. Uncompress the image on the "host machine":

        MAINCOMPUTER:~$ tar zxvf CatalystTutorial.tgz
  3. Boot the virtual machine using a tool like VMWare Player https://www.vmware.com/products/workstation-player.html or VirtualBox https://www.virtualbox.org/.

  4. Once you get a login prompt, enter the username catalyst and a password for catalyst. You should now be at a prompt that looks like:

        catalyst login: catalyst
        Password: catalyst
        ...
        catalyst@catalyst:~$
  5. Type "ifconfig" to get the IP address assigned to the virtual machine. You should get output along the lines of:

        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
        ...

    You want the IP address on the second line below the eth0 interface. The image is designed to automatically use a DHCP-assigned address.

    Try to ping this IP address from your "host machine" (main desktop):

        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
        ...

    Note: The ping above is being originated from your host machine (main desktop) and going to your guest virtual machine, not the other way around.

    If you are not seeing a valid IP address or it's not responding to pings (for example, you get error messages along the lines of "Request timed out", "100% packet loss", or "Destination Host Unreachable"), there could be a few network-related issues you might need to sort out. See the section below "Sorting Out Virtual Machine Network-Related Issues" for additional information and troubleshooting advice.

    Note: Remember this IP address... you will be using it throughout the tutorial.

  6. From your main desktop machine, open an SSH client and connect to the IP address found in the previous step. You should get a login prompt (accept the SSH key if you get a warning message about that). Login with the same username and password as we used in Step 4: catalyst / catalyst

        catalyst login: catalyst
        Password: catalyst
        ...
        catalyst@catalyst:~$
  7. Using the SSH session, change to the sample code directory for Chapter 3 included with the Tutorial Virtual Machine and start the Catalyst Development Server:

        $ cd Final/Chapter03/MyApp
        $ perl script/myapp_server.pl
  8. From your main desktop machine (the "host machine"), open a web browser and go to http://A.B.C.D:3000/, where A.B.C.D is the IP address to your virtual machine that you looked up in Step 5. For example, if your virtual machine is using the IP address 192.168.0.12, you would put the following URL into your web browser:

        http://192.168.0.12:3000/

    Make sure you don't forget the :3000 to use port 3000 instead of the usual port 80 that is used by HTTP by default.

    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.

  9. Optional: Also, to reduce download size, the Tutorial VM just includes a minimal command-line environment. You are free to use Debian's very capable apt package manager to install other packages. You will first want to pull the apt cache files with aptitude update (or apt-get update if you prefer apt-get).

    The VI/VIM 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:

        $ sudo aptitude update
        $ sudo aptitude install emacs

    In general, it is expected that people will boot up the Tutorial VM on their main desktop (the "host machine" using the terminology above) and then use that main desktop machine to SSH and web browse into the "guest VM" as they work through the tutorial. If you wish to install X Windows (or any other packages), just use the aptitude (or apt-get) Debian commands.

    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:

        $ sudo aptitude update
        $ sudo aptitude install xorg fluxbox iceweasel

    And then start X Windows from the VM Console with this command:

        $ startx

    Note that if you want to start Fluxbox from an SSH session, you can use the sudo dpkg-reconfigure x11-common and select "anybody" from the menu. Otherwise, you will need to be on the actual "VM console" to start it.

    If you have a preference for the Gnome desktop environment, you can do:

        $ sudo aptitude update
        $ sudo aptitude install gnome iceweasel
        $
        $ # You can reboot or start with 'startx', we will just reboot here
        $ reboot

    For KDE, just substitute the package name "kde" for "gnome" above.

        $ sudo aptitude install kde iceweasel

    Note that iceweasel is basically used to install Firefox on Debian boxes. You can start it under X Windows with either the firefox command or the iceweasel command (or use the menus). You can get more information on Iceweasel at https://wiki.debian.org/Iceweasel.

    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).

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 "system Perl". 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 "relocatable Perl" feature can also be used to run both the modules and Perl itself from your home directory [or any other directory you chose]).

Note: 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 RT ticket at https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual.

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 CPAN 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.

In Step 5 of the prior section above, we assumed that a "Bridged Mode" configuration and DHCP will work (it should for most people). If DHCP is not working or is not available in your location, most virtual machine "host" environments let you select between one of several different types of networking between the "guest" and the "host" machine.

    1) Bridged
    2) NAT
    3) Local host only

The Tutorial Virtual Machine defaults to "Bridged" -- this should result in the VM acting like another device on your network that will get a different DHCP IP address than the host machine. The advantage of this approach, is that you can easily SSH 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 VM and then use your SSH client and web browser from your main machine to connect into the virtual machine.

In some environments, you might have better luck with "NAT" (Network Address Translation) mode. With this configuration, the guest VM shares the same IP address as the host machine. The downside of this approach is that special configuration is required if you want to be able to SSH or web browse to the guest VM. The NAT option should automatically allow the VM "outbound connection" (e.g., to the Internet if you want to install additional Debian packages), but it requires special configuration if you want to get "inbound connections" that go from some other machine (including the "host machine") into the VM. Some virtual machine host environments let you configure a "static NAT" or "port forwarding" to reach the guest OS, but others omit this functionality.

Note: NAT mode can work fine if you install X Windows and do the whole tutorial locally on the actual VM vs. using SSH and a web browser from your host machine.

"Local host only" mode let's the guest VM and the host machine talk on a "private subnet" that other devices in your network cannot reach. This can work as long as you don't need to go from the VM to the Internet (for example, to install other Debian packages).

Consult the documentation on your virtual machine host environment for help configuring the options above. Here are some links that might help:

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):

  • Debian 6 (Squeeze)

  • Catalyst v5.90002

  • Catalyst::Devel v1.34

  • DBIx::Class v0.08195

  • Catalyst::Model::DBIC::Schema v0.54

  • Template Toolkit v2.22

  • HTML::FormFu -- v0.09004

  • NOTE: You can check the versions you have installed with the following command (note the slash before the space):

        perl -M<_mod_name_>\ 999

    or:

        perl -M<_mod_name_> -e 'print "$<_mod_name_>::VERSION\n"'

    For example:

        perl -MCatalyst::Devel\ 999

    or:

        perl -MCatalyst::Devel -e 'print "$Catalyst::Devel::VERSION\n";'
  • This tutorial will show URLs in the format of http://localhost:3000, but if you are running your web browser from outside the Tutorial Virtual Machine, you will want to substitute the IP address of your VM for the localhost in the URLs (again, you can get the IP address for eth0 from the ifconfig command). For example, if your VM has an IP address of 192.168.0.12, you will want to use a base URL of http://192.168.0.12:3000. Note that the development server defaults to port 3000 (you can change with the "-p" option on the command line).

    Please Note: Depending on the web browser you are using, you might need to hit Shift+Reload or Ctrl+Reload to pull a fresh page when testing your application at various points (see https://en.wikipedia.org/wiki/Wikipedia:Bypass_your_cache for a comprehensive list of options for each browser).

    Also, the -k keepalive option to the development server can be necessary with some browsers (especially Internet Explorer).

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.

Note: One of the advantages of using tools like Catalyst and DBIC is that applications become much more database independent. As such, you will notice that only the .sql files used to initialize the database change between database systems: most of the code generally remains the same.

You can jump to the next chapter of the tutorial here: Catalyst Basics

AUTHOR

Kennedy Clark, hkclark@gmail.com

Feel free to contact the author for any errors or suggestions, but the best way to report issues is via the CPAN RT Bug system at https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual.

Copyright 2006-2011, Kennedy Clark, under the Creative Commons Attribution Share-Alike License Version 3.0 (https://creativecommons.org/licenses/by-sa/3.0/us/).