Provided by: libdancer2-perl_0.207000+dfsg-1_all bug


       Dancer2::Manual::Migration - Migrating from Dancer to Dancer2


       version 0.207000

Migration from Dancer 1 to Dancer2

       This document covers some changes that users will need to be aware of while upgrading from
       Dancer (version 1) to Dancer2.

   Launcher script
       The default launcher script bin/ in Dancer looked like this:

           #!/usr/bin/env perl
           use Dancer;
           use MyApp;

       In Dancer2 it is available as bin/app.psgi and looks like this:

           #!/usr/bin/env perl

           use strict;
           use warnings;
           use FindBin;
           use lib "$FindBin::Bin/../lib";

           use MyApp;

       So you need to remove the "use Dancer;" part, replace the "dance;" command by
       "MyApp->to_app;" (where MyApp is the name of your application), and add the following

           use strict;
           use warnings;
           use FindBin;
           use lib "$FindBin::Bin/../lib";

       There is a Dancer Advent Calendar <> article covering the
       "to_app" keyword <> and its usage.

       You specify a different location to the directory used for serving static (public) content
       by setting the "public_dir" option. In that case, you have to set "static_handler" option

       1. In Dancer2, each module is a separate application with its own namespace and variables.
       You can set the application name in each of your Dancer2 application modules. Different
       modules can be tied into the same app by setting the application name to the same value.

       For example, to set the appname directive explicitly:


           package MyApp;
           use Dancer2;
           use MyApp::Admin

           hook before => sub {
               var db => 'Users';

           get '/' => sub {...};



           package MyApp::Admin;
           use Dancer2 appname => 'MyApp';

           # use a lexical prefix so we don't override it globally
           prefix '/admin' => sub {
               get '/' => sub {...};


       Without the appname directive, "MyApp::Admin" would not have access to variable "db". In
       fact, when accessing "/admin", the before hook would not be executed.

       See Dancer2::Cookbook <
       feature-to-split-your-application> for details.

       2. To speed up an app in Dancer2, install the recommended modules listed in the
       "Performance Improvements" in Dancer2::Manual::Deployment section.

       The request object (Dancer2::Core::Request) is now deferring much of its code to
       Plack::Request to be consistent with the known interface to PSGI requests.

       Currently the following attributes pass directly to Plack::Request:

       "address", "remote_host", "protocol", "port", "method", "user", "request_uri",
       "script_name", "content_length", "content_type", "content_encoding", "referer", and

       If previous attributes returned undef for no value beforehand, they will return whatever
       Plack::Request defines now, which just might be an empty list.

       For example:

           my %data = (
               referer    => request->referer,
               user_agent => request->user_agent,

       should be replaced by:

           my %data = (
               referer    => request->referer    || '',
               user_agent => request->user_agent || '',

   Plugins: plugin_setting
       "plugin_setting" returns the configuration of the plugin. It can only be called in
       "register" or "on_plugin_import".

       Dancer2 requires all routes defined via a string to begin with a leading slash "/".

       For example:

           get '0' => sub {
               return "not gonna fly";

       would return an error. The correct way to write this would be to use "get '/0'"

   Route parameters
       The "params" keyword which provides merged parameters used to allow body parameters to
       override route parameters. Now route parameters take precedence over query parameters and
       body parameters.

       We have introduced "route_parameters" to retrieve parameter values from the route
       matching. Please refer to Dancer2::Manual for more information.

       Dancer2 recommends the use of Plack::Test.

       For example:

           use strict;
           use warnings;
           use Test::More tests => 2;
           use Plack::Test;
           use HTTP::Request::Common;

               package App::Test; # or whatever you want to call it
               get '/' => sub { template 'index' };

           my $test = Plack::Test->create( App::Test->to_app );
           my $res  = $test->request( GET '/' );

           ok( $res->is_success, '[GET /] Successful' );
           like( $res->content, qr{<title>Test2</title>}, 'Correct title' );

       Other modules that could be used for testing are:

       ·   Test::TCP

       ·   Test::WWW::Mechanize::PSGI


       The "logger_format" in the Logger role (Dancer2::Core::Role::Logger) is now "log_format".

       "read_logs" can no longer be used, as with Dancer2::Test. Instead,
       Dancer2::Logger::Capture could be used for testing, to capture all logs to an object.

       For example:

           use strict;
           use warnings;
           use Test::More import => ['!pass'];
           use Plack::Test;
           use HTTP::Request::Common;
           use Ref::Util qw<is_coderef>;

               package App;
               use Dancer2;

               set log       => 'debug';
               set logger    => 'capture';

               get '/' => sub {
                   debug 'this is my debug message';
                   return 1;

           my $app = Dancer2->psgi_app;
           ok( is_coderef($app), 'Got app' );

           test_psgi $app, sub {
               my $cb = shift;

               my $res = $cb->( GET '/' );
               is $res->code, 200;

               my $trap = App->dancer_app->logger_engine->trapper;

               is_deeply $trap->read, [
                   { level => 'debug', message => 'this is my debug message' }

   Exports: Tags
       The following tags are not needed in Dancer2:

        use Dancer2 qw(:syntax);
        use Dancer2 qw(:tests);
        use Dancer2 qw(:script);

       The "plackup" command should be used instead. It provides a development server and reads
       the configuration options in your command line utilities.

       ·   Engines receive a logging callback

           Engines now receive a logging callback named "log_cb". Engines can use it to log
           anything in run-time, without having to worry about what logging engine is used.

           This is provided as a callback because the logger might be changed in run-time and we
           want engines to be able to always reach the current one without having a reference
           back to the core application object.

           The logger engine doesn't have the attribute since it is the logger itself.

       ·   Engines handle encoding consistently

           All engines are now expected to handle encoding on their own. User code is expected to
           be in internal Perl representation.

           Therefore, all serializers, for example, should deserialize to the Perl
           representation. Templates, in turn, encode to UTF-8 if requested by the user, or by

           One side-effect of this is that "from_yaml" will call YAML's "Load" function with
           decoded input.

       Templating engine changes

       Whereas in Dancer1, the following were equivalent for Template::Toolkit:

           template 'foo/bar'
           template '/foo/bar'

       In Dancer2, when using Dancer2::Template::TemplateToolkit, the version with the leading
       slash will try to locate "/foo/bar" relative to your filesystem root, not relative to your
       Dancer application directory.

       The Dancer2::Template::Simple engine is unchanged in this respect.

       Whereas in Dancer1, template engines have the methods:


       In Dancer2, you should instead write:


       You may not need these unless you are writing a templating engine.


       You no longer need to implement the "loaded" method. It is simply unnecessary.


       Now the Simple session engine is turned on by default, unless you specify a different one.


       You cannot set the public directory with "setting" now. Instead you will need to call

           # before
           setting( 'public_dir', 'new_path/' );

           # after
           config->{'public_dir'} = 'new_path';


       The "warnings" configuration option, along with the environment variable
       "DANCER_WARNINGS", have been removed and have no effect whatsoever.

       They were added when someone requested to be able to load Dancer without the warnings
       pragma, which it adds, just like Moose, Moo, and other modules provide.

       If you want this to happen now (which you probably shouldn't be doing), you can always
       control it lexically:

           use Dancer2;
           no warnings;

       You can also use Dancer2 within a narrower scope:

           { use Dancer2 }
           use strict;
           # warnings are not turned on

       However, having warnings turned it is very recommended.


       The configuration "server_tokens" has been introduced in the reverse (but more sensible,
       and Plack-compatible) form as "no_server_tokens".



       If you want to use Template::Toolkit instead of the built-in simple templating engine you
       used to enable the following line in the config.yml file.

           template: "template_toolkit"

       That was enough to get started. The start_tag and end_tag it used were the same as in the
       simple template <% and %> respectively.

       If you wanted to further customize the Template::Toolkit you could also enable or add the

                encoding:  'utf8'
                start_tag: '[%'
                end_tag:   '%]'

       In Dancer 2 you can also enable Template::Toolkit with the same configuration option:

           template: "template_toolkit"

       But the default start_tag and end_tag are now [% and %], so if you used the default in
       Dancer 1 now you will have to explicitly change the start_tag and end_tag values.  The
       configuration also got an extral level of depth. Under the "engine" key there is a
       "template" key and the "template_toolkit" key comes below that. As in this example:

                 start_tag: '<%'
                 end_tag:   '%>'

       In a nutshell, if you used to have

           template: "template_toolkit"

       You need to replace it with

           template: "template_toolkit"
                 start_tag: '<%'
                 end_tag:   '%>'

       Session engine

       The session engine is configured in the "engine" section.

       ·   "session_name" changed to "cookie_name".

       ·   "session_domain" changed to "cookie_domain".

       ·   "session_expires" changed to "cookie_duration".

       ·   "session_secure" changed to "is_secure".

       ·   "session_is_http_only" changed to "is_http_only".

       Dancer2 also adds two attributes for session:

       ·   "cookie_path" The path of the cookie to create for storing the session key. Defaults
           to "/".

       ·   "session_duration" Duration in seconds before sessions should expire, regardless of
           cookie expiration. If set, then SessionFactories should use this to enforce a limit on
           session validity.

       See Dancer2::Core::Role::SessionFactory for more detailed documentation for these options,
       or the particular session engine for other supported options.

         session: Simple

                   cookie_name: dance.set
               cookie_duration: '24 hours'
                   is_secure: 1
               is_http_only: 1

       Plack Middleware

       In Dancer1 you could set up Plack Middleware using a "plack_middlewares" key in your
       "config.yml" file. Under Dancer2 you will instead need to invoke middleware using
       Plack::Builder, as demonstrated in Dancer2::Manual::Deployment.

       Calling Keywords Explicitly

       In Dancer1, keywords could be imported individually into a package:

           package MyApp;
           use Dancer qw< get post params session >;

           get '/foo' { ... };

       Any keywords you did't export could be called explicitly:

           package MyApp;
           use Dancer qw< get post params session >;
           use List::Util qw< any >;

           Dancer::any sub { ... };

       Dancer2's DSL is implemented differently. Keywords only exist in the namespace of the
       package which "use"s Dancer2, i.e. there is no "Dancer2::any", only e.g. "MyApp::any".

       If you only want individual keywords, you can write a shim as follows:

           package MyApp::DSL;
           use Dancer2 appname => 'MyApp';

           use Exporter qw< import >;

           our @EXPORT = qw< get post ... >

       Then in other packages:

           package MyApp;

           use MyApp::DSL qw< get post >;

           MyApp::DSL::any sub { ... };


       This keyword does not exist in Dancer2. However, the same information can be found in


       This keyword is no longer required. Dancer2 loads the environment automatically and will
       not reload any other environment when called with load. (It's a good thing.)


       This keyword doesn't exist in Dancer2.


       In Dancer a session was created and a cookie was sent just by rendering a page using the
       "template" function. In Dancer2 one needs to actually set a value in a session object
       using the "session" function in order to create the session and send the cookie.

       The session keyword has multiple states:

       ·   No arguments

           Without any arguments, the session keyword returns a Dancer2::Core::Session object,
           which has methods for read, write, and delete.

               my $session = session;
               $session->write( $key => $value );

       ·   Single argument (key)

           If a single argument is provided, it is treated as the key, and it will retrieve the
           value for it.

               my $value = session $key;

       ·   Two arguments (key, value)

           If two arguments are provided, they are treated as a key and a value, in which case
           the session will assign the value to the key.

               session $key => $value;

       ·   Two arguments (key, undef)

           If two arguments are provided, but the second is undef, the key will be deleted from
           the session.

               session $key => undef;

       In Dancer 1 it wasn't possible to delete a key, but in Dancer2 we can finally delete:

           # these two are equivalent
           session $key => undef;

           my $session = session;

       You can retrieve the whole session hash with the "data" method:


       To destroy a session, instead of writing:


       In Dancer2, we write:

           app->destroy_session if app->has_session

       If you make changes to the session in an "after" hook, your changes will not be written to
       storage, because writing sessions to storage also takes place in an (earlier) "after"


       Dancer Core Developers


       This software is copyright (c) 2018 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.