Catalyst First Steps
Building Web Applications Using Catalyst
Contents
Preparing Development Environment
Top BottomFor 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 BottomInstall 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
- Changes - used to list history of changes for module
- Makefile.PL - lists module dependancies for application
- README - explains how to test application, what it does and other instructions
- lib - contains all the perl code for the application
- MyApp
- Lite
- Controller
- Root.pm - for top-level dispatch actions
- Model
- View
- Lite.pm - contains application-level configuration and global methodsi for use by the application - stuff that does not change per-deployment
- myapp_lite.conf - per-deployment configuration, eg database connectioninfo, regional settings, authentication keys. Default format is Config::General, but also supports XML, YAML, Apache, INI, Perl, JSON
- root - for template files
- favicon.ico
- static - static files such as images, css, javascript
- images
- btn_120x50_built.png
- btn_120x50_built_shadow.png
- btn_120x50_powered.png
- btn_120x50_powered_shadow.png
- btn_88x31_built.png
- btn_88x31_built_shadow.png
- btn_88x31_powered.png
- btn_88x31_powered_shadow.png
- catalyst_logo.png
- script - catalyst helper scripts to run/test server and create models controllers and views
- myapp_lite_cgi.pl
- myapp_lite_create.pl
- myapp_lite_fastcgi.pl
- myapp_lite_server.pl
- myapp_lite_test.pl
- t - scripts for automated testing
- 01app.t
- 02pod.t
- 03podcoverage.t
Makefile
Top BottomMakefile.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 BottomThe 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 BottomHaving 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
