Just a quick update for now – I’ve managed to book myself up quite fully.

I’m due to do a lightning talk tonight at the Birmingham Linux User Group called “FastCGI: High Performance, High Availability”. I’ve only recently looked at FastCGI, but already I can see a number of interesting aspects about it. The LUG sometimes webcasts the meetings – if they’re doing it tonight, I’ll see if I can get a copy of the video to post here (and I’ll post the slides as well).

I’ve also agreed to do a talk next week at the Birmingham PerlMongers meeting, about how (and why) The Freecycle Network is using Perl. I guess I’d better start writing it then… :)

Yesterday, I had my first real attempt at using AJAX with Catalyst. I’ve heard good things about jQuery, and it seemed simple enough, so I went with that rather than any of the other frameworks. Making AJAX requests was pretty easy, and the DOM stuff to display the results was straightforward as well.

From the Catalyst side of things, this was new territory. Initially, I was planning on XML output because…well, because that’s what I’ve always done. JSON has always been a “yeah, that can be done as well” kind of thing, but with jQuery being able to wrap up AJAX (AJAJ?) responses into JavaScript objects, it seemed easier to try returning JSON rather than having to process XML documents on the client side.

This is where the cool bit comes in – Catalyst already has a View plugin for JSON, which handles (almost) everything for you. So, a quick

./myapp_create view JSON JSON

later, I had my JSON view. By default, the View outputs everything in the stash as JSON, which I wasn’t too keen on, so I made a quick config edit:

# Configure View::JSON
 expose_stash = json

There – only the data I put in $c->stash->{json} would now be output. It should be noted that you need to at least have an empty hash there, otherwise the View complains when rendering, but initialising your variables is always a good thing. :)

All that was left was the Catalyst action code:

sub ajax : Local Args(0) {
  my ($self,$c) = @_;

  # Make sure there are no render errors
  $c->stash->{json} = {};

  if ( $c->req->param('action') eq 'get_post_details' ) {
    my $post = new Post( $c->req->param('post_id') || 0 );

    $c->stash->{json} = {
      post_body => $post->getPostDescription(),
      post_subject => $post->getPostSubject(),

  # Turn the stash into a JSON object
  $c->forward( 'MyApp::View::JSON' );

And that’s all there is to it! I guess if I was going to do a lot of AJAX endpoints I’d probably have an action for each, rather than an if…else ladder, but this Works For Me (for now). Any unknown ‘action’ calls are silently ignored (with an empty object being passed back). Adding new endpoints, and additional data to endpoints, is now very familiar Perl code. I’m now one of the cool kids using JSON, and it didn’t hurt a bit. :)