• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Zen: Building Maintainable Catalyst Applications
 

Zen: Building Maintainable Catalyst Applications

on

  • 3,624 views

After several years of building Catalyst applications, I've established a list of techniques that greatly increase maintainability....

After several years of building Catalyst applications, I've established a list of techniques that greatly increase maintainability.

Subtle points that are easy to understand, and easy to implement, that will help please your users and make your life easier.

Statistics

Views

Total Views
3,624
Views on SlideShare
3,592
Embed Views
32

Actions

Likes
4
Downloads
26
Comments
0

1 Embed 32

http://www.slideshare.net 32

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Bad things happen. Apps break, they throw exceptions.
  • I used to roll my own custom packages, but ErrorCatcher has changed most of that. Being able to emit
  • I have to alert myself in some way, and the user should see something sensical that either helps them help me, or at least helps them. <br /> This means an error action that is verbose, looks at the action that went wrong and is helpful. <br /> Sometimes I send email (using Catalyst::View::Email) or sometimes I just use Catalyst::Log::Log4perl and dispatch FATALs that way.
  • I used to roll my own custom packages, but ErrorCatcher has changed most of that. Being able to emit
  • Catalyst::Plugin::ErrorCatcher does a good job.
  • Catalyst::Log::Log4perl has a lot of dispatch options. It works very well, isn&#x2019;t slow and I can think of no reason to not use it.
  • Getting valid and useful information from end-users is maddeningly difficult. Programmers like to think they&#x2019;re so smart, but they still can&#x2019;t seem to figure this out and then berate users for not knowing what is relevant. <br /> It&#x2019;s the developers fault. Your error should explain what information is relevant, and if it can do that, just go ahead and send that on to the developers. Expecting the user to do it just means you&#x2019;re lazy. So don&#x2019;t be lazy, and if you want to be lazy, go hack on Rails.
  • Catalyst::Log is a very good start for a basic logging mechanism. It provides a rudimentary buffer, with a flush mechanism and the typical debug levels.
  • The already on CPAN enhanced logging package is Catalyst::Log::Log4perl, which really brings out a lot of power. You get all of the power of Log::Log4perl, but still it exists as $c->log.
  • Even if you use a logger that has all sorts of fantastic methods, you probably don&#x2019;t want to use them. Using the standard debug, info, warning, error and fatal methods gets you what you want, and there really aren&#x2019;t a lot of convincing arguments. If you adhere to those methods, then you can swap loggers in and out without having to update your app.
  • The fantastic _dump method gives you a pretty-printed output similar to what you&#x2019;d get with Data::Dump (and uses Data::Dump under the hood). <br /> <br /> This is an undocumented &#x201C;private&#x201D; method because it deviates from the standard logging methods above, but Catalyst::Log and Catalyst::Log::Log4perl both support it. It emits the message as an info message.
  • Right. Configuration. This with Chained is my primary reason for loving Catalyst. A lot of people don&#x2019;t know how to do this right.
  • Every Catalyst component, including the application (MyApp.pm), has a config accessor. As a rule, only modify this BEFORE setup is completed. After that, you can get heisenbugs. You&#x2019;ve been warned. So, this is how you configure something in a controller.
  • To configure that same controller at a higher precedence, you do it at the application level. These two configuration examples are identical and produce the same results.
  • To configure that same controller at a higher precedence, you do it at the application level. These two configuration examples are identical and produce the same results.
  • And to get at it, the big thing is to simply use accessors to get at the configuration key.
  • A moose example to create the &#x2018;foo&#x2019; accessor, since we have the &#x2018;foo&#x2019; config key (that was set to &#x2018;bar&#x2019;)
  • A moose example to create the &#x2018;foo&#x2019; accessor, since we have the &#x2018;foo&#x2019; config key (that was set to &#x2018;bar&#x2019;)
  • Or the old school way. The one that sucks.
  • The accessors work because config keys are merged into $self in the Catalyst component.
  • So, you have the hash entry &#x2018;foo&#x2019; that is set for you in your component to work with. This is merged in the way you would expect.
  • So if you have the configuration specified in the myapp_local file, that gets set in $self->{foo}. It just works down from there, traversing into the myapp.conf file and then the application configuration and finally the controller itself.
  • But, people still do this wrong.
  • This is wrong. While it will -probably- be right, don&#x2019;t do it. Simply do $self->{foo}. That is what you want, and in all cases will be the right value.
  • But, that case will almost always be correct. This case, however, will most likely not be. At component creation time, the configuration is merged into $self already. So use that.
  • Once you get an understanding of configuration, you really should use it and abuse it. Doing so will really enhance your applications and hopefully get you writing less code that does more. Which leads conveniently to the next point, number 4.
  • What makes a better controller? Well, to me, the thinner the better. The best way to do this is base classes.
  • The first and obvious step is to use more base classes. Put your generic code there, and benefit immediately.
  • A lot of controllers fit into specific types of behaviors, most of which are configurable. With the knowledge you now have of configuration, you can make very thin controllers that inherit from thicker base controllers and still delegate as much as possible to the models.
  • For most cases, sometimes your classes can really be nothing more than a config block. If you have a DBIC CRUD application, this is very easy to accomplish.
  • There&#x2019;s a package on CPAN that does this exactly, and gets you very far with nothing more than configuration blocks. I urge you to give it a try, as for most cases it works very well. Since it&#x2019;s a base class, you can override any methods it uses. It uses Chained, so it&#x2019;s very simple to alter behavior.
  • But, base classes aren&#x2019;t the best solution.
  • If you haven&#x2019;t been sold on the idea of roles versus inheritance (and multiple inheritance in particular), I&#x2019;m not going down that road. Catalyst 5.8 is all Moosey, which mean you can take advantage of Moose. Roles will make your code better. Promise.
  • This doesn&#x2019;t provide a lot of examples, but does point you in the right direction. If you combine roles with MooseX::Role::Parameterized you&#x2019;ll be amazed how flexible and generic your roles become. Then you do more with less code.
  • Moose is slower. Maybe. You probably won&#x2019;t notice in your web application, and it will be faster and more robust. Less code for you to test, and your tests will get messed up.
  • Pretty much all the core contributors to Moose are better programmers than I am, so why would I not use it? I would hire any of them, and you should to. Even better: use their work without paying them a dime. Awesome deal, isn&#x2019;t it?
  • Write tests.
  • The entire methodology is a route, letting you incrementally travel from point A to B. The steps can be easily modified and logged, so it&#x2019;s very very powerful.
  • This is my golden rule when building something. It&#x2019;s served me well, and if I work on an application with any significant number of users, I look at the tests to determine the workflow. Most of my tests are
  • Here&#x2019;s a snippet of a Test::FITesque test, which keeps status and context between the object tests, so I can mix and match. It makes it easy to test how things are done.
  • We&#x2019;ve all heard of mod_perl, and some of us have even used it.
  • But you should already know enough about mod_perl to use it. I&#x2019;m not saying it is bad, I&#x2019;m just saying that if you don&#x2019;t know that you need it, you probably shouldn&#x2019;t use it. So lets move on.
  • This means that your webserver (Apache) is spawning the FastCGI processes and handling the process management directly. This requires restarting your webserver to restart your application. I don&#x2019;t like that.
  • I prefer the external method, using a separate process manager and starting the FastCGI daemon and have it listen on a unix socket. This has a couple benefits, and ties into&#x2026;
  • Multiple processes can listen on a file socket, so you can start your new version on the same socket.
  • Which, is conveniently part of the manager that I use. It supports graceful restarting by opening the new instance on the FCGI socket and then shutting the old process down after the new is up and running. In other words, awesome and zero downtime.
  • There&#x2019;s also PSGI, and its reference implementation: Plack. Using Catalyst::Engine::PSGI lets you use this cool stuff, but it&#x2019;s new.
  • I ran a service with over 1,000 concurrent requests and pushing about 2Mbps of JSON data for an event, with Plack and AnyEvent (Tatsumaki, specifically). It works well, so go see Miyagawa-sans talk on Plack.
  • The main point of local::lib is that it separates your application from the vendor supplied paths. What this generally means is that on a per-user basis, you can have your own perl lib tree. Stuffing everything in the vendor perl has a lot of limitations, and it just isn&#x2019;t a good way to do it. You&#x2019;re really bound to whatever version of perl, bugs and all, the vendor supplies because updating Perl may break your entire system. Additionally, security patches supplied by your vendor may break Perl (Like Apple&#x2019;s latest security update).
  • You can do it, and most people do with few negative side-effects. It doesn&#x2019;t make it good, and considering how easy local::lib is to get going, there really isn&#x2019;t a reason to not use it.
  • In your application, if you keep your Makefile.PL (or Build.PL) up to date, you can install all the dependencies as the user that is running the application. You&#x2019;re guaranteed that the packages you install via local::lib are those running your application, so on a multi-user system (or multi-application, with one application per user) you can run different versions of different software.
  • In most cases, your application shouldn&#x2019;t run as root. You shouldn&#x2019;t have your dependencies requiring root, either. <br /> If you use external FastCGI, local::lib, you can run and deploy your entire application without ever touching the root account.
  • To setup local::lib, just install it from CPAN. When you load local::lib, it dumps out environment variable settings to use. Simply store those in your bash or cshrc profiles and you are set to go. Installing modules from CPAN or from a tarball puts them in a local, sanitary directory.
  • Want to start from scratch? No Problem. You still have a fallback to work from with your Perl, so removing your local::lib directory won&#x2019;t destroy Perl. You can start from scratch.
  • local::lib defaults to a &#x2018;perl&#x2019; directory that lives in your home directory. However, it is very trivial to have a lot of local::lib paths, and to even switch between them. <br /> <br /> This is particularly useful for doing smoke testing, if you have multiple environments.

Zen: Building Maintainable Catalyst Applications Zen: Building Maintainable Catalyst Applications Presentation Transcript