Catalyst

By Jonathan Rockway
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies

About this book

Many web applications are implemented in a way that makes developing them painful and repetitive. Catalyst is an open-source Perl-based Model-View-Controller framework that aims to solve this problem by reorganizing your web application to design and implement it in a natural, maintainable, and testable manner, making web development fun, fast, and rewarding.

Everything that your web application needs to do is only written once; you connect to the database in one place, have configuration in one place, etc. Then, you just write actions for each URL that your application needs, without worrying about the database connections or HTML to produce. Catalyst will handle the details so you can worry about writing your application.

Catalyst is designed to be reliable. There are hundreds of production applications and thousands of users. The code is well-tested, and new releases almost always maintain compatibility with applications written for older versions. You don't have to worry about Catalyst breaking your application and slowing down your development. It just works.

Most importantly, Catalyst has a thriving community. You can ask a question on the IRC channel and get a response at almost any time of the day.
 
This book embodies Catalyst's philosophies of Do It Yourself and Don't Repeat Yourself.

Publication date:
December 2007
Publisher
Packt
Pages
200
ISBN
9781847190956

 

Chapter 1. Introduction to Catalyst

Writing a web application is usually a repetitive process. For every page, you create a new script. This script performs essentially the same tasks as the other scripts in the application: connect to a database, get some input from the user, and produce a web page as the result. This style of application design results in a structure like this:

Building an application with this sort of design is certainly not a difficult process, but the constant repetition can be prone to error. If you decide to change your database, you will have to update the database connection string in each file. If you happen to forget one obscure file, you might not be aware until your users start sending you angry emails.

Of course, this problem is not limited to the database connection strings. As the diagram shows, repeated elements are everywhere. As each page generates its own HTML, a request from your web designer to update the layout of a page will involve having to dig through your Perl source code (potentially breaking database queries or other logic), just to change some HTML. In addition any time you make a change that affects your entire site or application, you'll have to make the change a number of times; once for each "page". Similarly, fixing one bug will entail copying the fix to every other place. This is inconvenient, a waste of time and just plain boring.

Catalyst is an open source Perl-based Model-View-Controller framework that aims to solve this problem by reorganizing your web application.

Catalyst Application Architecture

Rather than making each location that a user may visit an individual file, we make each location an action inside a Controller class. Catalyst sits in front of each of these controllers, and when it receives a request, it dispatches the request to the proper action. When an action is called by the dispatcher, it's in a pre-configured environment—all configuration options have been read from a configuration file, all databases are connected, all parameters from the request have been parsed, etc. All your action has to do is implement the logic associated with the request; there is no setup that you have to manually perform.

Although this dispatch mechanism is already an improvement over the traditional process, Catalyst doesn't stop here. Your actions can call on the help of Models and Views, as well.

A Model is a source of data. Most applications use a single relational database like MySQL or PostgreSQL as their Model, but Catalyst does not require that—you can use anything from files on disk, to an email server, or remote RSS feeds; and you can have as many Models as your application needs.

Each Model knows how to configure itself at startup (for example, connect to the database server), so you'll never have to worry about that. If you need to use a different database, you can change the connection information in one file, restart your application, and the change will take effect everywhere, automatically. The repetition that a traditional application would require is simply non-existent in Catalyst.

Lastly, a View is another component that your action may use. A View is a way to turn the raw data that your action generates into something more useful to your application's users. Typically, applications have a View that converts the data into an HTML page via a templating system, like the Template Toolkit, Mason, or ClearSilver.

Templating systems abstract your HTML or XML away from your Perl code, so the web designer can edit it without knowing anything about Perl or the underlying design of your application.

You don't have to completely understand what MVC is right now—we'll start learning the details in the next chapter. For now, think about all the code you've cut-n-pasted between files in your application and how Catalyst will eliminate that forever.

Extensibility

