Dancer is a "micro" web framework which is modeled after a Ruby framework called Sinatra that constructs web applications by building a list of HTTP verbs,
URLs (called routes) and methods to handle that type of traffic to that specific URL.

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

This example shows a single HTTP verb "GET" followed by the root URL "/" and an anonymous subroutine which returns the string "Hello World!" If you were to run this example, it would display "Hello World!" when you point your browser at http://localhost:3000.

That's the reason I wrote this tutorial. While I was investigating some Python web frameworks like Flask or Bottle I enjoyed the way they explained step by step how to build an example application which was a little more involved that a trivial example.

Using the Flaskr sample application as my inspiration (OK, shamelessly plagiarised) I translated that application to the Dancer framework so I could better understand how Dancer worked. (I'm learning it too!)

So "Dancr" was born.

Dancr is a simple "micro" blog which uses the SQLite database engine for simplicity's sake.

Here we have a single table with three columns: id, title, and text. The 'id' field is the primary key and will automatically get an ID assigned by the database engine when a row is inserted.

We want our application to initialize the database automatically for us when we start it, so open your favorite text editor and create a file called 'dancr.pl'. We're going to put the following subroutines in that file:

Nothing too fancy in here, I hope. Standard DBI except for the setting('database') thing - more on that in a bit. For now, just assume that the expression evaluates to file location for the database file.

(Note that you may want to look at the Dancer::Plugin::Database module for an easy way to configure and manage database connections for your Dancer apps, but the above will suffice for this tutorial.)

As you can see, the handler is created by specifying the HTTP verb 'get' and the URL to match, '/' and finally a subroutine to do something once those conditions have been satisfied. Something you might not notice right away is the semicolon at the end of the route handler. Since the subroutine actually is a coderef, it requires a semicolon.

Let's take a closer look at the subroutine. The first few lines are standard DBI. The only new concept as part of Dancer is that template directive at the end of the handler. That tells Dancer to process the output through one of its templating engines. In this case, we're using Template Toolkit which offers a lot more flexibility than the simple default Dancer template engine.

Templates all go into the views/ directory. Optionally, you can create a "layout" template which provides a consistent look and feel for all of your views. We'll construct our own layout template cleverly named main.tt a little later in this tutorial.

What's going on with the hashref as the second argument to the template directive? Those are all of the parameters we want to pass into our template. We have a msg field which displays a message to the user when an event happens like a new entry is posted, or the user logs in or out. It's called a "flash" message because we only want to display it one time, not every time the / URL is rendered.

The uri_for directive tells Dancer to provide a URI for that specific route, in this case, it is the route to post a new entry into the database. You might ask why we don't simply hardcode the /add URI in our application or templates. The best reason not to do that is because it removes a layer of flexibility on where to "mount" the web application. Although the application is coded to use the root URL / it might be better in the future to locate it under its own URL route (maybe /dancr?) - at that point we'd have to go through our application and the templates and update the URLs and hope we didn't miss any of them. By using the uri_for Dancer method, we can easily load the application wherever we like and not have to modify the application at all.

Finally, the entries field contains a hashref with the results from our database query. Those results will be rendered in the template itself, so we just pass them in.

Again, since this isn't a tutorial specifically about Template Toolkit, I'm going to gloss over the syntax here and just point out the section which starts with <ul class=entries> - this is the section where the database query results are displayed. You can also see at the very top some discussion about a session - more on that soon.

In addition, the PATCH verb was defined in RFC5789, and is intended as a "partial PUT" - sending just the changes required to the entity in question. How this would be handled is down to your app, it will vary depending on the type of entity in question and the serialisation in use.

Dancer currently supports GET, PUT/PATCH, POST, DELETE, OPTIONS which map to Retrieve, Update, Create, Delete respectively. Let's take a look now at the /add route handler which handles a POST operation.

As before, the HTTP verb begins the handler, followed by the route, and a subroutine to do something - in this case, it will insert a new entry into the database.

The first check in the subroutine is the make sure the user sending the data is logged in. If not, the application sends back an error and stops processing. Otherwise, we have standard DBI stuff. Let me insert (heh, heh) a blatant plug here for always, always using parameterized INSERTs in your application SQL statements. It's the only way to be sure your application won't be vulnerable to SQL injection. (See http://www.bobby-tables.com for correct INSERT examples in multiple languages.) Here we're using the params convenience method to pull in the parameters in the current HTTP request. (You can see the 'title' and 'text' form parameters in the show_entries.tt template above.) Those values are inserted into the database, then we set a flash message for the user and redirect her back to the root URL.

It's worth mentioning that the "flash message" is not part of Dancer, but a part of this specific application.

Dancer comes with a simple in-memory session manager out of the box. It supports a bunch of other session engines including YAML, memcached, browser cookies and others. For this application we're going to stick with the in-memory model which works great for development and tutorials, but won't persist across server restarts or scale very well in "real world" production scenarios.

To use sessions in our application, we have to tell Dancer to activate the session handler and initialize a session manager. To do that, we add some configuration directives toward the top of our dancr.pl file. But there are more options than just the session engine we want to set.

Hopefully these are fairly self-explanatory. We want the Simple session engine, the Template Toolkit template engine, logging enabled (at the 'debug' level with output to the console instead of a file), we want to show errors to the web browser, log access attempts and log Dancer warnings (instead of silently ignoring them)

In a more sophisticated application you would want to put these configuration options into a YAML file, but for this tutorial, we're going to keep it simple. Dancer also supports the notion of application environments meaning you can create a configuration file for your development instance, and another config file for the production environment (with things like debugging and showing errors disabled perhaps.) Dancer also doesn't impose any limits on what parameters you can set using the set syntax. For this application we're going to embed our single username and password into the application itself.

set 'username' => 'admin';
set 'password' => 'password';

Hopefully no one will ever guess our clever password! Obviously, you will want a more sophisticated user authentication scheme in any sort of non-tutorial application but this is good enough for our purposes.

This is the first handler which accepts two different verb types, a GET for a human browsing to the URL and a POST for the browser to submit the user's input to the web application. Since we're handling two different verbs, we check to see what verb is in the request. If it's not a POST, we drop down to the template directive and display the login.tt template.

This is even simpler than our show_entries.tt template - but wait - there's a login_url template parameter and we're only passing in the err parameter. Where's the missing parameter? It's being generated and sent to the template in a before_template_render hook - we'll come back to that in a moment or two.

So the user fills out the login.tt template and submits it back to the /login route handler. We now check the user input against our application settings and if they're incorrect, we alert the user, otherwise the application starts a session and sets the logged_in session parameter to the true() value. Dancer exports both a true() and false() convenience method which we use here. After that, it's another flash message and back to the root URL handler.

In Dancer, static files should go into the public/ directory, but in the application be sure to omit the public/ element from the path. For example, the stylesheet for Dancr lives in dancr/public/css/style.css but is served from http://localhost:3000/css/style.css.

If you wanted to build a mostly static web site you could simply write route handlers like this one:

get '/' => sub {
send_file 'index.html';
};

where index.html would live in your public/ directory.

send_file does exactly what it says: it loads a static file, then sends the contents of that file to the user.

I mentioned near the beginning of this tutorial that it is possible to create a layout template. In Dancr, that layout is called main and it's set up by putting in a directive like this:

set layout => 'main';

near the top of your web application. What this tells Dancer's template engine is that it should look for a file called main.tt in dancr/views/layouts/ and insert the calls from the template directive into a template parameter called content.

Aha! You now see where the flash message msg parameter gets rendered. You can also see where the content from the specific route handlers is inserted (the fourth line from the bottom in the content template parameter.)

Dancer has various hooks which provide additional flexibility and power. The hooks available are documented in the documentation for the hook keyword; the one we're interested in here is before_template_render which provides a way to manipulate the template parameters before they're passed to the engine for processing.

Using this hook, we can generate and set the URIs for the /login and /logout route handlers and the URI for the stylesheet. This is handy for situations like this where there are values which are re-used consistently across all (or most) templates. This cuts down on code-duplication and makes your app easier to maintain over time since you only need to update the values in this one place instead of everywhere you render a template.

Here again I'm using uri_for instead of hardcoding the routes. This code block is executed before any of the templates are processed so that the template parameters have the appropriate values before being rendered.

There's a lot more to route matching than shown here. For example, you can match routes with regular expressions, or you can match pieces of a route like /hello/:name where the :name piece magically turns into a named parameter in your handler for manipulation.

I hope this effort has been helpful and interesting enough to get you exploring Dancer on your own. The framework is still under heavy development but it's definitely mature enough to use in a production project. Additionally, there are now a lot of great Dancer plugins which extend and enhance the capabilities of the the platform.

This is free software; you can redistribute it and/or modify it under the terms of either the Artistic License 2.0 or the GNU Public License version 2.

The CSS stylesheet is copied verbatim from the Flaskr example application and is subject to their license:

Copyright (c) 2010 by Armin Ronacher and contributors.

Some rights reserved.

Redistribution and use in source and binary forms of the software as well as documentation, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

The names of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.