Catalyst::Manual::Tutorial::03_MoreCatalystBasics(3p)

NAME
Catalyst::Manual::Tutorial::03_MoreCatalystBasics - Catalyst Tutorial - Chapter 3: More Catalyst Application Development Basics

OVERVIEW
This is Chapter 3 of 10 for the Catalyst tutorial.

Tutorial Overview

1.  Introduction

2.  Catalyst Basics

3.  03_More Catalyst Basics

4.  Basic CRUD

5.  Authentication

6.  Authorization

7.  Debugging

8.  Testing

9.  Advanced CRUD

10. Appendices

DESCRIPTION
This chapter of the tutorial builds on the work done in Chapter 2 to explore some features that are more typical of "real world" web applications. From this chapter of the tutorial onward, we will be building a simple book database application. Although the application will be too limited to be of use to anyone, it should provide a basic environment where we can explore a variety of features used in virtually all web applications.

You can check out the source code for this example from the Catalyst Subversion repository as per the instructions in Catalyst::Manual::Tutorial::01_Intro.

Please take a look at "CATALYST INSTALLATION" in Catalyst::Manual::Tutorial::01_Intro before doing the rest of this tutorial. Although the tutorial should work correctly under most any recent version of Perl running on any operating system, the tutorial has been written using Debian 5 and tested to be sure it runs correctly in this environment.

