The Definitive Guide to Catalyst. Apress

Writing Extensible, Scalable,

and Maintainable Perl-Based

Web Applications

Write Well-Structured, Readable, and Maintainable Perl for Use on the Web.

Kieren Diment and Matt S Trout with
Eden Cardim, Jay Kuri, and Jess Robinson

E-books Shop
The Definitive Guide to Catalyst

About the Authors
Kieren Diment is a social researcher currently doing research in health
informatics in the School of Information Systems and Information Technology at the University of Wollongong, New South Wales, Australia. He was initially exposed to Unix in an artificial intelligence lab in 1994, where he learned emacs, shell, and a little bit about this thing called the World
Wide Web that seemed to be catching on. He spent the next eight years ignoring computers, working in psychiatric and neurological rehabilitation instead. After reading Neal Stephenson’s Cryptonomicon sometime in the early twenty-first century, he realized how much he enjoyed messing
around with computers, and it was a short step from installing Linux on an old laptop to
learning Perl. He now uses Perl every opportunity he gets in his work, mainly for doing clever
things with research data. He uses Catalyst mainly for showing this off. However, seeing as
web development is a small (but important) part of his work, he’s had to take an interest in the
documentation so that he can remember how it works—less RTFM than WTFM.
When not working, he can be found bushwalking or at the beach, where he surfs far too
infrequently. A few times a year, he rides a nine-foot longboard and is goofy-footed. When the
waves are heavy, or he is with his family (much more often), he bodysurfs instead.

Matt S Trout was thrust into Perl at the tender age of 17 by a backup accident and has been
happily using it for systems automation, network, web, and database development ever since. He is the second longest standing member of the Catalyst core team, the founder of the DBIx::Class ORM project, a Moose advocate since the early days of the object system, and a PAUSE administrator with a focus on helping find new maintainers for abandoned CPAN modules. Occasionally he finds time to write other CPAN modules and then inflict their maintenance on other members of the community, such as Devel::REPL, the interactive Perl shell, and Devel::Declare, the syntax extension system underlying MooseX::Declare and Method::Signatures::Simple.

Matt runs the technical team at Shadowcat Systems Limited, an open source consultancy
specializing in helping clients with design, development, and deployment of Catalyst and
DBIx::Class-based applications.

When occasionally not in front of his computer, he can be found playing Dungeons &
Dragons, producing a blur of hair on the dance floor at goth and metal clubs, or lurking in the
corner of a pub consuming copious quantities of real ale.

■Eden Cardim has a Bachelor of Science degree in computer science and has developed in
Perl since 1998. Experience with Perl applied to bioinformatics and web development has led
to involvement in CPAN open source projects, Reaction as a core developer, and Catalyst,
DBIx::Class, and Moose as a contributor.

