Wednesday, June 30, 2010

Light Mojolicious::Lite Experience

I've been playing with Mojolicious::Lite for the past couple of weeks. Why? Well, I had started a small project which records public data from a MMORPG that I frequently play (don't ask which one... it is no longer worth playing or even giving a free mention to) and needed a pretty front-end that myself and some friends could use to view the data.

Choices, Choices, Choices

The initial code I had written for the project was written in Python, which was two simple scripts; one that ran in a cron job once per day to retrieve the data as XML and one that analysed the data and outputted an ugly, little summary. I had intended to store some of the raw data in a database and wanted a web front-end to display it all and allow my non-technical friends to view and/or export the data as Excel spreadsheets.

Since this was a hobby project and I like to learn new libraries/frameworks for hobby projects, I had two decisions to make:

  • Which library for database access?
  • Which web-framework?

I was itching to use DBIx::Class for something, especially after using Class::DBI at work and hearing about the wonderful things DBIx::Class has that Class::DBI doesn't. But that's a Perl library, so I rewrote the Python script that ran in a cron job in Perl, so I could later hook DBIx::Class into it.

Deciding on the web-framework was next. A couple of years ago at university I had used Sinatra in a Ruby project and loved it and since then I have had a thing for web micro-frameworks. Python has Flask, which I always had in the back of my mind, and I like the way it uses decorators for the routing. Perl, luckily, has Mojolicious::Lite. I realise now that Dancer exists, however I found Mojo first (sorry, Dancer devs).

What I Like

The usual good things about web micro-frameworks: Rapid development.

The mechanism that Mojolicious::Lite uses for routing is very nice, feels very Perl-ish and allows for very compact code.

What I Don't Like

Errors in templates can result in cryptic error messages. For example:

Scalar found where operator expected at (eval 488) line 25, near "} $_M"
        (Missing operator before  $_M?)
And:
Template error in "rank.html.ep": syntax error at template line 2, near ";
$_M "
syntax error at (eval 488) line 25, near "} $_M "
syntax error at (eval 488) line 29, near "} }"
1: <% my $server_table = { %>
2:      <% my ($ranks, $server, $align) = @_; %>
3:
4:      <div class="rank_list <%= $align %>">

What caused the errors? The following two lines of code were:

<% my $server_table = { %>
...
<% } %>

When they should have been:

<% my $server_table = {%>
...
<%} %>

So a couple of spaces in the template produced those error messages. Not terribly obvious, unfortunately.

In the end...

A cryptic error message from a template error is far out-weighed by the things I like, and HTML::Mason's error messages have caused me much bigger headaches. Most of the time, any personal projects that I have only ever, if at all, need a super basic web interface, and I like to get my web applications up and functional as quickly as possible. Mojolicious::Lite let me do exactly that for this project and I intend on using it again in future projects.