Few weeks ago I had to fix some perl scripts. Actually make some HTTP requests, etc.
That was actually my first contact with the language. Never tried before! How can it be? Well, I ended up finding an amazing community and ecosystem (mostly covered by CPAN).
By ecosystem, I mean libs and tools out there. Couldn’t be different since it was created in 1987 (I was just a kid)!
So, I needed to develop something simple yet powerful enough to be further extended with new features. But the main requirement was: DO IT RIGHT NOW! Ok, I could have chosen Grails (my daily job), but know what? I even had time for JVM startup time. Same applies to anything on JVM, even clojure web stuff. I could also have chosen python with Flask and would be great. But what I was going to add to my toolset?
Enter Perl Dancer
After a quick research on the internet I found at least 3 trusted options:
- Catalyst - No, I wanted something simpler than that. Catalyst looks awesome, and I will try it at some point
- Mojolicious - Nice option. Very simple. But few plugins available if compared to the one I picked up
- Dancer - YES! It is super compact, and the community around it has created tons of great plugins: Dancer::Plugin::Mongo, Dancer::Plugin::REST and Dancer::Plugin::FlashMessage just to mention a few.
That is it, assemble plugins as you want and you are done.
Another nice point is the project folder structure created by Dancer. If you are used to frameworks like Grails and Django, you’ll like it. And not only that, Dancer comes by default with per-environment configuration (yaml) files, and a general config file where you configure, plugins, powerful templates, http filters, etc.
I can’t open that much of the code I wrote in few nights. But I can guarantee it was funny and quick to write. A simple Http request handler is:
It is simple like that. You call the get
providing the request path (notice the :wok_id
path variable) and then your perl routine that handles the request. mconfigs
is a simple routine that wraps the access to a MongoDB collection and a query.
Most web frameworks today offer this Sinatra approach, where you define routes and functions to handle them.
The template system is flexible and very powerful. Just embbed your html/css/js Twitter Bootstrap stuff and you have beauty. No time wasted with style cascading, user interface layout, etc.
MongoDB and keep it fast
As I said: DO IT RIGHT NOW! So, set up a new database machine? Create schemas? No way! Get a ready to go mongo instance at (mongoHQ)[http://www.mongohq.com/home] and start coding right away.
The good part of it is no set up time, no administration time, and you can grow it as you go. Well, mongoHQ here was actually used for development and I don’t intend to keep it once moving to production
The benefits of mongo itself you can find around. What I want to highlight is the aggregation framework in conjuntion with perl. The perl hash syntax perfectly fits mongo documents:
Using the aggregation framework you can easily break through any limitations when working without SQL statements with incredible performance.
Deploying for real
Well, your dancer application is able to process one request at time if you simply run it with perl yourap.pl
. I need to handle much more than one request at a time. There are plenty options like ones available for ruby (unicorn or passenger). I went for uWSGI inspite of many good options like Twiggy or Startman.
uWSGI provides a robust infrastructure and supports perl as well. But not all its features are available for perl like the queue framework and caching stuff. The speed of execution may compensate in most cases.
Wrapping Up
I’m passionate about clojure, and this blog won’t change because this quick perl affair. But sincerely speaking, I haven’t met any clojure equivalent for web yet. Fast to start, fast to develop, fast to test and fast to deploy. Plus strong community, plugins, etc.
I’m not expert enough in perl, but it now has a nice place among my tools. I can imagne myself with front end perl stuff and crazy distributed async clojure in the back end.