■Jay Kuri has been working with application development and Internet systems architecture
since 1994. In the 15 years since, he has worked on software for everything from portable e-book
readers to 800-machine web clusters. In 2004 he founded Ionzero (http://www.ionzero.com), a
software development firm dedicated to producing the best, most secure, and most maintainable
software possible for its clients. Through Ionzero, Jay also stays active in the computing community
in the form of contributions to open source software and community building, including the
Perl and Catalyst website http://catalyzed.org/.
When Jay has free time, he enjoys spending it with his lovely (and patient) wife, Rebecca,
and son, Justin. He also enjoys the occasional motorcycle trip as well as the outlet of noncode-
related creativity in the form of woodworking and sketching.

■Jess Robinson learned Perl in 2000 for personal entertainment. She likes databases for some
reason and now successfully combines those two interests as the lead documentor on the
DBIx::Class project. She helps out in both #catalyst and #dbix-class supporting users, and she
occasionally writes her own Catalyst applications.

Acknowledgments
This book was a team effort, and we’re especially grateful to Jess Robinson (Chapter 6), Jay
Kuri (Chapter 8), and Eden Caram and Robert “Phaylon” Seladek (both Chapter 12) for agreeing
to write and do an initial review of a chapter each. Special thanks to Florian Ragwitz, who
provided Kieren with much of the code in the latter part of Chapter 10 and the Mason code in Chapter 11.

Invaluable support and advice were obtained from a number of people within the Catalyst
community, only some of whom are mentioned here. Apologies if I have forgotten your name
in this list. Thanks to Jesse Sheidlower, Rafael Kitover, Jay Shirley, Joel Bernstein, Andrew
Rodland, Tomas Doran, Kennedy Clark, Dan Dascalescu, Devin Austin, and Marcus Ramberg
for help with code at timely moments. Thanks to Charley Harvey for the “out of the box” comment
in the introduction. In Chapter 11, Rafael Kitover also wrote the code for the WrapCGI
recipe, and Alexander Hartmeier provided code for the Email::Template recipe. Thanks also
go to Jos Boumans for the Advanced Catalyst::Test recipe and to Mateu Hunter and the other
MojoMojo developers for their article. Apologies to Luke Saunders for not having the time to
use the material he provided on Fixtures in DBIx::Class. Also in Chapter 11, thanks go to Hideo
Kumara and Cory G. Watson for fixing bugs in their software in a timely manner.
Special thanks Dave Rolsky for reviewing a portion of Chapter 2 based on his experience
writing the Moose manual. I know that the section of Chapter 2 that I rewrote based on his
advice will still not meet his exacting standards, but he caused it to be greatly improved.
Thanks to Dyana Wu for LolCatalyst.

Penultimately, thanks to the whole Catalyst community for making this not only the most
useful of the Perl web frameworks, but also the most popular.

Finally, I would like to thank my family for tolerating me when I had to lock myself away
to write enough material to meet deadlines. I couldn’t have written this book without your support.
Kieren Diment

Introduction

Catalyst is a web framework written in Perl and inspired by Perl. As with its parent language,
Catalyst makes the easy things easy and the hard things possible. Unlike other frameworks,
it comes with very little out of the box, and in fact doesn’t really come with a box. It’s not
designed to make the first half-day of your programming experience easy; it’s designed so that
the speed of development of your code is just as quick for you at the beginning of a project as it
is in the middle, at the end, and some period of time later when you realize you have to return
to a long-forgotten project to do maintenance programming or add new features.
In the rest of this introduction, we’ll help you work out if this book is for you. While
Catalyst is reasonably simple, it does require that you have some familiarity with Perl. You
can learn Perl and Catalyst concurrently, and in some ways this would be a good way to
learn how to program disciplined, structured, object-oriented Perl, but it will make the
learning curve steep.

If you’re familiar with Perl data types, references, and objects, then you can go straight
to Chapter 1. If you feel that you are already a skilled web programmer and Perl programmer,
then you can probably start with Chapter 3. If you’re not completely confident with Perl, on
the other hand, you’ll want to go through this review. The rest of this introduction will help
you decide if you need to read Chapter 2.

Assessing Your Perl Literacy
The following sections describe a continuum of Perl programming skills. A beginning programmer
will need to go elsewhere to brush up on Perl before being able to make full use of
this book. An intermediate or advanced programmer will be able to dive right in. Read on to
determine your level of Perl literacy.

Beginning
A beginning Perl programmer has little or no exposure to programming in Perl but may have
written very short Perl scripts. The beginner’s code is likely to be procedural in style and will
not make significant use of CPAN. If you are a beginner, the next thing that you need to learn
about is references. The tutorials at the PerlMonks site (http://www.perlmonks.org/?node=Tutorials), and in particular the tutorials on references (http://www.perlmonks.org/?node_id=591878), are good resources for this. Despite the fact that Catalyst uses some advanced
coding/programming techniques, there is really no barrier to beginning Perl programmers
using Catalyst, and in fact Catalyst may have some advantages, as its structure encourages the
development of good practices from a very early stage.

If you’re a raw beginner, you’ll probably need to go and do some more reading before you
can program with Catalyst effectively.

Intermediate
An intermediate Perl programmer has started using Perl in an object-oriented manner. He
or she is confident with using references (e.g., dispatch tables) and object-oriented Perl.
The contents of the book Perl Best Practices by Damian Conway (O’Reilly, 2005) make sense
to intermediate programmers, in that they are confident that they know which bits of the
book’s advice are sound for them and which to ignore. The next step for intermediate-level
programmers is to start writing code as if it was a CPAN module and to make use of Perl’s
testing infrastructure.

If you’re an intermediate-level Perl programmer, you can make good use of this book, but
you will need to pay attention to some of the more advanced concepts. The Moose section in
Chapter 2 may be of particular use to you.
Advanced
An advanced Perl programmer is familiar with all of the above and has probably written his or
her own CPAN modules. The advanced programmer is familiar with the various object systems
available for Perl 5 and why Moose represents a significant advance on the other Perl object systems.

If you’re an advanced Perl programmer, this book will assist you in getting up to speed on
Catalyst quickly and help you consolidate your knowledge and guide other, less-skilled programmers.
If you are in this category, then you should at least skim Chapter 3, but you will find
Chapters 4 and beyond more useful.

Catalyst and Perl
Perl has been around for a long time, so the continuum in the preceding sections reflects
Perl’s history. Perl started life as a procedural language. Prior to Perl version 5, objectoriented
programming wasn’t possible (beginner capability). However, with the release of
version 5, Perl began to have object-oriented capabilities (intermediate capability). Perl’s
object-oriented architecture is based on its strength as a text-processing language, and it
has hash-based dispatch tables at its core. Moose, the latest development in object-oriented
programming in Perl 5, simultaneously simplifies and makes more powerful Perl’s objectoriented
capabilities (advanced capability).

Since Catalyst version 5.8, all Catalyst development is based on Moose (which, by the
way, is not an acronym—it’s just software named after a large herbivorous mammal, a bit of
a tradition in the Unix toolchain). Moose is an all-in-one object system that really simplifies
object-oriented coding in Perl, greatly easing the transition from beginner to intermediate and
intermediate to advanced Perl programmer. We look at modern object-oriented Perl using
Moose in Chapter 2.
Finally, we’d like to stress in this introduction that it’s Catalyst’s job to provide you with
the tools that you need to make web programming easier. However, it’s also Catalyst’s job to
get out of your way and let you concentrate on your programming problem. This is Catalyst’s
strength and what makes it such a powerful tool. Used properly, Catalyst will speed up and
simplify your web development activities for a long time to come.


Screenshot

E-books Shop

Purchase Now !
Just with Paypal



Product details
 Price
 File Size
 12,805 KB
 Pages
 362 p
 File Type
 PDF format
 ISBN-13
 ISBN-10
 978-1-4302-2365-8
 978-1-4302-2366-5
 Copyright
 2009 by Kieren Diment and Matt S Trout 

Contents at a Glance
Foreword . xv
About the Authors . xix
About the Technical Reviewer . xxi
Acknowledgments . xxiii
Introduction . xxv
Chapter 1 Introducing the Catalyst Web Application Framework . 1
Chapter 2 Catalyst Setup and Background Knowledge . 19
Chapter 3 Your First Catalyst Application. 53
Chapter 4 Extending LolCatalyst-Lite. 81
Chapter 5 Deployment. 109
Chapter 6 Database Models. 135
Chapter 7 The Catalyst Dispatcher. 167
Chapter 8 Authentication and Authorization . 189
Chapter 9 Interacting with Web Services in Your Applications . 219
Chapter 10 Extending Catalyst. 233
Chapter 11 Catalyst Cookbook . 247
Chapter 12 The Reaction Component UI Framework. 303
Appendix A Compiling Your Own Perl. 315
Appendix B Making Your Own CPAN Mirror
with CPAN::Mini or minicpan.sh. 317
INDEX . 319

Table of Contents
Foreword 
About the Authors
About the Technical Reviewer 
Acknowledgments
Introduction
Chapter 1 Introducing the Catalyst Web Application Framework
Catalyst’s Usage and History. 2
The Model/View/Controller Pattern. 2
URL Path Dispatch Logic. 6
High-Level Session Management. 7
Authentication and Authorization . 7
Logging. 7
Templates. 8
Testing. 8
Deployment. 8
Catalyst vs. CGI . 9
Introducing Persistence FastCGI. 12
mod_perl. 12
Catalyst vs. Other Frameworks. 13
URI Routing via External File vs. Self-Contained Controllers . 13
Catalyst vs. CGI::Application. 14
Catalyst vs. Jifty. 16
Summary. 17
Chapter 2 Catalyst Setup and Background Knowledge
Installing Catalyst. 19
Choosing Hardware and Software. 19
Obtaining Perl. 21
Setting Up CPAN . 23
Installation. 24
Catalyst Background Knowledge . 24
Hypertext Transfer Protocol. 24
Object-Oriented Programming with Moose. 28
Making the Most of CPAN . 44
Search Strategies for CPAN. 45
Basic CPAN Usage. 47
Summary. 52
Chapter 3 Your First Catalyst Application
Sample Application Overview . 53
Files and Directories. 54
Top-Level Directory. 54
lib Directory. 55
root Directory . 55
script Directory . 55
t Directory. 56
Getting Started Writing Our Application. 57
Writing the Application. 59
A Brief Explanation of Views . 61
Our First Template. 62
A Brief Introduction to Catalyst Models. 65
Adding a Different View . 66
Testing. 67
Automated Testing. 69
Fixing the Plan . 71
Checking and Fixing the JSON Request. 72
Adding Authentication for the Web Portion of Our Application. 74
Adding a New User . 75
Requiring Authentication in the Application . 75
Requiring Authentication for Web Services Requests Only. 75
Requiring Authentication for the Whole Application. 79
Summary. 79
Chapter 4 Extending LolCatalyst-Lite
First Comes the Model. 82
New Model Capabilities .82
Application As Pure UI. 82
Measure Twice, Code Once. 82
Always Run the First Code from a Test . 83
Extracting the Model. 83
Moving the Model Logic into an Application-Independent Class. 84
Extending the Model. 87
A Driver Model for Generic Translation . 88
Fixing Up the Makefile.PL. 89
Enforcing the Interface Using a Role. 93
Interlude: LolCatalyst::Lite::Model::SnippetStore. 94
Another Interlude: Spotting Business Logic Errors. 96
Extending the Application. 96
First New Features. 97
A Chained Controller. 97
Adding Templates . 100
Back to the Test Cycle .101
Interlude: Converting the Snippets to Be Object Based. 101
Thin Controller, Smart Model. 102
Adding Tests for the New View Functionality. 105
Summary. 108
Chapter 5 Deployment
Using the Built-in Server . 109
Deploying with the Built-in Server . 110
HTTP::Prefork Engine Behind a Front-End Proxy. 111
FastCGI Deployment. 117
Deployment on Shared Hosting. 118
FastCGI Deployment on Your Own Web Server. 118
Deployment on mod_perl . 123
Using Apache As a Back End to a Front-End Proxy. 125
Getting Your Code to the Server. 125
Rsync. 125
Version Control Checkout. 126
make dist. 126
To make install or Not to make install, That Is the Question. 127
Dependency Installation. 128
The cpan . Command. 130
make installdeps . 131
make catalyst_par. 132
Summary. 133
Chapter 6 Database Models
Choosing and Connecting to a Database. 135
Popular Database Systems . 135
Connecting to the Database from Catalyst . 136
Designing and Creating the Sample Application Database . 137
Creating the Catalyst Model. 139
Exploring the DBIx::Class Schema Files .141
The Schema File. 141
The Result Files . 142
Deploying to a Different Database System. 147
Dealing with Data . 149
Simple CRUD. 150
Creating a User. 151
Displaying Users. 155
Updating Users. 157
Adding Roles. 159
Adding Roles to Users. 160
Removing a User . 163
Listing All Users and Their Roles. 164
Summary. 166
Chapter 7 The Catalyst Dispatcher
How Catalyst Prepares Before Dispatch . 169
The Basic Dispatch Process. 169
Two Special Dispatch Types: begin and auto. 171
LocalRegex and Regex Actions. 175
Internal Request Flow Control: $c->forward,
$c->detach, $c->go, and $c->visit. 177
forward and detach. 178
visit and go. 180
The Chained Dispatch Type. 181
Overriding the Public Dispatch Paths with Configuration. 187
Summary. 188
Chapter 8 Authentication and Authorization 
The Three Parts of Authentication . 189
Authentication Realms . 190
Persistence. 191
Authorization with Roles. 192
Installing the Required Modules. 193
Catalyst::Plugin::Authentication. 193
Catalyst::Authentication::Store::DBIx::Class . 193
Catalyst::Plugin::Authorization::Roles. 194
Catalyst::Plugin::Session. 194
Our Example Application: SneakyCat. 194
Creating and Configuring SneakyCat. 195
The SimpleDB Option . 200
A Side Trip into Application Setup. 200
Using Authentication. 203
Working with Authentication After Login. 207
Trying Out the Application . 209
Adding Finer-Grained Access Control
to SneakyCat with Authorization . 211
Working with the Authenticated User. 216
Summary. 218
Chapter 9 Interacting with Web Services in Your Applications
Consuming a Simple REST Service . 219
Setting Up OpenCalais in a Catalyst Application . 220
Creating Web Services with REST . 224
Implementing the REST Controller . 224
Changing the Data Type for Serialization. 228
Remaining Options for Web Services. 229
Summary. 231
Chapter 10 Extending Catalyst
Base Controllers. 233
A Simple SerialiseStashKey View. 234
Action Classes. 237
Adding Attributes to Controller Actions . 237
A Simple Example . 238
A More Sophisticated Example . 238
Adding More Than One New Attribute. 240
Cleaning Up Multiple Attributes with Roles. 241
Extending Catalyst with Plug-ins . 242
Helper Methods in MyApp.pm. 242
Catalyst::Plugin::SmartURI. 244
Catalyst::Plugin::Static::Simple. 244
Catalyst::Plugin::Server. 245
Summary. 245
Chapter 11 Catalyst Cookbook
Model/View/Controller. 247
Import Existing Schemas to DBIx::Class . 248
Use DBIx::Class::Tree and Display with JavaScript
and the Template Toolkit. 249
Catalyst::Model::Factory and Catalyst::Model::
Factory::PerRequest. 254
A Way to Quickly Resolve Browser Compatibility Issues. 255
Output CSV from a Controller Action. 258
Dynamically Generate Graphics with Catalyst::View::GD . 259
Dynamically Generate Barcodes from Catalyst. 260
Produce Graphs with Chart::Clicker and Catalyst::
View::Graphics::Primitive. 262
Alternative Templating Systems. 264
Easy Spam Protection with Catalyst::Controller::reCAPTCHA. 267
Catalyst::View::Component::SubInclude .269
Handle Legacy CGI Scripts in Catalyst. 270
Intelligent Use of Base Controllers . 272
Catalyst::View::Email::Template . 275
Miscellaneous Recipes. 277
Run Parts of an Application As a Stand-Alone Script. 277
MojoMojo: The Catalyst Wiki. 278
Persistent Login and API Authentication . 281
Multirealm and Progressive Authentication. 286
Deploy with a Cache. 289
Development Process. 292
Enforce Coding Standards with Perl::Critic and Perl::Tidy . 292
Advanced Usage of Catalyst::Test. 294
Workflow Testing with Test::WWW::Mechanize::Catalyst. 296
Write a Minimal Test Case to Troubleshoot. 297
Bash Aliases to Make Starting the Development Server Easier. 299
Alternative to perl -d: Devel::REPL and CatalystX::REPL. 300
Summary. 301
Chapter 12 The Reaction Component UI Framework
Reaction Overview. 303
The Reaction Arsenal . 304
Metaprogramming. 304
Infrastructure . 305
Basic Interface Model Components . 306
Basic UI Components . 306
Example Reaction Application. 308
Setup. 308
Our Interface Model. 308
The View. 310
The Skin. 311
Tying the Interface Model to the Application. 311
The Root Controller . 311
The LayoutSet. 313
The Widget . 314
Running the Application. 314
Summary. 314
Appendix A Compiling Your Own Perl
Patching Perl 5.10.0 to Work (Properly) with Catalyst  .316
Appendix B Making Your Own CPAN Mirror
with CPAN::Mini or minicpan.sh
Browsing Your minicpan on the Web. 318
INDEX . 319

  ●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●

═════ ═════

Previous Post Next Post