Dancer::Tutorial(3pm)
π NAME π
okay bestie, say hi to Dancer::Tutorial β An example to get you dancing. it's from the User Contributed Perl Documentation, so you know it's got that community-made flavor. π
β¨ VERSION β¨
version 1.3522 β fresh-ish, slay.
π¦ What is Dancer? π¦
so Dancer is a "micro" web framework. it's modeled after a Ruby framework called Sinatra (http://www.sinatrarb.com), and the whole vibe is: you build web apps by making a list of HTTP verbs, URLs (these are called routes), and methods that handle that exact type of traffic hitting that exact URL.
use Dancer;
get '/' => sub {
return 'Hello World!';
};
start;
look at that. one single HTTP verb "GET", then the root URL "/", then an anonymous subroutine that returns the string "Hello World!". no cap. if you actually ran this, it would display "Hello World!" the moment you pointed your browser at http://localhost:3000. iconic behavior honestly. β¨
πΈ How about a little more involved example? πΈ
literally the entire reason this tutorial exists, bestie. while the author was investigating some Python web frameworks like Flask (http://flask.pocoo.org/) and Bottle (http://bottle.paws.de/docs/dev/index.html), they were lowkey obsessed with how those docs explained, step by step, how to build an example application that was a little more involved than a trivial example.
so they took the Flaskr sample application (https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/) as inspiration (their words: "OK, shamelessly plagiarised") and translated that whole application over to the Dancer framework, purely so they could understand how Dancer works better. (they're learning it too! π relatable.)
and that's how "Dancr" was born. π
Dancr is a simple "micro" blog that uses the SQLite (http://www.sqlite.org) database engine, just for simplicity's sake. π
π Required perl modules π
obviously you need Dancer. you also need Template Toolkit, File::Slurp, and DBD::SQLite. all of these can be installed using your CPAN client, like:
cpan Dancer Template File::Slurp DBD::SQLite
π The database π
we're not gonna spend a lot of time on the database, because ngl it's not really the point of this particular tutorial.
create the database by running the following from the shell:
$ cat - | sqlite3 database
create table if not exists entries (
id integer primary key autoincrement,
title string not null,
text string not null
);
^D
the above creates a single table with three columns: id, title, and text. the id field is the primary key, and it will automatically get an ID assigned by the database engine whenever a row gets inserted. the database just hands it to you. king behavior. π
we want our application to initialize the database automatically for us when we start it, so open your favorite text editor (http://www.vim.org) and create a file called dancr.pl. we're going to put the following subroutines in that file:
sub connect_db {
my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database')) or
die $DBI::errstr;
return $dbh;
}
sub init_db {
my $db = connect_db();
my $schema = read_file('./schema.sql');
$db->do($schema) or die $db->errstr;
}
nothing too fancy in here, hopefully. standard DBI, except for that setting('database') thing β more on that in a bit. for now just assume that expression evaluates to the file location for the database file.
note: you may wanna 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. π«Ά
π Our first route handler π
let's tackle our first route handler now β the one for the root URL '/'. it looks like this:
get '/' => sub {
my $db = connect_db();
my $sql = 'select id, title, text from entries order by id desc';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute or die $sth->errstr;
template 'show_entries.tt', {
'msg' => get_flash(),
'add_entry_url' => uri_for('/add'),
'entries' => $sth->fetchall_hashref('id'),
};
};
as you can see, the handler is created by specifying the HTTP verb 'get', then the URL to match, '/', and finally a subroutine to do something once those conditions have been satisfied. something you might not notice right away: that semicolon at the end of the route handler. since the subroutine actually is a coderef, it requires a semicolon. don't forget her. π
let's take a closer look at the subroutine. the first few lines are standard DBI. the only new concept that's part of Dancer itself 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 whole 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. π
so 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 whenever an event happens β like a new entry being posted, or the user logging in or out. it's called a "flash" message because we only want to display it one time, not every single time the / URL is rendered.
the "uri_for" directive tells Dancer to provide a URI for that specific route β in this case, it's 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. even though 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"?) β but at that point we'd have to go through our application and the templates and update all the URLs, and just 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. main character flexibility energy. π
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.
so what does the show_entries.tt template look like? this:
<% IF session.logged_in %>
<form action="<% add_entry_url %>" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
<% END %>
<ul class=entries>
<% IF entries.size %>
<% FOREACH id IN entries.keys.nsort %>
<li><h2><% entries.$id.title %></h2><% entries.$id.text %>
<% END %>
<% ELSE %>
<li><em>Unbelievable. No entries here so far</em>
<% END %>
</ul>
again, since this isn't a tutorial specifically about Template Toolkit, we're gonna gloss over the syntax here and just point out the section that starts with "<ul class=entries>" β that's the section where the database query results get displayed. you can also see, at the very top, some discussion about a session β more on that soon. π
π Other HTTP verbs π
there are 8 defined HTTP verbs defined in RFC 2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9): OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT. of these, the majority of web applications focus on the verbs that closely map to the CRUD (Create, Retrieve, Update, Delete) operations that most database-driven applications need to implement. periodt.
in addition, the "PATCH" verb was defined in RFC5789 (http://tools.ietf.org/html/rfc5789), and it's intended as a "partial PUT" β sending just the changes required to the entity in question. how that would be handled is down to your app; it will vary depending on the type of entity in question and the serialization in use. so, y'know, your call bestie. π¦
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.
post '/add' => sub {
if ( not session('logged_in') ) {
send_error("Not logged in", 401);
}
my $db = connect_db();
my $sql = 'insert into entries (title, text) values (?, ?)';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute(params->{'title'}, params->{'text'}) or die $sth->errstr;
# note: 'flash' keyword imported by Dancer::Plugin::FlashMessage,
# not part of Dancer core
flash message => 'New entry posted!';
redirect '/';
};
as before, the HTTP verb begins the handler, followed by the route, then a subroutine to do something β in this case, it will insert a new entry into the database.
the first check in the subroutine is to make sure the user sending the data is logged in. if not, the application sends back an error and stops processing. otherwise, we've got 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.) π this is not a drill, bestie, do not string-concatenate your user input into SQL, that's how you get owned.
here we're using the "params" convenience method to pull in the parameters of the current HTTP request. (you can see the 'title' and 'text' form parameters in the show_entries.tt template above.) those values get inserted into the database, then we set a flash message for the user and redirect her back to the root URL. β¨
and okay so that's the setup and the routes, but bestie we are so not done β there's still the whole login situation, and the templates, andβ
<!-- part 1 ends mid-moment on purpose, more parts follow -->
π Logins and sessions π
okay so Dancer literally ships with a cute little in-memory session manager right out of the box β no cap. It also supports a whole bunch of other session engines, like YAML, memcached, browser cookies and friends. For this app though, we're keeping it simple and sticking with the in-memory model, which is chef's kiss for development and tutorialsβ¦ but bestie, listen: it will not persist across server restarts, and it does not scale very well in "real world" production scenarios. Noted. Moving on.
π Configuration options
To actually use sessions in our application, we gotta tell Dancer to activate the session handler and initialize a session manager. So we drop some configuration directives toward the top of our dancr.pl file. But there's more we wanna set than just the session engine.
set 'session' => 'Simple';
set 'template' => 'template_toolkit';
set 'logger' => 'console';
set 'log' => 'debug';
set 'show_errors' => 1;
set 'startup_info' => 1;
set 'warnings' => 1;
set 'database' => database;
Hopefully these are fairly self-explanatory, but let's vibe-check them anyway: we want the Simple session engine, the Template Toolkit template engine, logging on (at the 'debug' level, with output going 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'd want to put these configuration options into a YAML file, but for this tutorial we're keeping 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'll want a more sophisticated user authentication scheme in any sort of non-tutorial application, but this is good enough for our purposes.
πͺ Logging in
Now that Dancr is configured to handle sessions, let's take a look at the URL handler for the /login route.
any ['get', 'post'] => '/login' => sub {
my $err;
if ( request->method() eq "POST" ) {
# process form input
if ( params->{'username'} ne setting('username') ) {
$err = "Invalid username";
}
elsif ( params->{'password'} ne setting('password') ) {
$err = "Invalid password";
}
else {
session 'logged_in' => true;
set_flash('You are logged in.');
return redirect '/';
}
}
# display login form
template 'login.tt', {
'err' => $err,
};
};
This is the first handler that 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 back 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.
<h2>Login</h2>
<% IF err %><p class=error><strong>Error:</strong> <% err %><% END %>
<form action="<% login_url %>" method=post>
<dl>
<dt>Username:
<dd><input type=text name=username>
<dt>Password:
<dd><input type=password name=password>
<dd><input type=submit value=Login>
</dl>
</form>
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, bestie, I promise.
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 a false() convenience method, which is what we're using here. After that, it's another flash message and back to the root URL handler π
π Logging out
And finally, we need a way to clear our user's session with the customary logout procedure.
get '/logout' => sub {
session->destroy;
set_flash('You are logged out.');
redirect '/';
};
session->destroy; is Dancer's way to remove a stored session. We notify the user she is logged out and route her back to the root URL once again. Iconic exit. π
π Layout and static files π
We still have a missing puzzle piece or two. First: how can we use Dancer to serve our CSS stylesheet? Second: where are flash messages displayed? Third: what about the before_template_render hook?
π Serving static files
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. Fr fr, no mystery there.
π¨ Layouts
I mentioned near the beginning of this tutorial that it's 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.
For this web application, the layout template looks like this.
<!doctype html>
<html>
<head>
<title>Dancr</title>
<link rel=stylesheet type=text/css href="<% css_url %>">
</head>
<body>
<div class=page>
<h1>Dancr</h1>
<div class=metanav>
<% IF not session.logged_in %>
<a href="<% login_url %>">log in</a>
<% ELSE %>
<a href="<% logout_url %>">log out</a>
<% END %>
</div>
<% IF msg %>
<div class=flash> <% msg %> </div>
<% END %>
<% content %>
</div>
</body>
</html>
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).
But what about all those other *_url template parameters?? π
πͺ Using before_template_render
Dancer has various hooks (http://en.wikipedia.org/wiki/Hooking) 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 one, where there are values which are re-used consistently across all (or most) templates. It 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. Obsessed with the DRYness π
hook 'before_template_render' => sub {
my $tokens = shift;
$tokens->{'css_url'} = request->base . 'css/style.css';
$tokens->{'login_url'} = uri_for('/login');
$tokens->{'logout_url'} = uri_for('/logout');
};
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. Slay.
π§© Putting it all together π§©
The complete tutorial code is available on GitHub:
https://github.com/PerlDancer/dancer-tutorial
Assuming you have Git installed, you can clone the code:
git clone git://github.com/PerlDancer/dancer-tutorial.git
... then run dancer.pl. That's it, bestie, you're cooking π
π Advanced route moves π
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. Main character routing energy, honestly β¨
π Happy dancing! π
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 platform.
Happy dancing! ππΊ
π SEE ALSO
http://perldancer.orghttps://github.com/PerlDancer/DancerDancer::Plugin
π COPYRIGHT AND LICENSE π
okay bestie, legal era incoming, but it's actually important so stay with me π«Ά
Copyright (C) 2010 by Mark R. Allen.
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.
β οΈ and now the loud part, no cap: 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.
likeβ¦ they're saying "we gave u this for free, pls don't sue us" and honestly? valid π
π AUTHOR π
Dancer Core Developers
the whole crew, the icons, the collective π
π COPYRIGHT AND LICENSE π
(yes bestie, another one β the main module's own license, separate from the tutorial text)
This software is copyright (c) 2010 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie!! π you made it all the way to the bottom, which is giving main character energy fr fr. go build something iconic with Dancer, and remember: this was perl v5.40.1, dated 2026-05-05, from Dancer::Tutorial(3pm) β so if ur reading a printed copy, that's the vintage π
now go slay β¨π¦πΈ
π¦ where this page comes from
- ubuntu stonking: libdancer-perl 1.3522-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms