Building notes, projects, and occasional rants

Ruby on Rails, and Catalyst

So, after having learned a bit about Ruby, the next logical step for me was to try Ruby-On-Rails.

A bit of background: before going to work at Sapo in the XMPP Messenger product, I was a lead-developer at Novis. My main task there, was to build websites and provisioning systems, and the glue that ties billing systems with technical databases. We used a framework that was developed internally since ‘99, and in it’s latest generation was called Apache::WAF. It was a MVC-style framework, with a strict separation between code and layout. The View was Template Toolkit, but it was extensible to other technologies (we used Mason a lot also). The Model where our own provisioning system libraries and other stuff. Apache::WAF allowed us to build the Controller very quickly. Also, it was pretty fast, using mod_perl.

So using MVC-style frameworks in web development is nothing new to me.

Except that Ruby-On-Rails is much more than that, and in a extremely clean package. You can get up-to-speed in no time, thanks in part to the generation of code that is the base of RoR, and also to the Screencasts available at the Ruby-On-Rails website.

Also, having a clean and powerful language behind it, it sure helps.

It’s not perfect. The part of mapping URLs to controllers could be better. RoR uses the same setup that Apache::WAF uses: you register URL namespaces on a special config file and associate them with controllers. The main advantage I see with this approach is that you can look at this single config file and know all the URLs of your app, so clashes are less likely.

The Model part of RoR is just excellent. As far as I can tell, there is no Perl equivalent to ActiveRecord. Yes, yes, I know about Class::DBI and Alzabo, and the others. But those solutions solve the CRUD problem: creating, retrieving, updating and deleting one object at a time. They all have solution for forging relations between tables and objects. But ActiveRecord does all that and more. It’s the first framework (and I really expect and would love to see some perl guru to jump on me on this one and say “Have you looked at X on CPAN?”…) I’ve seen that solves the 1+100 queries problem efficiently.

The 1+100 queries problem is simple to understand: suppose you have a Books table and a Authors table (for simplicity, a book can only have one author), and you want to list all the books with the respective authors.

With a basic CRUD framework, you do a Books->find_all or Books->search to get all the books and for each one, you do something like $book->author->name to get the author name. If the first query for books returns 100 books, you’ll then do 100 queries for authors. Yes, yes, you could cache ID->name of authors, but that really is not the point. The point is that something that should be done with a simple join at the database level with a single query, is now being done with multiple queries (and if you write to me saying that “Yeah, but my MySQL is fast enough you wont even notice”, I’ll warn you right now that I’ll verbally abuse you. Real world is a lot different).

ActiveRecord is able to solve this. You can say that “Please give me all the books, and also their respective authors” and with a single query to the database, you’ll get a list of Book object with the author method returning a pre-created object. You can even specify which fields you need from author.

It’s very very good! The Model in Ruby-On-Rails is for me the part that makes the most difference to the other MVCs I’ve used in the past.

Of course the Perl community is not standing still. For quite some time, I knew about Maypole. I’ve tried to use in the past and it was a painful experience. The setup was not straight forward as I would expect (in Perl, simple things should be easy). Maybe I’m dense, but it took me some time to get some type of application going.

Recently, Catalyst emerged as the new kid on the Perl block. I’ve used it also in some early versions, and I decided to try it again last week.

It’s very very good. It borrows a lot of ideas from RoR, like the code generation features. The View can be Template Toolkit (like Apache::WAF), but also Mason, HTML::Template and others.

The Controller is nice. They use method attributes to specify the URL that your class listens to. For example, if your App is Library, and you have a Controller Library::C::Author, and inside that you write:

sub add : Local { code }
sub view : Path('/my/path') { code }
sub show : Regex('^show_author/(\d+)$') { code }

You’ll get the following URLs: /author/add (it uses the class name after Library::C as a URL path), /my/path, and /show_author/42.

Each URL will be dispatched to the correct method.

It’s very nice, and in contrast with RoR, you see the URLs in the controller. It can be dangerous because two different controllers can claim the same Global URL, but overall I prefer this approach. It has all the power of RoR (the add method above is like RoR), and much more. Of course, I can be totally wrong, I’m not an expert with either of these two frameworks, but as far as I can tell, this is the way things are.

But the feature I like the most in Catalyst is the simplicity. It’s just dead simple to use! If you know your Perl, you can be up and running in 5 minutes or less. Comparing with Apache::WAF, I can see that we did a lot of stuff there that it’s not needed.

Catalyst is a perfect example of the KISS principle (and I’m amazed I could not find a Wikipedia page about the KISS principle to link to).

The Model part can be anything: Class::DBI, Net::LDAP, you name it. But as I said above, I don’t know a Model as good as ActiveRecord in Perl, so for now, the Model part is inferior in Perl-land.

To see more of Catalyst in action, check out the article at

AJAX as been in the news and both of these two frameworks support it very well. They both use the Prototype JS library. This library was developed with RoR on it’s mind, and it shows. Although its the same one, I saw some shortcuts to do cool and useful stuff in RoR that don’t seem to be present in Catalyst (my favorite example of this is the upload ajax widget). But that is a small issue. It’s easy to see how it works in Rails and port it to Catalyst.

Although I’ve been using Prototype, there is a non-free alternative that you should be aware of. The nice folks at Backbase have a extremely powerful JS framework to build Rich Internet Applications. You can download the Community Edition (it was made available yesterday) and try it out, and even use it for noncommercial uses. It’s the best JS framework I’ve seen. My single problem with it: it does not, at this time, supports Safari. They are still working on it.

So to summarize, we have some great tools to build webapps. Ruby on rails is getting a lot of press, but fear not, my fellow camel-loving friends, Perl is in good shape with Catalyst.

Ruby on rails seems to have an edge on AJAX stuff, but I don’t think it will last.

I still think that Ruby is a better, cleaner language than Perl5, and that might make a difference for some of you, but I’m an old perl guy, so I’ll be using a lot of Catalyst in the months to come.

Now, I need to find a ActiveRecord-type of framework for Perl.