CREATE A NEW APPLICATION
The remainder of the tutorial will build an application called "MyApp". First use the Catalyst "catalyst.pl" script to initialize the framework for the "MyApp" application (make sure you aren't still inside the directory of the "Hello" application from the previous chapter of the tutorial or in a directory that already has a "MyApp" subdirectory):

    $ catalyst.pl MyApp
    created "MyApp"
    created "MyApp/script"
    created "MyApp/lib"
    created "MyApp/root"
    ...
    created "MyApp/script/myapp_create.pl"
    $ cd MyApp

This creates a similar skeletal structure to what we saw in Chapter 2 of the tutorial, except with "MyApp" and "myapp" substituted for "Hello" and "hello".

EDIT THE LIST OF CATALYST PLUGINS
One of the greatest benefits of Catalyst is that it has such a large library of plugins and base classes available. Plugins are used to seamlessly integrate existing Perl modules into the overall Catalyst framework. In general, they do this by adding additional methods to the "context" object (generally written as $c) that Catalyst passes to every component throughout the framework.

By default, Catalyst enables three plugins/flags:

•   "-Debug" Flag

    Enables the Catalyst debug output you saw when we started the "script/myapp_server.pl" development server earlier. You can remove this item when you place your application into production.

    As you may have noticed, "-Debug" is not a plugin, but a flag. Although most of the items specified on the "__PACKAGE__->setup" line of your application class will be plugins, Catalyst supports a limited number of flag options (of these, "-Debug" is the most common). See the documentation for "Catalyst.pm" to get details on other flags (currently "-Engine", "-Home", and "-Log").

    If you prefer, you can use the "$c->debug" method to enable debug messages.

    TIP: Depending on your needs, it can be helpful to permanently remove "-Debug" from "lib/MyApp.pm" and then use the "-d" option to "script/myapp_server.pl" to re-enable it just for the development server. We will not be using that approach in the tutorial, but feel free to make use of it in your own projects.

•   Catalyst::Plugin::ConfigLoader

    "ConfigLoader" provides an automatic way to load configurable parameters for your application from a central Config::General file (versus having the values hard-coded inside your Perl modules). Config::General uses syntax very similar to Apache configuration files. We will see how to use this feature of Catalyst during the authentication and authorization sections (Chapter 5 and Chapter 6).

    IMPORTANT NOTE: If you are using a version of Catalyst::Devel prior to version 1.06, be aware that Catalyst changed the default format from YAML to the more straightforward "Config::General" style. This tutorial uses the newer "myapp.conf" file for "Config::General". However, Catalyst supports both formats and will automatically use either "myapp.conf" or "myapp.yml" (or any other format supported by Catalyst::Plugin::ConfigLoader and Config::Any). If you are using a version of Catalyst::Devel prior to 1.06, you can convert to the newer format by simply creating the "myapp.conf" file manually and deleting "myapp.yml". The default contents of the "myapp.conf" you create should only consist of one line:

        name MyApp

    TIP: This script can be useful for converting between configuration formats:

        perl -Ilib -e 'use MyApp; use Config::General;
            Config::General->new->save_file("myapp.conf", MyApp->config);'

•   Catalyst::Plugin::Static::Simple

    "Static::Simple" provides an easy way to serve static content, such as images and CSS files, from the development server.

For our application, we want to add one new plugin into the mix. To do this, edit "lib/MyApp.pm" (this file is generally referred to as your application class) and delete the lines with:

    use Catalyst qw/-Debug
                    ConfigLoader
                    Static::Simple/;

Then replace it with:

    # Load plugins
    use Catalyst qw/
                    -Debug
                    ConfigLoader
                    Static::Simple

                    StackTrace
                    /;

Note: Recent versions of "Catalyst::Devel" have used a variety of techniques to load these plugins/flags. For example, you might see the following:

    __PACKAGE__->setup(qw/-Debug ConfigLoader Static::Simple/);

Don't let these variations confuse you -- they all accomplish the same result.

This tells Catalyst to start using one new plugin, Catalyst::Plugin::StackTrace, to add a stack trace to the standard Catalyst "debug screen" (the screen Catalyst sends to your browser when an error occurs). Be aware that StackTrace output appears in your browser, not in the console window from which you're running your application, which is where logging output usually goes.

Make sure that when adding new plugins that you include them as a new dependancies within the Makefile.PL file. For example, after adding the StackTrace plugin the Makefile.PL should include the following line:

    requires 'Catalyst::Plugin::StackTrace';

Notes:

•   "__PACKAGE__" is just a shorthand way of referencing the name of the package where it is used. Therefore, in "MyApp.pm", "__PACKAGE__" is equivalent to "MyApp".

•   You will want to disable StackTrace before you put your application into production, but it can be helpful during development.

•   When specifying plugins on the "__PACKAGE__->setup" line, you can omit "Catalyst::Plugin::" from the name. Additionally, you can spread the plugin names across multiple lines as shown here, or place them all on one (or more) lines as with the default configuration.

CREATE A CATALYST CONTROLLER
As discussed earlier, controllers are where you write methods that interact with user input. Typically, controller methods respond to "GET" and "POST" requests from the user's web browser.

Use the Catalyst "create" script to add a controller for book-related actions:

    $ script/myapp_create.pl controller Books
     exists "/home/me/MyApp/script/../lib/MyApp/Controller"
     exists "/home/me/MyApp/script/../t"
    created "/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm"
    created "/home/me/MyApp/script/../t/controller_Books.t"

Then edit "lib/MyApp/Controller/Books.pm" (as discussed in Chapter 2 of the Tutorial, Catalyst has a separate directory under "lib/MyApp" for each of the three parts of MVC: "Model", "View", and "Controller") and add the following method to the controller:

    =head2 list

    Fetch all book objects and pass to books/list.tt2 in stash to be displayed

    =cut

    sub list : Local {
        # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
        # 'Context' that's used to 'glue together' the various components
        # that make up the application
        my ($self, $c) = @_;

        # Retrieve all of the book records as book model objects and store in the
        # stash where they can be accessed by the TT template
        # $c->stash->{books} = [$c->model('DB::Book')->all];
        # But, for now, use this code until we create the model later
        $c->stash->{books} = '';

        # Set the TT template to use.  You will almost always want to do this
        # in your action methods (action methods respond to user input in
        # your controllers).
        $c->stash->{template} = 'books/list.tt2';
    }

TIP: See Appendix 1 for tips on removing the leading spaces when cutting and pasting example code from POD-based documents.

Programmers experienced with object-oriented Perl should recognize $self as a reference to the object where this method was called. On the other hand, $c will be new to many Perl programmers who have not used Catalyst before (it's sometimes written as $context). The Context object is automatically passed to all Catalyst components. It is used to pass information between components and provide access to Catalyst and plugin functionality.

Catalyst actions are regular Perl methods, but they make use of attributes (the "": Local"" next to the ""sub list"" in the code above) to provide additional information to the Catalyst dispatcher logic (note that the space between the colon and the attribute name is optional; you will see attributes written both ways). Most Catalyst Controllers use one of five action types:

•   :Private -- Use ":Private" for methods that you want to make into an action, but you do not want Catalyst to directly expose to your users. Catalyst will not map ":Private" methods to a URI. Use them for various sorts of "special" methods (the "begin", "auto", etc. discussed below) or for methods you want to be able to "forward" or "detach" to. (If the method is a plain old "helper method" that you don't want to be an action at all, then just define the method without any attribute -- you can call it in your code, but the Catalyst dispatcher will ignore it.)

    There are five types of "special" build-in ":Private" actions: "begin", "end", "default", "index", and "auto".

    •   With "begin", "end", "default", "index" private actions, only the most specific action of each type will be called. For example, if you define a "begin" action in your controller it will override a "begin" action in your application/root controller -- only the action in your controller will be called.

    •   Unlike the other actions where only a single method is called for each request, every auto action along the chain of namespaces will be called. Each "auto" action will be called from the application/root controller down through the most specific class.

•   :Path -- ":Path" actions let you map a method to an explicit URI path. For example, "":Path('list')"" in "lib/MyApp/Controller/Books.pm" would match on the URL "http://localhost:3000/books/list" but "":Path('/list')"" would match on "http://localhost:3000/list". You can use ":Args()" to specify how many arguments an action should accept. See "Action_types" in Catalyst::Manual::Intro for more information and a few examples.

•   :Local -- ":Local" is merely a shorthand for "":Path('_name_of_method_')"". For example, these are equivalent: ""sub create_book :Local {...}"" and ""sub create_book :Path('create_book') {...}"".

•   :Global -- ":Global" is merely a shorthand for "":Path('/_name_of_method_')"". For example, these are equivalent: ""sub create_book :Global {...}"" and ""sub create_book :Path('/create_book') {...}"".

•   :Chained -- Newer Catalyst applications tend to use the Chained dispatch form of action types because of its power and flexibility. It allows a series of controller methods to be automatically dispatched to service a single user request. See Catalyst::Manual::Tutorial::04_BasicCRUD and Catalyst::DispatchType::Chained for more information on chained actions.

You should refer to "Action_types" in Catalyst::Manual::Intro for additional information and for coverage of some lesser-used action types not discussed here ("Regex" and "LocalRegex").

CATALYST VIEWS
As mentioned in Chapter 2 of the tutorial, views are where you render output, typically for display in the user's web browser (but also possibly using into output-generation systems, such as PDF or JSON). The code in "lib/MyApp/View" selects the type of view to use, with the actual rendering template found in the "root" directory. As with virtually every aspect of Catalyst, options abound when it comes to the specific view technology you adopt inside your application. However, most Catalyst applications use the Template Toolkit, known as TT (for more information on TT, see <http://www.template-toolkit.org>). Other somewhat popular view technologies include Mason (<http://www.masonhq.com> and <http://www.masonbook.com>) and HTML::Template (<http://html-template.sourceforge.net>).

  Create a Catalyst View
When using TT for the Catalyst view, there are two main helper scripts:

•   Catalyst::Helper::View::TT

•   Catalyst::Helper::View::TTSite

Both helpers are similar. "TT" creates the "lib/MyApp/View/TT.pm" file and leaves the creation of any hierarchical template organization entirely up to you. (It also creates a "t/view_TT.t" file for testing; test cases will be discussed in Chapter 8.) "TTSite", on the other hand, creates a modular and hierarchical view layout with separate Template Toolkit (TT) files for common header and footer information, configuration values, a CSS stylesheet, and more.

While "TTSite" was useful to bootstrap a project, its use is now deprecated and it should be considered historical. For most Catalyst applications it adds redundant functionality and structure; many in the Catalyst community recommend that it's easier to learn both Catalyst and Template Toolkit if you use the more basic "TT" approach. Consequently, this tutorial will use "plain old TT."

Enter the following command to enable the "TT" style of view rendering for this tutorial:

    $ script/myapp_create.pl view TT TT
     exists "/home/me/MyApp/script/../lib/MyApp/View"
     exists "/home/me/MyApp/script/../t"
     created "/home/me/MyApp/script/../lib/MyApp/View/TT.pm"
     created "/home/me/MyApp/script/../t/view_TT.t"

This simply creates a view called "TT" (the second 'TT' argument) in a file called "TT.pm" (the first 'TT' argument). It is now up to you to decide how you want to structure your view layout. For the tutorial, we will start with a very simple TT template to initially demonstrate the concepts, but quickly migrate to a more typical "wrapper page" type of configuration (where the "wrapper" controls the overall "look and feel" of your site from a single file or set of files).

Edit "lib/MyApp/View/TT.pm" and you should see that the default contents contains something similar to the following:

    __PACKAGE__->config(TEMPLATE_EXTENSION => '.tt');

And update it to match:

    __PACKAGE__->config(
        # Change default TT extension
        TEMPLATE_EXTENSION => '.tt2',
        # Set the location for TT files
        INCLUDE_PATH => [
                MyApp->path_to( 'root', 'src' ),
            ],
    );

NOTE: Make sure to add a comma after '.tt2' outside the single quote.

This changes the default extension for Template Toolkit from '.tt' to '.tt2' and changes the base directory for your template files from "root" to "root/src". These changes from the default are done mostly to facilitate the application we're developing in this tutorial; as with most things Perl, there's more than one way to do it...

Note: We will use "root/src" as the base directory for our template files, which a full naming convention of "root/src/_controller_name_/_action_name_.tt2". Another popular option is to use "root/" as the base (with a full filename pattern of "root/_controller_name_/_action_name_.tt2").

  Create a TT Template Page
First create a directory for book-related TT templates:

    $ mkdir -p root/src/books

Then create "root/src/books/list.tt2" in your editor and enter:

    [% # This is a TT comment.  The '-' at the end "chomps" the newline.  You won't -%]
    [% # see this "chomping" in your browser because HTML ignores blank lines, but  -%]
    [% # it WILL eliminate a blank line if you view the HTML source.  It's purely   -%]
    [%- # optional, but both the beginning and the ending TT tags support chomping. -%]

    [% # Provide a title -%]
    [% META title = 'Book List' -%]

    <table>
    <tr><th>Title</th><th>Rating</th><th>Author(s)</th></tr>
    [% # Display each book in a table row %]
    [% FOREACH book IN books -%]
      <tr>
        <td>[% book.title %]</td>
        <td>[% book.rating %]</td>
        <td></td>
      </tr>
    [% END -%]
    </table>

As indicated by the inline comments above, the "META title" line uses TT's META feature to provide a title to the "wrapper" that we will create later. Meanwhile, the "FOREACH" loop iterates through each "book" model object and prints the "title" and "rating" fields.

The "[%" and "%]" tags are used to delimit Template Toolkit code. TT supports a wide variety of directives for "calling" other files, looping, conditional logic, etc. In general, TT simplifies the usual range of Perl operators down to the single dot (".") operator. This applies to operations as diverse as method calls, hash lookups, and list index values (see <http://search.cpan.org/perldoc?Template::Manual::Variables> for details and examples). In addition to the usual "Template" module Pod documentation, you can access the TT manual at <http://search.cpan.org/perldoc?Template::Manual>.

TIP: While you can build all sorts of complex logic into your TT templates, you should in general keep the "code" part of your templates as simple as possible. If you need more complex logic, create helper methods in your model that abstract out a set of code into a single call from your TT template. (Note that the same is true of your controller logic as well -- complex sections of code in your controllers should often be pulled out and placed into your model objects.)

  Test Run The Application
To test your work so far, first start the development server:

    $ script/myapp_server.pl

Then point your browser to <http://localhost:3000> and you should still get the Catalyst welcome page. Next, change the URL in your browser to <http://localhost:3000/books/list>. If you have everything working so far, you should see a web page that displays nothing other than our column headers for "Title", "Rating", and "Author(s)" -- we will not see any books until we get the database and model working below.

If you run into problems getting your application to run correctly, it might be helpful to refer to some of the debugging techniques covered in the Debugging chapter of the tutorial.

CREATE A SQLITE DATABASE
In this step, we make a text file with the required SQL commands to create a database table and load some sample data. We will use SQLite, a popular database that is lightweight and easy to use. Open "myapp01.sql" in your editor and enter:

    --
    -- Create a very simple database to hold book and author information
    --
    CREATE TABLE book (
            id          INTEGER PRIMARY KEY,
            title       TEXT ,
            rating      INTEGER
    );
    -- 'book_author' is a many-to-many join table between books & authors
    CREATE TABLE book_author (
            book_id     INTEGER,
            author_id   INTEGER,
            PRIMARY KEY (book_id, author_id)
    );
    CREATE TABLE author (
            id          INTEGER PRIMARY KEY,
            first_name  TEXT,
            last_name   TEXT
    );
    ---
    --- Load some sample data
    ---
    INSERT INTO book VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
    INSERT INTO book VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
    INSERT INTO book VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
    INSERT INTO book VALUES (4, 'Perl Cookbook', 5);
    INSERT INTO book VALUES (5, 'Designing with Web Standards', 5);
    INSERT INTO author VALUES (1, 'Greg', 'Bastien');
    INSERT INTO author VALUES (2, 'Sara', 'Nasseh');
    INSERT INTO author VALUES (3, 'Christian', 'Degu');
    INSERT INTO author VALUES (4, 'Richard', 'Stevens');
    INSERT INTO author VALUES (5, 'Douglas', 'Comer');
    INSERT INTO author VALUES (6, 'Tom', 'Christiansen');
    INSERT INTO author VALUES (7, 'Nathan', 'Torkington');
    INSERT INTO author VALUES (8, 'Jeffrey', 'Zeldman');
    INSERT INTO book_author VALUES (1, 1);
    INSERT INTO book_author VALUES (1, 2);
    INSERT INTO book_author VALUES (1, 3);
    INSERT INTO book_author VALUES (2, 4);
    INSERT INTO book_author VALUES (3, 5);
    INSERT INTO book_author VALUES (4, 6);
    INSERT INTO book_author VALUES (4, 7);
    INSERT INTO book_author VALUES (5, 8);

Then use the following command to build a "myapp.db" SQLite database:

    $ sqlite3 myapp.db < myapp01.sql

If you need to create the database more than once, you probably want to issue the "rm myapp.db" command to delete the database before you use the "sqlite3 myapp.db < myapp01.sql" command.

Once the "myapp.db" database file has been created and initialized, you can use the SQLite command line environment to do a quick dump of the database contents:

    $ sqlite3 myapp.db
    SQLite version 3.5.9
    Enter ".help" for instructions
    sqlite> select * from book;
    1|CCSP SNRS Exam Certification Guide|5
    2|TCP/IP Illustrated, Volume 1|5
    3|Internetworking with TCP/IP Vol.1|4
    4|Perl Cookbook|5
    5|Designing with Web Standards|5
    sqlite> .q
    $

Or:

    $ sqlite3 myapp.db "select * from book"
    1|CCSP SNRS Exam Certification Guide|5
    2|TCP/IP Illustrated, Volume 1|5
    3|Internetworking with TCP/IP Vol.1|4
    4|Perl Cookbook|5
    5|Designing with Web Standards|5

As with most other SQL tools, if you are using the full "interactive" environment you need to terminate your SQL commands with a ";" (it's not required if you do a single SQL statement on the command line). Use ".q" to exit from SQLite from the SQLite interactive mode and return to your OS command prompt.

Please note that here we have chosen to use 'singular' table names. This is because the default inflection code for DBIx::Class:Schema::Loader does NOT handle plurals. There has been much philosophical discussion on whether table names should be plural or singular. There is no one correct answer, as long as one makes a choice and remains consistent with it. If you prefer plural table names (e.g. they are easier and more natural to read) then you will need to pass it an inflect_map option. See DBIx::Class:Schema::Loader for more information.

For using other databases, such as PostgreSQL or MySQL, see Appendix 2.

DATABASE ACCESS WITH DBIx::Class
Catalyst can be used with virtually any form of datastore available via Perl. For example, Catalyst::Model::DBI can be used to access databases through the traditional Perl "DBI" interface or you can use a model to access files of any type on the filesystem. However, most Catalyst applications use some form of object-relational mapping (ORM) technology to create objects associated with tables in a relational database. Matt Trout's DBIx::Class (abbreviated as "DBIC") has rapidly emerged as the Perl-based ORM technology of choice. Most new Catalyst applications rely on DBIx::Class, as will this tutorial.

Although DBIx::Class has included support for a "create=dynamic" mode to automatically read the database structure every time the application starts, it's use is no longer recommended. While it can make for "flashy" demos, the use of the "create=static" mode we use below can be implemented just as quickly and provides many advantages (such as the ability to add your own methods to the overall DBIC framework, a technique that we see in Chapter 4).

  Make Sure You Have a Recent Version of the DBIx::Class Model
First, let's be sure we have a recent version of the DBIC helper, Catalyst::Model::DBIC::Schema, by running this command:

    $ perl -MCatalyst::Model::DBIC::Schema -e \
        'print "$Catalyst::Model::DBIC::Schema::VERSION\n"'
    0.23

Please note the '\' above. Depending on your environment, you might be able to cut and paste the text as shown or need to remove the '\' character to that the command is all on a single line.

If you don't have version 0.23 or higher, please run this command to install it directly from CPAN:

    $ sudo cpan Catalyst::Model::DBIC::Schema

And re-run the version print command to verify that you are now at 0.23 or higher.

  Create Static DBIx::Class Schema Files
Before you continue, make sure your "myapp.db" database file is in the application's topmost directory. Now use the model helper with the "create=static" option to read the database with DBIx::Class::Schema::Loader and automatically build the required files for us:

    $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
        create=static dbi:SQLite:myapp.db
     exists "/home/me/MyApp/script/../lib/MyApp/Model"
     exists "/home/me/MyApp/script/../t"
    Dumping manual schema for MyApp::Schema to directory /home/me/MyApp/script/../lib ...
    Schema dump completed.
    created "/home/me/MyApp/script/../lib/MyApp/Model/DB.pm"
    created "/home/me/MyApp/script/../t/model_DB.t"

Please note the '\' above. Depending on your environment, you might be able to cut and paste the text as shown or need to remove the '\' character to that the command is all on a single line.

The "script/myapp_create.pl" command breaks down like this:

•   "DB" is the name of the model class to be created by the helper in "lib/MyApp/Model".

•   "DBIC::Schema" is the type of the model to create.

•   "MyApp::Schema" is the name of the DBIC schema file written to "lib/MyApp/Schema.pm".

•   "create=static" causes DBIx::Class::Schema::Loader to load the schema as it runs and then write that information out into files.

•   "components=TimeStamp" causes the help to include the DBIx::Class::TimeStamp DBIC component.

•   And finally, "dbi:SQLite:myapp.db" is the standard DBI connect string for use with SQLite.

If you look in the "lib/MyApp/Schema.pm" file, you will find that it only contains a call to the "load_namespaces" method. You will also find that "lib/MyApp" contains a "Schema" subdirectory, which then has a subdirectory called "Result". This "Result" subdirectory then has files named according to each of the tables in our simple database ("Author.pm", "BookAuthor.pm", and "Book.pm"). These three files are called "Result Classes" in DBIx::Class nomenclature. Although the Result Class files are named after tables in our database, the classes correspond to the row-level data that is returned by DBIC (more on this later, especially in "EXPLORING THE POWER OF DBIC" in Catalyst::Manual::Tutorial::04_BasicCRUD).

The idea with the Result Source files created under "lib/MyApp/Schema/Result" by the "create=static" option is to only edit the files below the "# DO NOT MODIFY THIS OR ANYTHING ABOVE!" warning. If you place all of your changes below that point in the file, you can regenerate the automatically created information at the top of each file should your database structure get updated.

Also note the "flow" of the model information across the various files and directories. Catalyst will initially load the model from "lib/MyApp/Model/DB.pm". This file contains a reference to "lib/MyApp/Schema.pm", so that file is loaded next. Finally, the call to "load_namespaces" in "Schema.pm" will load each of the "Result Class" files from the "lib/MyApp/Schema/Result" subdirectory. The final outcome is that Catalyst will dynamically create three table-specific Catalyst models every time the application starts (you can see these three model files listed in the debug output generated when you launch the application).

NOTE: Older versions of Catalyst::Model::DBIC::Schema use the deprecated DBIx::Class "load_classes" technique instead of the newer "load_namspaces". For new applications, please try to use "load_namespaces" since it more easily supports a very useful DBIC technique called "ResultSet Classes." If you need to convert an existing application from "load_classes" to "load_namespaces," you can use this process to automate the migration (but first make sure you have v0.23 "Catalyst::Model::DBIC::Schema" as discussed above):

    $ # First delete the existing schema file to disable "compatibility" mode
    $ rm lib/MyApp/Schema.pm
    $
    $ # Then re-run the helper to build the files for "load_namespaces"
    $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
        create=static dbi:SQLite:myapp.db
    $
    $ # Now convert the existing files over
    $ cd lib/MyApp/Schema
    $ perl -MIO::All -e 'for (@ARGV) { my $s < io($_); $s =~ s/.*\n\# You can replace.*?\n//s;
          $s =~ s/'MyApp::Schema::/'MyApp::Schema::Result::/g; my $d < io("Result/$_");
          $d =~ s/1;\n?//; "$d$s" > io("Result/$_"); }' *.pm
    $ cd ../../..
    $
    $ # And finally delete the old files
    $ rm lib/MyApp/Schema/*.pm

The ""perl -MIO::ALL ..."" script will copy all the customized relationship (and other) information below ""# DO NOT MODIFY"" line from the old files in "lib/MyApp/Schema" to the new files in "lib/MyApp/Schema/Result" (we will be starting to add some "customized relationship information in the section below).

ENABLE THE MODEL IN THE CONTROLLER
Open "lib/MyApp/Controller/Books.pm" and un-comment the model code we left disabled earlier so that your version matches the following (un- comment the line containing "[$c->model('DB::Book')->all]" and delete the next 2 lines):

    =head2 list

    Fetch all book objects and pass to books/list.tt2 in stash to be displayed

    =cut

    sub list : Local {
        # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
        # 'Context' that's used to 'glue together' the various components
        # that make up the application
        my ($self, $c) = @_;

        # Retrieve all of the book records as book model objects and store in the
        # stash where they can be accessed by the TT template
        $c->stash->{books} = [$c->model('DB::Book')->all];

        # Set the TT template to use.  You will almost always want to do this
        # in your action methods (action methods respond to user input in
        # your controllers).
        $c->stash->{template} = 'books/list.tt2';
    }

TIP: You may see the "$c->model('DB::Book')" un-commented above written as "$c->model('DB')->resultset('Book')". The two are equivalent. Either way, "$c->model" returns a DBIx::Class::ResultSet which handles queries against the database and iterating over the set of results that is returned.

We are using the "->all" to fetch all of the books. DBIC supports a wide variety of more advanced operations to easily do things like filtering and sorting the results. For example, the following could be used to sort the results by descending title:

    $c->model('DB::Book')->search({}, {order_by => 'title DESC'});

Some other examples are provided in "Complex WHERE clauses" in DBIx::Class::Manual::Cookbook, with additional information found at "search" in DBIx::Class::ResultSet, "Searching" in DBIx::Class::Manual::FAQ, DBIx::Class::Manual::Intro and Catalyst::Model::DBIC::Schema.

  Test Run The Application
First, let's enable an environment variable that causes DBIx::Class to dump the SQL statements used to access the database. This is a helpful trick when you are trying to debug your database-oriented code:

    $ export DBIC_TRACE=1

This assumes you are using bash as your shell -- adjust accordingly if you are using a different shell (for example, under tcsh, use "setenv DBIC_TRACE 1").

NOTE: You can also set this in your code using "$class->storage->debug(1);". See DBIx::Class::Manual::Troubleshooting for details (including options to log to a file instead of displaying to the Catalyst development server log).

Then launch the Catalyst development server. The log output should display something like:

    $ script/myapp_server.pl
    [debug] Debug messages enabled
    [debug] Statistics enabled
    [debug] Loaded plugins:
    .----------------------------------------------------------------------------.
    | Catalyst::Plugin::ConfigLoader  0.22                                       |
    | Catalyst::Plugin::StackTrace  0.09                                         |
    | Catalyst::Plugin::Static::Simple  0.21                                     |
    '----------------------------------------------------------------------------'

    [debug] Loaded dispatcher "Catalyst::Dispatcher"
    [debug] Loaded engine "Catalyst::Engine::HTTP"
    [debug] Found home "/home/me/MyApp"
    [debug] Loaded Config "/home/me/MyApp/myapp.conf"
    [debug] Loaded components:
    .-----------------------------------------------------------------+----------.
    | Class                                                           | Type     |
    +-----------------------------------------------------------------+----------+
    | MyApp::Controller::Books                                        | instance |
    | MyApp::Controller::Root                                         | instance |
    | MyApp::Model::DB                                                | instance |
    | MyApp::Model::DB::Author                                        | class    |
    | MyApp::Model::DB::Book                                          | class    |
    | MyApp::Model::DB::BookAuthor                                    | class    |
    | MyApp::View::TT                                                 | instance |
    '-----------------------------------------------------------------+----------'

    [debug] Loaded Private actions:
    .----------------------+--------------------------------------+--------------.
    | Private              | Class                                | Method       |
    +----------------------+--------------------------------------+--------------+
    | /default             | MyApp::Controller::Root              | default      |
    | /end                 | MyApp::Controller::Root              | end          |
    | /index               | MyApp::Controller::Root              | index        |
    | /books/index         | MyApp::Controller::Books             | index        |
    | /books/list          | MyApp::Controller::Books             | list         |
    '----------------------+--------------------------------------+--------------'

    [debug] Loaded Path actions:
    .-------------------------------------+--------------------------------------.
    | Path                                | Private                              |
    +-------------------------------------+--------------------------------------+
    | /                                   | /default                             |
    | /                                   | /index                               |
    | /books                              | /books/index                         |
    | /books/list                         | /books/list                          |
    '-------------------------------------+--------------------------------------'

    [info] MyApp powered by Catalyst 5.80003
    You can connect to your server at http://debian:3000

NOTE: Be sure you run the "script/myapp_server.pl" command from the 'base' directory of your application, not inside the "script" directory itself or it will not be able to locate the "myapp.db" database file. You can use a fully qualified or a relative path to locate the database file, but we did not specify that when we ran the model helper earlier.

Some things you should note in the output above:

•   Catalyst::Model::DBIC::Schema dynamically created three model classes, one to represent each of the three tables in our database ("MyApp::Model::DB::Author", "MyApp::Model::DB::BookAuthor", and "MyApp::Model::DB::Book").

•   The "list" action in our Books controller showed up with a path of "/books/list".

Point your browser to <http://localhost:3000> and you should still get the Catalyst welcome page.

Next, to view the book list, change the URL in your browser to <http://localhost:3000/books/list>. You should get a list of the five books loaded by the "myapp01.sql" script above without any formatting. The rating for each book should appear on each row, but the "Author(s)" column will still be blank (we will fill that in later).

Also notice in the output of the "script/myapp_server.pl" that DBIx::Class used the following SQL to retrieve the data:

    SELECT me.id, me.title, me.rating FROM book me

because we enabled DBIC_TRACE.

You now have the beginnings of a simple but workable web application. Continue on to future sections and we will develop the application more fully.

CREATE A WRAPPER FOR THE VIEW
When using TT, you can (and should) create a wrapper that will literally wrap content around each of your templates. This is certainly useful as you have one main source for changing things that will appear across your entire site/application instead of having to edit many individual files.

  Configure TT.pm For The Wrapper
In order to create a wrapper, you must first edit your TT view and tell it where to find your wrapper file. Your TT view is located in "lib/MyApp/View/TT.pm".

Edit "lib/MyApp/View/TT.pm" and change it to match the following:

    __PACKAGE__->config(
        # Change default TT extension
        TEMPLATE_EXTENSION => '.tt2',
        # Set the location for TT files
        INCLUDE_PATH => [
                MyApp->path_to( 'root', 'src' ),
            ],
        # Set to 1 for detailed timer stats in your HTML as comments
        TIMER              => 0,
        # This is your wrapper template located in the 'root/src'
        WRAPPER => 'wrapper.tt2',
    );

  Create the Wrapper Template File and Stylesheet
Next you need to set up your wrapper template. Basically, you'll want to take the overall layout of your site and put it into this file. For the tutorial, open "root/src/wrapper.tt2" and input the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>[% template.title or "My Catalyst App!" %]</title>
    <link rel="stylesheet" href="[% c.uri_for('/static/css/main.css') %]" />
    </head>

    <body>
    <div id="outer">
    <div id="header">
        [%# Your logo could go here -%]
        <img src="[% c.uri_for('/static/images/btn_88x31_powered.png') %]" />
        [%# Insert the page title -%]
        <h1>[% template.title or site.title %]</h1>
    </div>

    <div id="bodyblock">
    <div id="menu">
        Navigation:
        <ul>
            <li><a href="[% c.uri_for('/books/list') %]">Home</a></li>
            <li><a href="[% c.uri_for('/') %]" title="Catalyst Welcome Page">Welcome</a></li>
        </ul>
    </div><!-- end menu -->

    <div id="content">
        [%# Status and error messages %]
        <span class="message">[% status_msg %]</span>
        <span class="error">[% error_msg %]</span>
        [%# This is where TT will stick all of your template's contents. -%]
        [% content %]
    </div><!-- end content -->
    </div><!-- end bodyblock -->

    <div id="footer">Copyright (c) your name goes here</div>
    </div><!-- end outer -->

    </body>
    </html>

Notice the status and error message sections in the code above:

    <span class="status">[% status_msg %]</span>
    <span class="error">[% error_msg %]</span>

If we set either message in the Catalyst stash (e.g., "$c->stash->{status_msg} = 'Request was successful!'") it will be displayed whenever any view used by that request is rendered. The "message" and "error" CSS styles can be customized to suit your needs in the "root/static/css/main.css" file we create below.

Notes:

•   The Catalyst stash only lasts for a single HTTP request. If you need to retain information across requests you can use Catalyst::Plugin::Session (we will use Catalyst sessions in the Authentication chapter of the tutorial).

•   Although it is beyond the scope of this tutorial, you may wish to use a JavaScript or AJAX tool such as jQuery (<http://www.jquery.com>) or Dojo (<http://www.dojotoolkit.org>).

Create A Basic Stylesheet

First create a central location for stylesheets under the static directory:

    $ mkdir root/static/css

Then open the file "root/static/css/main.css" (the file referenced in the stylesheet href link of our wrapper above) and add the following content:

    #header {
        text-align: center;
    }
    #header h1 {
        margin: 0;
    }
    #header img {
        float: right;
    }
    #footer {
        text-align: center;
        font-style: italic;
        padding-top: 20px;
    }
    #menu {
        font-weight: bold;
        background-color: #ddd;
    }
    #menu ul {
        list-style: none;
        float: left;
        margin: 0;
        padding: 0 0 50% 5px;
        font-weight: normal;
        background-color: #ddd;
        width: 100px;
    }
    #content {
        margin-left: 120px;
    }
    .message {
        color: #390;
    }
    .error {
        color: #f00;
    }

You may wish to check out a "CSS Framework" like Emastic (<http://code.google.com/p/emastic/>) as a way to quickly provide lots of high-quality CSS functionality.

  Test Run The Application
Restart the development server and hit "Reload" in your web browser and you should now see a formatted version of our basic book list. Although our wrapper and stylesheet are obviously very simple, you should see how it allows us to control the overall look of an entire website from two central files. To add new pages to the site, just provide a template that fills in the "content" section of our wrapper template -- the wrapper will provide the overall feel of the page.

  Updating the Generated DBIx::Class Result Class Files
Let's manually add some relationship information to the auto-generated Result Class files. (Note: if you are using a database other than SQLite, such as PostgreSQL, then the relationship could have been automatically placed in the Result Class files. If so, you can skip this step.) First edit "lib/MyApp/Schema/Result/Book.pm" and add the following text below the "# You can replace this text..." comment:

    #
    # Set relationships:
    #

    # has_many():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *foreign* table (aka, foreign key in peer table)
    __PACKAGE__->has_many(book_authors => 'MyApp::Schema::Result::BookAuthor', 'book_id');

    # many_to_many():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of has_many() relationship this many_to_many() is shortcut for
    #     3) Name of belongs_to() relationship in model class of has_many() above
    #   You must already have the has_many() defined to use a many_to_many().
    __PACKAGE__->many_to_many(authors => 'book_authors', 'author');

Note: Be careful to put this code above the "1;" at the end of the file. As with any Perl package, we need to end the last line with a statement that evaluates to "true". This is customarily done with "1;" on a line by itself.

This code defines both a "has_many" and a "many_to_many" relationship. The "many_to_many" relationship is optional, but it makes it easier to map a book to its collection of authors. Without it, we would have to "walk" though the "book_author" table as in "$book->book_author->first->author->last_name" (we will see examples on how to use DBIx::Class objects in your code soon, but note that because "$book->book_author" can return multiple authors, we have to use "first" to display a single author). "many_to_many" allows us to use the shorter "$book->author->first->last_name". Note that you cannot define a "many_to_many" relationship without also having the "has_many" relationship in place.

Then edit "lib/MyApp/Schema/Result/Author.pm" and add relationship information as follows (again, be careful to put in above the "1;" but below the "# DO NOT MODIFY THIS OR ANYTHING ABOVE!" comment):

    #
    # Set relationships:
    #

    # has_many():
    #   args:
    #     1) Name of relationship, DBIC will create an accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *foreign* table (aka, foreign key in peer table)
    __PACKAGE__->has_many(book_authors => 'MyApp::Schema::Result::BookAuthor', 'author_id');

    # many_to_many():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of has_many() relationship this many_to_many() is shortcut for
    #     3) Name of belongs_to() relationship in model class of has_many() above
    #   You must already have the has_many() defined to use a many_to_many().
    __PACKAGE__->many_to_many(books => 'book_authors', 'book');

Finally, do the same for the "join table," "lib/MyApp/Schema/Result/BookAuthor.pm":

    #
    # Set relationships:
    #

    # belongs_to():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *this* table
    __PACKAGE__->belongs_to(book => 'MyApp::Schema::Result::Book', 'book_id');

    # belongs_to():
    #   args:
    #     1) Name of relationship, DBIC will create accessor with this name
    #     2) Name of the model class referenced by this relationship
    #     3) Column name in *this* table
    __PACKAGE__->belongs_to(author => 'MyApp::Schema::Result::Author', 'author_id');

  Run The Application
Run the Catalyst development server script with the "DBIC_TRACE" option (it might still be enabled from earlier in the tutorial, but here is an alternate way to specify the option just in case):

    $ DBIC_TRACE=1 script/myapp_server.pl

Make sure that the application loads correctly and that you see the three dynamically created model class (one for each of the Result Classes we created).

Then hit the URL <http://localhost:3000/books/list> with your browser and be sure that the book list is displayed via the relationships established above. You can leave the development server running for the next step if you wish.

Note: You will not see the authors yet because the view does not yet use the new relations. Read on to the next section where we update the template to do that.

UPDATING THE VIEW
Let's add a new column to our book list page that takes advantage of the relationship information we manually added to our schema files in the previous section. Edit "root/src/books/list.tt2" and replace the "empty" table cell "<td></td>" with the following:

    ...
    <td>
      [% # NOTE: See Chapter 4 for a better way to do this!                      -%]
      [% # First initialize a TT variable to hold a list.  Then use a TT FOREACH -%]
      [% # loop in 'side effect notation' to load just the last names of the     -%]
      [% # authors into the list. Note that the 'push' TT vmethod doesn't return -%]
      [% # a value, so nothing will be printed here.  But, if you have something -%]
      [% # in TT that does return a value and you don't want it printed, you     -%]
      [% # 1) assign it to a bogus value, or                                     -%]
      [% # 2) use the CALL keyword to call it and discard the return value.      -%]
      [% tt_authors = [ ];
         tt_authors.push(author.last_name) FOREACH author = book.authors %]
      [% # Now use a TT 'virtual method' to display the author count in parens   -%]
      [% # Note the use of the TT filter "| html" to escape dangerous characters -%]
      ([% tt_authors.size | html %])
      [% # Use another TT vmethod to join & print the names & comma separators   -%]
      [% tt_authors.join(', ') | html %]
    </td>
    ...

IMPORTANT NOTE: You should keep as much "logic code" as possible out of your views. Instead, this kind of logic belongs in your model (the same goes for controllers -- keep them as "thin" as possible and push all of the "complicated code" out to your model objects). Avoid code like you see in the previous example -- we are only using it here to show some extra features in TT until we get to the more advanced model features we will see in Chapter 4 (see "EXPLORING THE POWER OF DBIC" in Catalyst::Manual::Tutorial::04_BasicCRUD).

Then hit "Reload" in your browser (note that you don't need to reload the development server or use the "-r" option when updating TT templates) and you should now see the number of authors each book has along with a comma-separated list of the authors' last names. (If you didn't leave the development server running from the previous step, you will obviously need to start it before you can refresh your browser window.)

If you are still running the development server with "DBIC_TRACE" enabled, you should also now see five more "SELECT" statements in the debug output (one for each book as the authors are being retrieved by DBIx::Class):

    SELECT me.id, me.title, me.rating FROM book me:
    SELECT author.id, author.first_name, author.last_name FROM book_author me
    JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '1'
    SELECT author.id, author.first_name, author.last_name FROM book_author me
    JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '2'
    SELECT author.id, author.first_name, author.last_name FROM book_author me
    JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '3'
    SELECT author.id, author.first_name, author.last_name FROM book_author me
    JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '4'
    SELECT author.id, author.first_name, author.last_name FROM book_author me
    JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '5'

Also note in "root/src/books/list.tt2" that we are using "| html", a type of TT filter, to escape characters such as < and > to &lt; and &gt; and avoid various types of dangerous hacks against your application. In a real application, you would probably want to put "| html" at the end of every field where a user has control over the information that can appear in that field (and can therefore inject markup or code if you don't "neutralize" those fields). In addition to "| html", Template Toolkit has a variety of other useful filters that can found in the documentation for Template::Filters.

RUNNING THE APPLICATION FROM THE COMMAND LINE
In some situations, it can be useful to run your application and display a page without using a browser. Catalyst lets you do this using the "scripts/myapp_test.pl" script. Just supply the URL you wish to display and it will run that request through the normal controller dispatch logic and use the appropriate view to render the output (obviously, complex pages may dump a lot of text to your terminal window). For example, if you type:

    $ script/myapp_test.pl "/books/list"

You should get the same text as if you visited <http://localhost:3000/books/list> with the normal development server and asked your browser to view the page source.

OPTIONAL INFORMATION
NOTE: The rest of this chapter of the tutorial is optional. You can skip to Chapter 4, Basic CRUD, if you wish.

  Using 'RenderView' for the Default View
Once your controller logic has processed the request from a user, it forwards processing to your view in order to generate the appropriate response output. Catalyst uses Catalyst::Action::RenderView by default to automatically perform this operation. If you look in "lib/MyApp/Controller/Root.pm", you should see the empty definition for the "sub end" method:

    sub end : ActionClass('RenderView') {}

The following bullet points provide a quick overview of the "RenderView" process:

•   "Root.pm" is designed to hold application-wide logic.

•   At the end of a given user request, Catalyst will call the most specific "end" method that's appropriate. For example, if the controller for a request has an "end" method defined, it will be called. However, if the controller does not define a controller-specific "end" method, the "global" "end" method in "Root.pm" will be called.

•   Because the definition includes an "ActionClass" attribute, the Catalyst::Action::RenderView logic will be executed after any code inside the definition of "sub end" is run. See Catalyst::Manual::Actions for more information on "ActionClass".

•   Because "sub end" is empty, this effectively just runs the default logic in "RenderView". However, you can easily extend the "RenderView" logic by adding your own code inside the empty method body ("{}") created by the Catalyst Helpers when we first ran the "catalyst.pl" to initialize our application. See Catalyst::Action::RenderView for more detailed information on how to extend "RenderView" in "sub end".

  RenderView's "dump_info" Feature
One of the nice features of "RenderView" is that it automatically allows you to add "dump_info=1" to the end of any URL for your application and it will force the display of the "exception dump" screen to the client browser. You can try this out by starting the development server as before and then point your browser to this URL:

    http://localhost:3000/books/list?dump_info=1

You should get a page with the following message at the top:

    Caught exception in MyApp::Controller::Root->end "Forced debug -
    Scrubbed output at /usr/share/perl5/Catalyst/Action/RenderView.pm line 46."

Along with a summary of your application's state at the end of the processing for that request. The "Stash" section should show a summarized version of the DBIC book model objects. If desired, you can adjust the summarization logic (called "scrubbing" logic) -- see Catalyst::Action::RenderView for details.

Note that you shouldn't need to worry about "normal clients" using this technique to "reverse engineer" your application -- "RenderView" only supports the "dump_info=1" feature when your application is running in "-Debug" mode (something you won't do once you have your application deployed in production).

  Using The Default Template Name
By default, "Catalyst::View::TT" will look for a template that uses the same name as your controller action, allowing you to save the step of manually specifying the template name in each action. For example, this would allow us to remove the "$c->stash->{template} = 'books/list.tt2';" line of our "list" action in the Books controller. Open "lib/MyApp/Controller/Books.pm" in your editor and comment out this line to match the following (only the "$c->stash->{template}" line has changed):

    =head2 list

    Fetch all book objects and pass to books/list.tt2 in stash to be displayed

    =cut

    sub list : Local {
        # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
        # 'Context' that's used to 'glue together' the various components
        # that make up the application
        my ($self, $c) = @_;

        # Retrieve all of the book records as book model objects and store in the
        # stash where they can be accessed by the TT template
        $c->stash->{books} = [$c->model('DB::Book')->all];

        # Set the TT template to use.  You will almost always want to do this
        # in your action methods (actions methods respond to user input in
        # your controllers).
        #$c->stash->{template} = 'books/list.tt2';
    }

You should now be able to restart the development server as per the previous section and access the <http://localhost:3000/books/list> as before.

NOTE: Please note that if you use the default template technique, you will not be able to use either the "$c->forward" or the "$c->detach" mechanisms (these are discussed in Chapter 2 and Chapter 9 of the Tutorial).

IMPORTANT: Make sure that you do NOT skip the following section before continuing to the next chapter 4 Basic CRUD.

  Return To A Manually Specified Template
In order to be able to use "$c->forward" and "$c->detach" later in the tutorial, you should remove the comment from the statement in "sub list" in "lib/MyApp/Controller/Books.pm":

    $c->stash->{template} = 'books/list.tt2';

Then delete the "TEMPLATE_EXTENSION" line in "lib/MyApp/View/TT.pm".

You should then be able to restart the development server and access <http://localhost:3000/books/list> in the same manner as with earlier sections.

AUTHOR
Kennedy Clark, "hkclark@gmail.com"

Please report any errors, issues or suggestions to the author. The most recent version of the Catalyst Tutorial can be found at <http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.

Copyright 2006-2008, Kennedy Clark, under Creative Commons License (<http://creativecommons.org/licenses/by-sa/3.0/us/>).