The Sysadmin Notebook  

Sitemap

Catalyst First Steps

Building Web Applications Using Catalyst

Contents

Preparing Development Environment

Top Bottom

For a development environment use local::lib to leave the core Perl untouch by your experiments

Installing local::lib

user:~> su root 
cpan 
cpan> install local::lib
cpan> quit
root:~ # exit

add "eval $(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)" to bashrc

Configure cpan
user:~> cpan
cpan> o conf init urllist
cpan> o conf commit
cpan> quit

Install Catalyst

Top Bottom

Install Catalyst::Devel for a development machine or Catalyst::Runtime for a production server

# PERL_MM_USE_DEFAULT=1 instructs cpan to accept defaults to prompts where possible
user:~> PERL_MM_USE_DEFAULT=1 cpan
cpan> install Catalyst::Devel
# takes about 20 minutes on a Turion 64 X2 TL-56 with 3GB RAM

First App

Top Bottom

user:~> catalyst.pl MyApp::Lite
# creates following directory structure

Makefile

Top Bottom

Makefile.PL is a perl script that when run (perl Makefile.PL), generates a 'Makefile'. The 'Makefile' is then used by 'make' to build the system and install prerequisites for the application.The default Makefile.PL when you run 'catalyst.pl MyApp::Lite', looks like this:

#!/usr/bin/env perl
# IMPORTANT: if you delete this file your app will not work as
# expected.  You have been warned.
use inc::Module::Install;

name 'MyApp-Lite';
all_from 'lib/MyApp/Lite.pm';

requires 'Catalyst::Runtime' => '5.80013';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Plugin::Static::Simple';
requires 'Catalyst::Action::RenderView';
requires 'parent';
requires 'Config::General'; # This should reflect the config file format you've chosen
                 # See Catalyst::Plugin::ConfigLoader for supported formats
catalyst;

install_script glob('script/*.pl');
auto_install;
WriteAll;
use inc::Module::Install
tells Perl which build system to use: the 'inc' allows the inclusion of local modules contained in the local directory 'inc'.
name 'MyApp-Lite'; all_from 'lib/MyApp/Lite.pm';
generates meta data for the application
requires ....
list of CPAN prerequisites. You should add your own prerequisites here according to the needs of your application: e.g. Catalyst::View::TT.
catalyst;
tells the build system to use Module::Install::Catalyst, which provides some utility functions
install_script glob('script/*.pl');
tells Module::Install that the scripts in the script directory will need to be installed into the user's PATH
auto_install
tells Module::Install to install all the CPAN prerequisites. All prerequisites should therefore be listed before this line
WriteAll;
instruction to build system to write all files required for the build process

Root Controller

Top Bottom

The Root Controller handles requests to the root namespace for your application

package MyApp::Lite::Controller::Root;

use strict;
use warnings;
use parent 'Catalyst::Controller';

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

=head1 NAME

MyApp::Lite::Controller::Root - Root Controller for MyApp::Lite

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    # Hello World
    $c->response->body( $c->welcome_message );
}

sub default :Path {
    my ( $self, $c ) = @_;
    $c->response->body( 'Page not found' );
    $c->response->status(404);
}

=head2 end

Attempt to render a view, if needed.

=cut

sub end : ActionClass('RenderView') {}

=head1 AUTHOR

David Ramlakhan

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
sub index :Path :Args(0)
The name of this subroutine is arbitrary, but 'index' is a sensible choice. The ':Path' attribute means that this subroutine is called in response to a specified path. The ':Args(0)' attribute means that the path this subroutine responds to, is for the controllers path with no subsequent arguments. Because this is the Root controller for the application, this subroutine will run in response to requests for the application root
sub default :Path
This subroutine is called if no other subroutine matches a request path
sub end : ActionClass('RenderView')
This subroutine runs after all other code in the controller is run

Next Steps

Top Bottom

Having run the catalyst.pl to create the skeleton for your application, you now have a working application that you can run using

MyApp-Lite> perl script/myapp_lite_server.pl 

You can connect to the application at the URL: http://localhost:3000

The application doesn't do much. To add features, you have to begin customising the application