package Dancer2::Manual::Migration; # ABSTRACT: Migrating from Dancer to Dancer2 use strict; use warnings; 1; __END__ =pod =encoding UTF-8 =head1 NAME Dancer2::Manual::Migration - Migrating from Dancer to Dancer2 =head1 VERSION version 1.1.0 =head1 Migration from Dancer 1 to Dancer2 This document covers some changes that users will need to be aware of while upgrading from L (version 1) to L. =head2 Launcher script The default launcher script F in L looked like this: #!/usr/bin/env perl use Dancer; use MyApp; dance; In L it is available as F and looks like this: #!/usr/bin/env perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use MyApp; MyApp->to_app; So you need to remove the C part, replace the C command by C<< MyApp->to_app; >> (where MyApp is the name of your application), and add the following lines: use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; There is a L article L<< covering the C keyword|http://advent.perldancer.org/2014/9 >> and its usage. =head2 Configuration You specify a different location to the directory used for serving static (public) content by setting the C option. In that case, you have to set C option also. =head2 Apps 1. In L, each module is a B with its own namespace and variables. You can set the application name in each of your L 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: C: package MyApp; use Dancer2; use MyApp::Admin hook before => sub { var db => 'Users'; }; get '/' => sub {...}; 1; C: package MyApp::Admin; use Dancer2 appname => 'MyApp'; # use a lexical prefix so we don't override it globally prefix '/admin' => sub { get '/' => sub {...}; }; 1; Without the appname directive, C would not have access to variable C. In fact, when accessing C, the before hook would not be executed. See L for details. 2. To speed up an app in Dancer2, install the recommended modules listed in the L section. =head2 Request The request object (L) is now deferring much of its code to L to be consistent with the known interface to L requests. Currently the following attributes pass directly to L: C
, C, C, C, C, C, C, C, C, C, C, C, and C. If previous attributes returned I for no value beforehand, they will return whatever L 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 || '', ); =head2 Plugins: plugin_setting C returns the configuration of the plugin. It can only be called in C or C. =head2 Routes L requires all routes defined via a string to begin with a leading slash C. For example: get '0' => sub { return "not gonna fly"; }; would return an error. The correct way to write this would be to use C =head2 Route parameters The C 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 C to retrieve parameter values from the route matching. Please refer to L for more information. =head2 Tests Dancer2 recommends the use of L. 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{Test2}, 'Correct title' ); Other modules that could be used for testing are: =over 4 =item * L =item * L =back =head3 Logs The C in the Logger role (L) is now C. C can no longer be used, as with L. Instead, L 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; { 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' } ]; }; =head2 Exports: Tags The following tags are not needed in L: use Dancer2 qw(:syntax); use Dancer2 qw(:tests); use Dancer2 qw(:script); The C command should be used instead. It provides a development server and reads the configuration options in your command line utilities. =head2 Engines =over 4 =item * Engines receive a logging callback Engines now receive a logging callback named C. 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. =item * 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 default. One side-effect of this is that C will call L's C function with decoded input. =back =head3 Templating engine changes Whereas in Dancer1, the following were equivalent for Template::Toolkit: template 'foo/bar' template '/foo/bar' In Dancer2, when using L, the version with the leading slash will try to locate C relative to your filesystem root, not relative to your Dancer application directory. The L engine is unchanged in this respect. Whereas in Dancer1, template engines have the methods: $template_engine->view('foo.tt') $template_engine->view_exists('foo.tt') In Dancer2, you should instead write: $template_engine->view_pathname('foo.tt') $template_engine->pathname_exists($full_path) You may not need these unless you are writing a templating engine. =head3 Serializers You no longer need to implement the C method. It is simply unnecessary. =head3 Sessions Now the L session engine is turned on by default, unless you specify a different one. =head2 Configuration =head3 C You cannot set the public directory with C now. Instead you will need to call C: # before setting( 'public_dir', 'new_path/' ); # after config->{'public_dir'} = 'new_path'; =head3 warnings The C configuration option, along with the environment variable C, have been removed and have no effect whatsoever. They were added when someone requested to be able to load Dancer without the L pragma, which it adds, just like L, L, 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 L turned it is very recommended. =head3 server_tokens The configuration C has been introduced in the reverse (but more sensible, and Plack-compatible) form as C. C changed to C. =head3 engines 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 following: engines: template_toolkit: 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 extra level of depth. Under the C key there is a C