Command line dancing

Some of the changes Dancer2 has introduced were the removal of all import tags:

use Dancer ':syntax'; # use Dancer2;
use Dancer ':script'; # use Dancer2;
use Dancer ':tests';  # use Dancer2;

Now you simply call use Dancer2 for all of these. It will create an application and provide keywords. If you want to create a PSGI application from it, simply call to_app on your class. Otherwise, it doesn't get in the way.

But I need it

While the last paragraph actually summed up this entire article, it probably wasn't very clear.

A useful feature of going through Dancer itself is that it will read the configuration files, parse them, understand them, and even create objects lazily using them.

People often write scripts that have to then duplicate what Dancer does. It's quite understandable that people wish to be able to use Dancer in their scripts to have access to the configuration and the generated objects.

Reaching the config

By importing Dancer2 in your command line scripts, you have full access to the configuration using the imported keywords:

use Dancer2; # strict && warnings included

# this works:
my $appname = config->{'appname'};

# and so does this:
my $appname = app->name;

You shouldn't worry about becoming an app and starting a web server. Yes, as soon as it hits use Dancer2 it will create a Dancer2 application behind the scenes, and that app can be run at any given point in time by calling dance or the preferred method to_app on it, but unless you actually call those, this app will simply be created in the background.

You will still be able to access all the configuration from it without having to run it under a web environment.

Small example

One small example of wanting to access the configurations from a command line script would be to run some sort of import to or export from a database.

Being able to access the configuration file and having the classes that are automatically inflated from that configuration file is mighty useful.

use Dancer2
use Dancer2::Plugin::Database;

my $user_id = $ARGV[0] or die "$0 <USER ID>\n";
$user_id =~ /^[0-9]+$/ or die "User ID must be a positive number\n";

my $sth = database->prepare( 'select * from users where id = ?' );



If you want to create command line applications that use the Dancer2 structures, you can simply load Dancer2 and use the keywords it provides you.

What does this give you access to?

  • Dancer keywords

    config probably being the most helpful one, but template will also be available just like other engine-related keywords are - explained below under Engines.

  • Keywords created by plugins

    This means you can make use of any functionality they provide outside of a request.

  • Engines

    This means you can use the facilities your web application has when logging events (using debug, warning, and error), accessing or setting session information, serializing, and even rendering templates (using the template keyword).


This article has been written by Sawyer X for the Perl Dancer Advent Calendar 2014.


No copyright retained. Enjoy.

2014 // Sawyer X <>