Taking a spin with Dancer, the lightweight Perl web application framework

No readers like this yet.
Kids doing a line dance

Image by Florida Memory. Modified by Opensource.com. CC BY-SA 2.0.

Dancer is a lightweight web application framework for Perl, inspired by the Sinatra framework in Ruby. Dancer bills itself as simple and flexible, but powerful enough to run most any web application you can think up.

A bit of history

The first version of Dancer was developed by Alexis Sukrieh and some colleagues beginning in 2009. There wasn't anything similar in the Perl ecosystem at that time, and the community picked it up and ran with it.

Like most prototypes, according to Sukrieh, there were some design decisions that could have been improved upon. So in 2011, Dancer 2 was born—a ground-up rewrite with everything object-oriented, a consistent, high-quality API, and no problematic global variables to pass around. Most Perl programmers, given an opportunity, will tell you that having a lot of globals is a big Perl no-no. It works, mostly, but it presents a maintenance and scalability nightmare.

Given these improvements, the community has embraced Dancer 2 with just as much enthusiasm, and Dancer is now a viable framework for production environments.

What can you do with it

Dancer lets you define routes, URL paths in your web application, and then designate what happens when a user sends their web browser to that path. The software includes a minimalist web server for development use, and is compatible with most any web server you might want to run it on in deployment, as it is PSGI-compliant. You can also run it behind Apache as a CGI application.

Dancer has a robust plugin system, enabling pluggable modules to be added quickly to your application. Many plugins already exist for common use cases, including a variety of database systems, authentication, localization, session management, page templating, and more. The framework is lightweight enough that, for many of the plugins, it's largely syntactic sugar—you can introduce the same functionality without a plugin by simply using the module, and maybe having a line or two in a hook routine.

Here's a very basic Dancer application:


use Dancer2;

get '/' => sub {
  return 'Hello World!';
};

start;

If you were to run this in the built-in web server and point your web browser at http://localhost:3000, you'd get "Hello World!" on your screen, and nothing more. Any other URL would return a nicely-formatted "Not Found" page.

The routes support wildcard matching, so you can easily use Dancer to create RESTful applications, and there is a plugin to make using Ajax easier, as well.

Why Dancer?

I spoke with Sawyer X, one of the developers who has been heavily involved in the Dancer community for some years. I asked him why one might choose Dancer over any of the many other web frameworks for Perl. He said the question is similar to picking a programming language—it's about what works for you and the way you think. "Does declaring routes make sense to you? Does it feel comfortable?" he asked. If it does, then Dancer may be the framework for you. Dancer, he continues, is not heavily opinionated about the tools you use to build an application. It provides usable defaults, but there are lots of other possibilities. A simple template engine is included, for instance, but you can easily use Template Toolkit or other template engine, without drastically changing the application.

I've known Sawyer for about four years now, so I was unsurprised when he went on to say, "That's not my reason for working on Dancer. The reason I took a major role in Dancer was that I found a good home there; I found a good spot that made me feel welcomed and happy. That's why I'm in the Perl community."

Come out onto the floor!

The Dancer community welcomes new people—both creator and consumer. If you have a Perl web application that could use a simple but feature-rich framework, give Dancer a try. I've used it more than once for simple mock-ups and demos, and am working on some ideas for larger things using Dancer. If you'd like to see Dancer do new moves, your ideas and code are welcome.

Nooks
and
Crannies

This article is part of the Nooks and Crannies column by D Ruth Bavousett, which explores open source projects that you might not have heard of. Send column suggestions to open@opensource.com with Nooks and Crannies in the subject line and a link to the project's home page, or submit your own story.

User profile image.
Ruth Holloway has been a system administrator and software developer for a long, long time, getting her professional start on a VAX 11/780, way back when. She spent a lot of her career (so far) serving the technology needs of libraries, and has been a contributor since 2008 to the Koha open source library automation suite. Ruth is currently a Perl developer and project lead at Clearbuilt.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.