AJAX on Perl

January 26th, 2007 by Alexander Mamchenkov

For the past week or so, I was playing a lot with web development and finally managed to put my hands on so popular these days AJAX technology. A lot of readings here and there gave me a more or less complete picture on how it works and I decided to try it with my favorite Perl web development framework Catalyst. Another couple of days coding and re-coding, I found out that all components that I used to use before can be easily integrated and work while using AJAX with almost no modifications.

First of all, the Perl modules for web development I usually use are as follows:

  • Catalyst - provides a very nice MVC framework and prevents from writing tons of code like events dispatching, connection of different components and so on.
  • Catalyst-Plugin-Session - provides a fully integrated into Catalyst session mechanism.
  • Class-DBI - an amazing database abstraction layer framework. I find this to be the best way of dealing with databases.
  • Class-DBI-mysql - a nice addong to Class-DBI which improves the work with databases even more (given MySQL is used as a DB back-end).
  • Template (or Template-Toolkit) - a very flexible and easy to use template system.
  • Class-DBI-FormBuilder - a module which allows to easily integrate CGI-FormBuilder with Class-DBI and provide many useful features while working with different forms.

This list of modules is normally more than enough to develop any web site. I used the whole combination for a while now and I think I won’t change it any time soon.

Now, in order be able to create an AJAX application, using the same set of tools, the only thing that has to be added is a Catalyst-Plugin-Prototype module, which will integrate a very famous AJAX framework Prototype into Catalyst. From now on, it is possible to do all those AJAX things at no headache.

I have also converted one of the web app to running on Catalyst to the AJAX technology by doing the next steps:

  1. Make sure templates do not include site header and footer if we are in AJAX mode, so that they can be used as a parts by them selfs. This is done because when using AJAX, the header and footer of the web site is retrieved only one and then remains at its place.
  2. Write JavaScript event handlers to do requests to the web server, retrieve needed components and replace parts of the web site. I have put everything in one JavaScript class called MyPage which is created when web site accessed and persists during the whole session.
  3. Put the action listeners like onClick, onLoad and others instead of normal links on navigation elements when in AJAX more.

Although I already feel quite comfortable with Catalyst and AJAX, there are still couple of things I will go more into:

  1. Tracking browser history and acting appropriately on back/forward/refresh requests
  2. Learn more about JavaScript
  3. Find out some Best Practice Uses of AJAX. The problem here is that most of the tutorials there on the web are very basic and simple, so it is easy to get a very basic idea, but hard to work with more complex things.

Posted in Technology | No Comments »