Skip to content

Commit d39cb05

Browse files
committed
Merge branch 'feature/migration'
2 parents 355380f + 627cf2f commit d39cb05

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(Sawyer X)
99

1010
[ DOCUMENTATION ]
11+
* Migration document! (Snigdha Dagar)
1112
* GH #667: Fix typo in cookbook pod. (Lindsey Beesley)
1213
* GH #649, #670: Document core logger. (simbabque)
1314
* GH #689: Git guide markdown fixes. (Paul Cochrane)

lib/Dancer2/Manual/Migration.pod

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package Dancer2::Manual::Migration;
2+
3+
use strict;
4+
use warnings;
5+
6+
1;
7+
8+
__END__
9+
10+
=pod
11+
12+
=head2 Migration from Dancer1 to Dancer2
13+
14+
This document covers some changes that users will need to be aware of
15+
while upgrading from L<Dancer> (version 1) to L<Dancer2>.
16+
17+
=head3 Apps
18+
19+
1. In L<Dancer2>, each module is a B<separate application> with its own
20+
namespace and variables. You can set the application name in each of your
21+
L<Dancer2> application modules. Different modules can be tied into the same
22+
app by setting the application name to the same value.
23+
24+
For example, to set the appname directive explicitly:
25+
26+
C<MyApp>:
27+
28+
package MyApp;
29+
use Dancer2;
30+
use MyApp::Admin
31+
32+
hook before => sub {
33+
var db => 'Users';
34+
};
35+
36+
get '/' => sub {...};
37+
38+
1;
39+
40+
C<MyApp::Admin>:
41+
42+
package MyApp::Admin;
43+
use Dancer2 appname => 'MyApp';
44+
45+
# use a lexical prefix so we don't override it globally
46+
prefix '/admin' => sub {
47+
get '/' => sub {...};
48+
};
49+
50+
1;
51+
52+
Without the appname directive, C<MyApp::Admin> would not have access
53+
to variable C<db>. In fact, when accessing C</admin>, the before hook would
54+
not be executed.
55+
56+
See L<Dancer2::Cookbook|https://metacpan.org/pod/Dancer2::Cookbook#Using-the-prefix-feature-to-split-your-application>
57+
for details.
58+
59+
2. The following modules can be used to speed up an app in Dancer2:
60+
61+
=over 4
62+
63+
=item * L<URL::Encode::XS>
64+
65+
=item * L<CGI::Deurl::XS>
66+
67+
=item * L<HTTP::Parser::XS>
68+
69+
=back
70+
71+
They would need to be installed separately. This is because L<Dancer2> does
72+
not incorporate any C code, but it can get C-code compiled as a module.
73+
Thus, these modules can be used for speed improvement provided:
74+
75+
=over 4
76+
77+
=item * You have access to a C interpreter
78+
79+
=item * You don't need to fatpack your application
80+
81+
=back
82+
83+
=head3 Plugins: plugin_setting
84+
85+
C<plugin_setting> returns the configuration of the plugin. It can no
86+
longer be called outside of C<register> or C<on_plugin_import>.
87+
88+
=head3 Routes
89+
90+
L<Dancer2> requires all routes defined via a string to begin with a leading
91+
slash C</>.
92+
93+
For example:
94+
95+
get '0' => sub {
96+
return "not gonna fly";
97+
};
98+
99+
would return an error. The correct way to write this would be to use
100+
C<get '/0'>
101+
102+
=head3 Tests
103+
104+
Dancer2 recommends the use of L<Plack::Test>.
105+
106+
For example:
107+
108+
use strict;
109+
use warnings;
110+
111+
use Test::More tests => 3;
112+
113+
use Plack::Test;
114+
use HTTP::Request::Common;
115+
116+
use Test2;
117+
{ package Test2; set apphandler => 'PSGI'; set log => 'error'; }
118+
119+
test_psgi( Test2::dance, sub {
120+
my $app = shift;
121+
122+
my $res = $app->( GET '/' );
123+
124+
ok $res->is_success;
125+
126+
is $res->code => 200, 'response status is 200 for /';
127+
128+
like $res->content => qr#<title>Test2</title>#, 'title is okay';
129+
} );
130+
131+
Other modules that could be used for testing are:
132+
133+
=over 4
134+
135+
=item * L<Test::TCP>
136+
137+
=item * L<Test::WWW::Mechanize::PSGI>
138+
139+
=back
140+
141+
=head4 Logs
142+
143+
C<read_logs> can no longer be used, as with L<Dancer2::Test>. Instead,
144+
L<Dancer2::Logger::Capture> could be used for testing, to capture all
145+
logs to an object.
146+
147+
For example:
148+
149+
use strict;
150+
use warnings;
151+
use Test::More import => ['!pass'];
152+
use Plack::Test;
153+
use HTTP::Request::Common;
154+
155+
{
156+
package App;
157+
use Dancer2;
158+
159+
set log => 'debug';
160+
set logger => 'capture';
161+
162+
get '/' => sub {
163+
debug 'this is my debug message';
164+
return 1;
165+
};
166+
}
167+
168+
my $app = Dancer2->psgi_app;
169+
is( ref $app, 'CODE', 'Got app' );
170+
171+
test_psgi $app, sub {
172+
my $cb = shift;
173+
174+
my $res = $cb->( GET '/' );
175+
is $res->code, 200;
176+
177+
my $trap = App->dancer_app->logger_engine->trapper;
178+
179+
is_deeply $trap->read, [
180+
{ level => 'debug', message => 'this is my debug message' }
181+
];
182+
};
183+
184+
=head3 Exports: Tags
185+
186+
The following tags are not needed in L<Dancer2>:
187+
188+
use Dancer2 qw(:syntax);
189+
use Dancer2 qw(:tests);
190+
use Dancer2 qw(:script);
191+
192+
The C<plackup> command should be used instead. It provides a development
193+
server and reads the configuration options in your command line utilities.

0 commit comments

Comments
 (0)