MVC isn't all that Catalyst provides. At the time of writing, there were over 190 freely-available plugins that added new features to Catalyst. Existing plug-ins offer functionality such as configuration file parsers, specialized logging tools, email interfaces, caching, user authentication and authorization; cryptography, internationalization, and localization, browser detection and even virus scanning. Most of these plug-ins drop right into your application and instantly provide you with the advertised functionality. This saves you from having to reinvent the wheel for common (and uncommon!) tasks.

In addition to plug-ins, pre-made Models, Controllers, and Views (collectively called components) are also available. Since Catalyst is fully object-oriented, your application-specific components can simply "subclass" these off-the-shelf components and customize the functionality to whatever degree is necessary.

Finally, if there's no pre-built component available, there's the possibility that a generic Perl module from the CPAN (Comprehensive Perl Archive Network; http://search.cpan.org/) can help you—there are over 10,000 modules currently available!

Reusability

Another advantage of Catalyst's MVC design is that the components need not be specific to one application. If you're developing a number of related applications, you can share components between them with minimal (or no) modification, since the components are just Perl objects that need not know anything specific about your application. (Of course, if you need a component that's specific to your application, that's no problem for Catalyst.)

Flexibility

As Catalyst handles all the details of loading your components and handling requests, you aren't tied to any details of this process. This means that you can deploy your application with any web server (Apache and lighttpd are widely used) under whatever configuration best suits your environment. If you're using mod_perl with Apache 1.3, that's no problem for Catalyst. If you're using lighttpd as a proxy to several backend FastCGI servers, Catalyst will work great. Catalyst even comes with a built-in HTTP server, so you can develop applications without having to install and configure a web server.

With Catalyst, like Perl itself, There Is More Than One Way To Do It, and Catalyst will bend to fit your environment so you can use what you're already familiar with.

Reliability

Finally, Catalyst is designed to be reliable. The run-time package comes with over 2600 unit tests that automatically check the reliability of Catalyst when you install it. In the unlikely event that there's something wrong with your Perl environment, Catalyst won't install until it's fixed. There's always the possibility of a bug in Catalyst, but nothing major has come up because the core developer team tries to keep Catalyst as small as possible. Everything that's non-essential is kept in plug-ins or in reusable base components. (We try to keep these stable too, of course.)

This reliability isn't just for the Catalyst developers though. When you create your application (or components within it), unit tests are automatically generated. These tests serve to make sure that your application is at least minimally functional. If you choose to, you can easily add your own automatic tests that can test every part of

your application. Rather than manually going through your site when you make a major (or minor) change, you can have the computer double-check everything while you get a cup of coffee. Testing is examined in detail in Chapter 8.

 

Catalyst Application Architecture


Rather than making each location that a user may visit an individual file, we make each location an action inside a Controller class. Catalyst sits in front of each of these controllers, and when it receives a request, it dispatches the request to the proper action. When an action is called by the dispatcher, it's in a pre-configured environment—all configuration options have been read from a configuration file, all databases are connected, all parameters from the request have been parsed, etc. All your action has to do is implement the logic associated with the request; there is no setup that you have to manually perform.

Although this dispatch mechanism is already an improvement over the traditional process, Catalyst doesn't stop here. Your actions can call on the help of Models and Views, as well.

A Model is a source of data. Most applications use a single relational database like MySQL or PostgreSQL as their Model, but Catalyst does not require that—you can use anything from files on disk, to an email server, or remote RSS feeds; and you can have as many Models as your application needs.

Each Model knows how to configure itself at startup (for example, connect to the database server), so you'll never have to worry about that. If you need to use a different database, you can change the connection information in one file, restart your application, and the change will take effect everywhere, automatically. The repetition that a traditional application would require is simply non-existent in Catalyst.

Lastly, a View is another component that your action may use. A View is a way to turn the raw data that your action generates into something more useful to your application's users. Typically, applications have a View that converts the data into an HTML page via a templating system, like the Template Toolkit, Mason, or ClearSilver.

Templating systems abstract your HTML or XML away from your Perl code, so the web designer can edit it without knowing anything about Perl or the underlying design of your application.

You don't have to completely understand what MVC is right now—we'll start learning the details in the next chapter. For now, think about all the code you've cut-n-pasted between files in your application and how Catalyst will eliminate that forever.

Extensibility

MVC isn't all that Catalyst provides. At the time of writing, there were over 190 freely-available plugins that added new features to Catalyst. Existing plug-ins offer functionality such as configuration file parsers, specialized logging tools, email interfaces, caching, user authentication and authorization; cryptography, internationalization, and localization, browser detection and even virus scanning. Most of these plug-ins drop right into your application and instantly provide you with the advertised functionality. This saves you from having to reinvent the wheel for common (and uncommon!) tasks.

In addition to plug-ins, pre-made Models, Controllers, and Views (collectively called components) are also available. Since Catalyst is fully object-oriented, your application-specific components can simply "subclass" these off-the-shelf components and customize the functionality to whatever degree is necessary.

Finally, if there's no pre-built component available, there's the possibility that a generic Perl module from the CPAN (Comprehensive Perl Archive Network; http://search.cpan.org/) can help you—there are over 10,000 modules currently available!

Reusability

Another advantage of Catalyst's MVC design is that the components need not be specific to one application. If you're developing a number of related applications, you can share components between them with minimal (or no) modification, since the components are just Perl objects that need not know anything specific about your application. (Of course, if you need a component that's specific to your application, that's no problem for Catalyst.)

Flexibility

As Catalyst handles all the details of loading your components and handling requests, you aren't tied to any details of this process. This means that you can deploy your application with any web server (Apache and lighttpd are widely used) under whatever configuration best suits your environment. If you're using mod_perl with Apache 1.3, that's no problem for Catalyst. If you're using lighttpd as a proxy to several backend FastCGI servers, Catalyst will work great. Catalyst even comes with a built-in HTTP server, so you can develop applications without having to install and configure a web server.

With Catalyst, like Perl itself, There Is More Than One Way To Do It, and Catalyst will bend to fit your environment so you can use what you're already familiar with.

Reliability

Finally, Catalyst is designed to be reliable. The run-time package comes with over 2600 unit tests that automatically check the reliability of Catalyst when you install it. In the unlikely event that there's something wrong with your Perl environment, Catalyst won't install until it's fixed. There's always the possibility of a bug in Catalyst, but nothing major has come up because the core developer team tries to keep Catalyst as small as possible. Everything that's non-essential is kept in plug-ins or in reusable base components. (We try to keep these stable too, of course.)

This reliability isn't just for the Catalyst developers though. When you create your application (or components within it), unit tests are automatically generated. These tests serve to make sure that your application is at least minimally functional. If you choose to, you can easily add your own automatic tests that can test every part of

your application. Rather than manually going through your site when you make a major (or minor) change, you can have the computer double-check everything while you get a cup of coffee. Testing is examined in detail in Chapter 8.

 

Installing Catalyst


Now that you're convinced that you want to try Catalyst, you just have to install it. Since Catalyst is Perl-based, you'll need to have a working version of Perl. Most Linux and BSD distributions come with a suitable Perl in the default install, as do many commercial Unix variants. (Mac OS X is no exception.) Microsoft Windows doesn't ship with Perl, but you can get a base Perl distribution for free from ActiveState, Strawberry Perl, or Cygwin. Regardless of your operating system, you'll need Perl version 5.8.1 or higher (5.8.8, the latest at the time of writing, is the version used in the book).

The easiest way to install Catalyst is via your operating system's package manager. This mostly applies to Linux and the BSDs and the syntax is specific to your distribution. On Debian-based versions of GNU/Linux, the command is:

$ apt-get install libcatalyst-perl

On FreeBSD or OpenBSD, you can build the Catalyst "port" by running the following commands as root:

$ cd /usr/ports/www/p5-Catalyst-Runtime
$ make install

If you're using ActiveState on Windows, ActiveState has a "PPM" package for Catalyst. You can find the PPM at http://cpan.uwinnipeg.ca/dist/ Catalyst-Runtime.

Finally, be sure to check your operating system's manual for specific instructions on installing packages. The latest details are available from the Catalyst Installation guide at http://search.cpan.org/perldoc?Catalyst::Manual::Installation.

CPAN

If your operating system doesn't provide pacakges, installing Catalyst directly from the CPAN is a simple process. Perl ships with a utility called cpan that handles the installation process for you. Simply run this from the command line:

$ cpan Catalyst::Runtime Catalyst::Devel

and Catalyst (and all of its dependencies) will be downloaded, integrity-checked and installed.

The disadvantage of this approach is that you'll need a C compiler gcc, make, and related development utilities. Most Linux distributions have these available, but they aren't a part of the default install. Mac OS X offers gcc and make as part of its "developer tools" package, which you'll need to install if you choose to use cpan.

The cpan script is, unfortunately, notorious for asking difficult questions the first time that you run it (in order to build a configuration suitable for your system). If you want to avoid these questions and just get Catalyst installed, try the cat-install script at http://www.shadowcat.co.uk/static/cat-install. You can download that file, run it with Perl, and Catalyst should be installed when it's finished running.

 

Where to go for Help?


If cpan is just not working for you and you feel lost, the Catalyst community would love to help you out. The best place for questions is the #catalyst IRC channel on irc.perl.org. Users, developers, and other helpful people monitor this channel and are happy to answer Catalyst-related questions.

For longer questions, email is probably a better means of communication. Catalyst has an English-language users list that you can sign up for at: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst.

As Catalyst becomes more popular, new lists are being created for other languages. Catalyst-de was recently created for German-speaking users, and there's interest in creating a Japanese-language mailing list. Many readers of the English list speak other languages, so if you're having trouble with English, say so and someone can likely assist you in your native language.

Finally, the main project web site, http://www.catalystframework.org/, provides links to useful community tools including the Catalyst wiki, Planet Catalyst (a collection of Catalyst-related blog postings), and the Catalyst Advent Calendar (a mini-cookbook published each December).

As always, before posting to the mailing list or asking on IRC, do a quick Google search to see if you're experiencing a common problem.

 

Summary


Many web applications are implemented in a way that makes developing them painful and repetitive. Catalyst, an MVC framework for Perl, lets you design and implement a web application in a natural, maintainable, and testable manner. Everything that your web application needs to do is only written once; you connect to the database in one place, have configuration in one place, etc. Then, you just write actions for each URL that your application needs, without worrying about the database connections or HTML to produce. Catalyst will handle the details so you can worry about writing your application. Thanks to Catalyst's plug-in system, it can do more than just dispatch requests—it can manage sessions and users for you, as well as handle many other common tasks. Catalyst is designed to be reliable. There are hundreds of production applications and thousands of users. The code is well-tested and new releases almost always maintain compatibility with applications written for older versions. You don't have to worry about Catalyst breaking your application and slowing down your development. It just works.

Most importantly, Catalyst (and Perl) has a thriving community. You can ask a question on the IRC channel and get a response at almost any time of the day. There are also resources on the web for learning more; blogs from developers and users, recipes on the advent calendar and a wealth of questions answered on the mailing list.

About the Author

  • Jonathan Rockway

    Jonathan Rockway, a member of the Catalyst Core Team, has been programming Perl since his middle-school years. He became professionally involved with Perl when he was a desktop support minion at the University of Chicago and inherited a mod_perl application. He now works as a software developer at Infinity Interactive. In his spare time, he maintains a collection of modules on the CPAN and tries to speak at as many Perl conferences as possible.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Access now