1Dancer2::Tutorial(3)  User Contributed Perl Documentation Dancer2::Tutorial(3)
2
3
4

NAME

6       Dancer2::Tutorial - An example to get you dancing
7

VERSION

9       version 0.400000
10

Tutorial Overview

12       This tutorial is has three parts. Since they build on one another, each
13       part is meant to be gone through in sequential order.
14
15       Part I, the longest part of this tutorial, will focus on the basics of
16       Dancer2 development by building a simple yet functional blog app,
17       called "dancr", that you can use to impress your friends, mates, and
18       family.
19
20       In Part II, you'll learn about the preferred way to get your own web
21       apps up and running by using the "dancer2" utility. We will take the
22       script written in Part I and convert it into a proper Dancer2 app,
23       called "Dancr2", to help you gain an understanding of what the
24       "dancer2" utility does for you.
25
26       Finally, in Part III, we give you a taste of the power of plugins that
27       other developers have written and will show you how to modify the
28       "Dancr2" app to use a database plugin.
29
30       This tutorial assumes you have some familiarity with Perl and that you
31       know how to create and execute a Perl script on your computer. Some
32       experience with web development is also greatly helpful but not
33       entirely necessary. This tutorial is mostly geared toward developers
34       but website designers can get something out of it as well since the
35       basics of templating are covered plus it might be good for a designer
36       to have a decent idea of how Dancer2 works.
37

Part I: Let's Get Dancing!

39       Part I covers many of the basic concepts you'll need to know to lay a
40       good foundation for your future development work with Dancer2 by
41       building a simple micro-blogging app.
42
43   What is Dancer2?
44       Dancer2 is a micro-web framework, written in the Perl programming
45       language, and is modeled after a Ruby web application framework called
46       Sinatra <http://www.sinatrarb.com>.
47
48       When we say "micro" framework, we mean that Dancer2 aims to maximize
49       your freedom and control by getting out of your way. "Micro" doesn't
50       mean Dancer2 is only good for creating small apps. Instead, it means
51       that Dancer2's primary focus is on taking care of a lot of the boring,
52       technical details of your app for you and by creating an easy, clean
53       routing layer on top of your app's code. It also means you have almost
54       total control over the app's functionality and how you create and
55       present your content. You will not confined to someone else's approach
56       to creating a website or app.
57
58       With Dancer2, you can build anything from a specialized content
59       management system to providing a simple API for querying a database
60       over the web. But you don't have to reinvent the wheel, either. Dancer2
61       has hundreds of plugins that you can take advantage of. You can add
62       only the capabilities your app needs to keep complexity to a minimum.
63
64       As a framework, Dancer2 provides you with the tools and infrastructure
65       you can leverage to deliver content on the web quickly, easily and
66       securely. The tools, Dancer2 provides, called "keywords," are commands
67       that you use to build your app, access the data inside of it, and
68       deliver it on the internet in many different formats.
69
70       Dancer2's keywords provide what is called a Domain Specific Language
71       (DSL) designed specifically for the task of building apps. But don't
72       let the technical jargon scare you off. Things will become clearer in
73       our first code example which we will look at shortly.
74
75       Getting Dancer2 installed
76
77       First, we need to make sure you have Dancer2 installed. Typically, you
78       will do that with one of the following two commands:
79
80           cpan Dancer2  # requires the cpan command to be installed and configured
81           cpanm Dancer2 # requires you have cpanminus installed
82
83       If you aren't familiar with installing Perl modules on your machine,
84       you should read this guide <https://www.cpan.org/modules/INSTALL.html>.
85       You may also want to consult your OS's documentation or a knowledgeable
86       expert. And, of course, your search engine of choice is always there
87       for you, as well.
88
89       Your first Dancer2 "Hello World!" app
90
91       Now that you have Dancer2 installed, open up your favorite text editor
92       and copy and paste the following lines of Perl code into it and save it
93       to a file called "dancr.pl":
94
95           #!/usr/bin/env perl
96           use Dancer2;
97
98           get '/' => sub {
99               return 'Hello World!';
100           };
101
102           start;
103
104       If you make this script executable and run it, it will fire up a
105       simple, standalone web server that will display "Hello World!" when you
106       point your browser to <http://localhost:3000>. Cool!
107
108       Important note: We want to emphasize that writing a script file like
109       this with a "start" command is not how you would typically begin
110       writing a Dancer2 app. Part II of this tutorial will show you the
111       recommended approach using the "dancer2" utility. For now, we want to
112       stay focused on the fundamentals.
113
114       So, though our example app is very simple, there is a lot going on
115       under the hood when we invoke "use Dancer2;" in our first line of code.
116       We won't go into the gory details of how it all works. For now, it's
117       enough for you to know that the Dancer2 module infuses your script with
118       the ability to use Dancer2 keywords for building apps. Getting
119       comfortable with the concept of keywords is probably the most important
120       step you can take as a budding Dancer2 developer and this tutorial will
121       do its best to help foster your understanding of them.
122
123       The next line of code in our example (which spans three lines to make
124       it more readable) is the route handler. Let's examine this line
125       closely, because route handlers are at the core of how to build an app
126       with Dancer2.
127
128       The syntax of a Dancer2 "route handler" has three parts:
129
130       •   an http method or http verb; in this example, we use the "get"
131           keyword to tell Dancer2 that this route should apply to GET http
132           requests. "get" is the first of many keywords that Dancer2 provides
133           that we will cover in this tutorial.  Those familiar with web
134           development will know that a GET request is what we use to fetch
135           information from a website.
136
137       •   the route pattern; this is the bit of code that appears immediately
138           after our "get" keyword. In this example it is a forward slash
139           ("/"), wrapped in single quotes, and it represents the pattern we
140           wish to match against the URL that the browser, or client, has
141           requested. Web developers will immediately recognize that the
142           forward slash symbolizes the root directory of our website.
143           Experienced Perl programmers will pick up on the fact that the
144           route pattern is nothing more than an argument for our "get"
145           keyword.
146
147       •   the route action; this is the subroutine that returns our data.
148           More precisely, it is a subroutine reference. The route action in
149           our example returns a simple string, "Hello World!". Like the route
150           pattern, the route action is nothing more than an argument to our
151           "get" keyword.
152
153           Note that convention has us use the fat comma ("=>") operator
154           between the route pattern and the action to to make our code more
155           readable. But we could just as well have used a regular old comma
156           to separate these argument to our "get" method. Gotta love Perl for
157           its flexibility.
158
159       So to put our route pattern in the example into plain English, we are
160       telling our app, "If the root directory is requested with the GET http
161       method, send the string 'Hello World!' back in our response." Of
162       course, since this is a web app, we also have to send back headers with
163       our response. This is quitely taken care of for us by Dancer2 so we
164       don't have to think about it.
165
166       The syntax for route handlers might seem a bit foreign for newer Perl
167       developers.  But rest assured there is nothing magical about it and it
168       is all just plain old Perl under the hood. If you keep in mind that the
169       keyword is a subroutine (or more precisely, a method) and that the
170       pattern and action are arguments to the keyword, you'll pick it up in
171       no time. Thinking of these keywords as "built-ins" to the Dancer2
172       framework might also eliminate any initial confusion about them.
173
174       The most important takeaway here is that we build our app by adding
175       route handlers which are nothing more than a collection of, HTTP verbs,
176       URL patterns, and actions.
177
178   How about a little more involved example?
179       While investigating some Python web frameworks like Flask
180       <http://flask.pocoo.org/> or Bottle <https://bottlepy.org/docs/dev/>, I
181       enjoyed the way they explained step-by-step how to build an example
182       application which was a little more involved than a trivial example.
183       This tutorial is modeled after them.
184
185       Using the Flaskr <https://github.com/pallets/flask> sample application
186       as my inspiration (OK, shamelessly plagiarised) I translated that
187       application to the Dancer2 framework so I could better understand how
188       Dancer2 worked. (I'm learning it too!)
189
190       So "dancr" was born.
191
192       dancr is a simple "micro" blog which uses the SQLite
193       <http://www.sqlite.org> database engine for simplicity's sake.  You'll
194       need to install sqlite on your server if you don't have it installed
195       already. Consult your OS documentation for getting SQLite installed on
196       your machine.
197
198       Required Perl modules
199
200       Obviously you need Dancer2 installed. You'll also need the Template
201       Toolkit, File::Slurper, and DBD::SQLite modules.  These all can be
202       installed using your CPAN client with the following command:
203
204           cpan Template File::Slurper DBD::SQLite
205
206   The database code
207       We're not going to spend a lot of time on the database, as it's not
208       really the point of this particular tutorial. Try not to dwell on this
209       section too much if you don't understand all of it.
210
211       Open your favorite text editor <http://www.vim.org> and create a schema
212       definition called 'schema.sql' with the following content:
213
214           create table if not exists entries (
215               id integer primary key autoincrement,
216               title string not null,
217               text string not null
218           );
219
220       Here we have a single table with three columns: id, title, and text.
221       The 'id' field is the primary key and will automatically get an ID
222       assigned by the database engine when a row is inserted.
223
224       We want our application to initialize the database automatically for us
225       when we start it. So, let's edit the 'dancr.pl' file we created earlier
226       and give it the ability to talk to our database with the following
227       subroutines: (Or, if you prefer, you can copy and paste the finished
228       dancr.pl script, found near the end of Part I in this tutorial, into
229       the file all at once and then just follow along with the tutorial.)
230
231           sub connect_db {
232               my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database'))
233                   or die $DBI::errstr;
234
235               return $dbh;
236           }
237
238           sub init_db {
239               my $db     = connect_db();
240               my $schema = read_text('./schema.sql');
241               $db->do($schema)
242                   or die $db->errstr;
243           }
244
245       Nothing too fancy in here, I hope. It's standard DBI except for the
246       setting('database') thing, more on that in a bit. For now, just assume
247       that the expression evaluates to the location of the database file.
248
249       In Part III of the tutorial, we will show you how to use the
250       Dancer2::Plugin::Database module for an easier way to configure and
251       manage database connections for your Dancer2 apps.
252
253   Our first route handler
254       Ok, let's get back to the business of learning Dancer2 by creating our
255       app's first route handler for the root URL.  Replace the route handler
256       in our simple example above with this one:
257
258           get '/' => sub {
259               my $db  = connect_db();
260               my $sql = 'select id, title, text from entries order by id desc';
261
262               my $sth = $db->prepare($sql)
263                   or die $db->errstr;
264
265               $sth->execute
266                   or die $sth->errstr;
267
268               template 'show_entries.tt', {
269                   msg           => get_flash(),
270                   add_entry_url => uri_for('/add'),
271                   entries       => $sth->fetchall_hashref('id'),
272               };
273           };
274
275       Our new route handler is the same as the one in our first example
276       except that our route action does a lot more work.
277
278       Something you might not have noticed right away is the semicolon at the
279       end of the route handler. This might confuse newer Perl coders and is a
280       source of bugs for more experienced ones who forget to add it. We need
281       the semicolon there because we are creating a reference to a subroutine
282       and because that's just what the Perl compiler demands and we must obey
283       if we want our code to run.
284
285       Alright, let's take a closer look at this route's action. The first few
286       lines are standard DBI. The important bit related to Dancer2 is the
287       "template" keyword at the end of the action. That tells Dancer2 to
288       process the output through one of its templating engines. There are
289       many template engines available for use with Dancer2. In this tutorial,
290       we're using Template Toolkit which offers a lot more flexibility than
291       the simple default Dancer2 template engine.
292
293       Templates all go into a "views/" directory which located in the same
294       directory as our dancr.pl script. Optionally, you can create a "layout"
295       template which provides a consistent look and feel for all of your
296       views. We'll construct our own layout template, cleverly named main.tt,
297       a little later in this tutorial.
298
299       So what's going on with the hashref as the second argument to the
300       template directive? Those are all of the parameters we want to pass
301       into our template. We have a "msg" field which displays a message to
302       the user when an event happens like a new entry is posted, or the user
303       logs in or out.  It's called a "flash" message because we only want to
304       display it one time, not every time the "/" URL is rendered.
305
306       The "uri_for" directive tells Dancer2 to provide a URI for that
307       specific route, in this case, it is the route to post a new entry into
308       the database.  You might ask why we don't simply hardcode the "/add"
309       URI in our application or templates.  The best reason not to do that is
310       because it removes a layer of flexibility as to where to "mount" the
311       web application.  Although the application is coded to use the root URL
312       "/" it might be better in the future to locate it under its own URL
313       route (maybe "/dancr"?)  - at that point we'd have to go through our
314       application and the templates and update the URLs and hope we didn't
315       miss any of them.  By using the "uri_for" Dancer2 method, we can easily
316       load the application wherever we like and not have to modify the
317       application at all.
318
319       Finally, the "entries" field contains a hashref with the results from
320       our database query.  Those results will be rendered in the template
321       itself, so we just pass them in.
322
323       So what does the show_entries.tt template look like? This:
324
325         [% IF session.logged_in %]
326           <form action="[% add_entry_url %]" method=post class=add-entry>
327             <dl>
328               <dt>Title:
329               <dd><input type=text size=30 name=title>
330               <dt>Text:
331               <dd><textarea name=text rows=5 cols=40></textarea>
332               <dd><input type=submit value=Share>
333             </dl>
334           </form>
335         [% END %]
336         <ul class=entries>
337         [% IF entries.size %]
338           [% FOREACH id IN entries.keys.nsort %]
339             <li><h2>[% entries.$id.title | html %]</h2>[% entries.$id.text | html %]
340           [% END %]
341         [% ELSE %]
342           <li><em>Unbelievable. No entries here so far</em>
343         [% END %]
344         </ul>
345
346       Go ahead and create a "views/" directory in the same directory as the
347       script and add this file to it.
348
349       Again, since this isn't a tutorial about Template Toolkit, we'll gloss
350       over the syntax here and just point out the section which starts with
351       "<ul class=entries>". This is the section where the database query
352       results are displayed. You can also see at the very top some discussion
353       about a session, more on that soon.
354
355       The only other Template Toolkit related thing that has to be mentioned
356       here is the "| html" in "[% entries.$id.title | html %]". That's a
357       filter <http://www.template-
358       toolkit.org/docs/manual/Filters.html#section_html> to convert
359       characters like "<" and ">" to "&lt;" and "&gt;". This way they will be
360       displayed by the browser as content on the page rather than just
361       included. If we did not do this, the browser might interpret content as
362       part of the page, and a malicious user could smuggle in all kinds of
363       bad code that would then run in another user's browser. This is called
364       Cross Site Scripting <https://en.wikipedia.org/wiki/Cross-
365       site_scripting> or XSS and you should make sure to avoid it by always
366       filtering data that came in from the web when you display it in a
367       template.
368
369   Other HTTP verbs
370       There are 8 defined HTTP verbs defined in RFC 2616
371       <http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9>: OPTIONS,
372       GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT.  Of these, the majority
373       of web applications focus on the verbs which closely map to the CRUD
374       (Create, Retrieve, Update, Delete) operations most database-driven
375       applications need to implement.
376
377       In addition, the "PATCH" verb was defined in RFC5789
378       <http://tools.ietf.org/html/rfc5789>, and is intended as a "partial
379       PUT", sending just the changes required to the entity in question.  How
380       this would be handled is down to your app, it will vary depending on
381       the type of entity in question and the serialization in use.
382
383       Dancer2's keywords currently supports GET, PUT/PATCH, POST, DELETE,
384       OPTIONS which map to Retrieve, Update, Create, Delete respectively.
385       Let's take a look now at the "/add" route handler which handles a POST
386       operation.
387
388           post '/add' => sub {
389               if ( not session('logged_in') ) {
390                   send_error("Not logged in", 401);
391               }
392
393               my $db = connect_db();
394               my $sql = 'insert into entries (title, text) values (?, ?)';
395               my $sth = $db->prepare($sql)
396                   or die $db->errstr;
397
398               $sth->execute(
399                   body_parameters->get('title'),
400                   body_parameters->get('text')
401               ) or die $sth->errstr;
402
403               set_flash('New entry posted!');
404               redirect '/';
405           };
406
407       As before, the HTTP verb begins the handler, followed by the route, and
408       a subroutine to do something; in this case it will insert a new entry
409       into the database.
410
411       The first check in the subroutine is to make sure the user sending the
412       data is logged in. If not, the application returns an error and stops
413       processing. Otherwise, we have standard DBI stuff. Let me insert (heh,
414       heh) a blatant plug here for always, always using parameterized INSERTs
415       in your application SQL statements. It's the only way to be sure your
416       application won't be vulnerable to SQL injection. (See
417       <http://www.bobby-tables.com> for correct INSERT examples in multiple
418       languages.) Here we're using the "body_parameters" convenience method
419       to pull in the parameters in the current HTTP request. (You can see the
420       'title' and 'text' form parameters in the show_entries.tt template
421       above.) Those values are inserted into the database, then we set a
422       flash message for the user and redirect her back to the root URL.
423
424       It's worth mentioning that the "flash message" is not part of Dancer2,
425       but a part of this specific application. We need to implement it
426       ourself.
427
428   Logins and sessions
429       Dancer2 comes with a simple in-memory session manager out of the box.
430       It supports a bunch of other session engines including YAML, memcached,
431       browser cookies and others. We'll just stick with the in-memory model
432       which works great for development and tutorials, but won't persist
433       across server restarts or scale very well in "real world" production
434       scenarios.
435
436       Configuration options
437
438       To use sessions in our application, we have to tell Dancer2 to activate
439       the session handler and initialize a session manager. To do that, we
440       add some configuration directives toward the top of our 'dancr.pl'
441       file.  But there are more options than just the session engine we want
442       to set.
443
444           set 'database'     => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
445           set 'session'      => 'Simple';
446           set 'template'     => 'template_toolkit';
447           set 'logger'       => 'console';
448           set 'log'          => 'debug';
449           set 'show_errors'  => 1;
450           set 'startup_info' => 1;
451
452       Hopefully these are fairly self-explanatory. We want the Simple session
453       engine, the Template Toolkit template engine, logging enabled (at the
454       'debug' level with output to the console instead of a file), we want to
455       show errors to the web browser and prints a banner at the server start
456       with information such as versions and the environment.
457
458       Dancer2 doesn't impose any limits on what parameters you can set using
459       the "set" syntax. For this application we're going to embed our single
460       username and password into the application itself:
461
462           set 'username' => 'admin';
463           set 'password' => 'password';
464
465       Hopefully no one will ever guess our clever password!  Obviously, you
466       will want a more sophisticated user authentication scheme in any sort
467       of non-tutorial application but this is good enough for our purposes.
468
469       In Part II of our tutorial, we will show you how to use Dancer2's
470       configuration files to manage these options and set up different
471       environments for your app using different configuration files. For now,
472       we're going to keep it simple and leave that discussion for later.
473
474       Logging in
475
476       Now that dancr is configured to handle sessions, let's take a look at
477       the URL handler for the "/login" route.
478
479           any ['get', 'post'] => '/login' => sub {
480               my $err;
481
482               if ( request->method() eq "POST" ) {
483                   # process form input
484                   if ( body_parameters->get('username') ne setting('username') ) {
485                       $err = "Invalid username";
486                   }
487                   elsif ( body_parameters->get('password') ne setting('password') ) {
488                       $err = "Invalid password";
489                   }
490                   else {
491                       session 'logged_in' => true;
492                       set_flash('You are logged in.');
493                       return redirect '/';
494                   }
495               }
496
497               # display login form
498               template 'login.tt', {
499                   err => $err,
500               };
501           };
502
503       This is the first handler which accepts two different verb types, a GET
504       for a human browsing to the URL and a POST for the browser to submit
505       the user's input to the web application.  Since we're handling two
506       different verbs, we check to see what verb is in the request.  If it's
507       not a POST, we drop down to the "template" directive and display the
508       login.tt template:
509
510         <h2>Login</h2>
511         [% IF err %]<p class=error><strong>Error:</strong> [% err %][% END %]
512         <form action="[% login_url %]" method=post>
513           <dl>
514             <dt>Username:
515             <dd><input type=text name=username>
516             <dt>Password:
517             <dd><input type=password name=password>
518             <dd><input type=submit value=Login>
519           </dl>
520         </form>
521
522       This is even simpler than our show_entries.tt template–but wait–
523       there's a "login_url" template parameter and we're only passing in the
524       "err" parameter. Where's the missing parameter? It's being generated
525       and sent to the template in a "before_template_render" directive, we'll
526       come back to that in a moment or two.
527
528       So the user fills out the login.tt template and submits it back to the
529       "/login" route handler.  We now check the user input against our
530       application settings and if the input is incorrect, we alert the user,
531       otherwise the application starts a session and sets the "logged_in"
532       session parameter to the true() value. Dancer2 exports both a true()
533       and false() convenience method which we use here.  After that, it's
534       another flash message and back to the root URL handler.
535
536       Logging out
537
538       And finally, we need a way to clear our user's session with the
539       customary logout procedure.
540
541           get '/logout' => sub {
542               app->destroy_session;
543               set_flash('You are logged out.');
544               redirect '/';
545           };
546
547       "app->destroy_session;" is Dancer2's way to remove a stored session.
548       We notify the user she is logged out and route her back to the root URL
549       once again.
550
551       You might wonder how we can then set a value in the session in
552       "set_flash", because we just destroyed the session.
553
554       Destroying the session has removed the data from the persistence layer
555       (which is the memory of our running application, because we are using
556       the "simple" session engine). If we write to the session now, it will
557       actually create a completely new session for our user. This new, empty
558       session will have a new session ID, which Dancer2 tells the user's
559       browser about in the response.  When the browser requests the root URL,
560       it will send this new session ID to our application.
561
562   Layout and static files
563       We still have a missing puzzle piece or two. First, how can we use
564       Dancer2 to serve our CSS stylesheet? Second, where are flash messages
565       displayed?  Third, what about the "before_template_render" directive?
566
567       Serving static files
568
569       In Dancer2, static files should go into the "public/" directory, but in
570       the application itself be sure to omit the "public/" element from the
571       path.  For example, the stylesheet for dancr lives in
572       "dancr/public/css/style.css" but is served from
573       <http://localhost:3000/css/style.css>.
574
575       If you wanted to build a mostly static web site you could simply write
576       route handlers like this one:
577
578           get '/' => sub {
579               send_file 'index.html';
580           };
581
582       where index.html would live in your "public/" directory.
583
584       "send_file" does exactly what it says: it loads a static file, then
585       sends the contents of that file to the user.
586
587       Let's go ahead and create our style sheet. In the same directory as
588       your dancr.pl script, issue the following commands:
589
590           mkdir public && mkdir public/css && touch public/css/style.css
591
592       Next add the following css to the "public/css/style.css" file you just
593       created:
594
595           body            { font-family: sans-serif; background: #eee; }
596           a, h1, h2       { color: #377ba8; }
597           h1, h2          { font-family: 'Georgia', serif; margin: 0; }
598           h1              { border-bottom: 2px solid #eee; }
599           h2              { font-size: 1.2em; }
600
601           .page           { margin: 2em auto; width: 35em; border: 5px solid #ccc;
602                             padding: 0.8em; background: white; }
603           .entries        { list-style: none; margin: 0; padding: 0; }
604           .entries li     { margin: 0.8em 1.2em; }
605           .entries li h2  { margin-left: -1em; }
606           .add-entry      { font-size: 0.9em; border-bottom: 1px solid #ccc; }
607           .add-entry dl   { font-weight: bold; }
608           .metanav        { text-align: right; font-size: 0.8em; padding: 0.3em;
609                             margin-bottom: 1em; background: #fafafa; }
610           .flash          { background: #cee5F5; padding: 0.5em;
611                             border: 1px solid #aacbe2; }
612           .error          { background: #f0d6d6; padding: 0.5em; }
613
614       Be sure to save the file.
615
616       Layouts
617
618       I mentioned earlier in the tutorial that it is possible to create a
619       "layout" template. In dancr, that layout is called "main" and it's set
620       up by putting in a directive like this:
621
622           set layout => 'main';
623
624       near the top of your web application.  This tells Dancer2's template
625       engine that it should look for a file called main.tt in
626       "views/layouts/" and insert the calls from the "template" directive
627       into a template parameter called "content".
628
629       Here is the simple layout file we will use for this web application. Go
630       ahead and add this the main.tt file to the "views/layouts/" directory.
631
632         <!doctype html>
633         <html>
634         <head>
635           <title>dancr</title>
636           <link rel=stylesheet type=text/css href="[% css_url %]">
637         </head>
638         <body>
639           <div class=page>
640           <h1>dancr</h1>
641              <div class=metanav>
642              [% IF not session.logged_in %]
643                <a href="[% login_url %]">log in</a>
644              [% ELSE %]
645                <a href="[% logout_url %]">log out</a>
646              [% END %]
647           </div>
648           [% IF msg %]
649             <div class=flash> [% msg %] </div>
650           [% END %]
651           [% content %]
652         </div>
653         </body>
654         </html>
655
656       Aha! You now see where the flash message "msg" parameter gets rendered.
657       You can also see where the content from the specific route handlers is
658       inserted (the fourth line from the bottom in the "content" template
659       parameter).
660
661       But what about all those other *_url template parameters?
662
663       Using "before_template_render"
664
665       Dancer2 has a way to manipulate the template parameters before they're
666       passed to the engine for processing. It's "before_template_render".
667       Using this keyword, you can generate and set the URIs for the "/login"
668       and "/logout" route handlers and the URI for the stylesheet. This is
669       handy for situations like this where there are values which are re-used
670       consistently across all (or most) templates. This cuts down on code-
671       duplication and makes your app easier to maintain over time since you
672       only need to update the values in this one place instead of everywhere
673       you render a template.
674
675           hook before_template_render => sub {
676               my $tokens = shift;
677
678               $tokens->{'css_url'}    = request->base . 'css/style.css';
679               $tokens->{'login_url'}  = uri_for('/login');
680               $tokens->{'logout_url'} = uri_for('/logout');
681           };
682
683       Here again I'm using "uri_for" instead of hardcoding the routes.  This
684       code block is executed before any of the templates are processed so
685       that the template parameters have the appropriate values before being
686       rendered.
687
688   Putting it all together
689       Here's the complete 'dancr.pl' script from start to finish.
690
691           use Dancer2;
692           use DBI;
693           use File::Spec;
694           use File::Slurper qw/ read_text /;
695           use Template;
696
697           set 'database'     => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
698           set 'session'      => 'Simple';
699           set 'template'     => 'template_toolkit';
700           set 'logger'       => 'console';
701           set 'log'          => 'debug';
702           set 'show_errors'  => 1;
703           set 'startup_info' => 1;
704           set 'username'     => 'admin';
705           set 'password'     => 'password';
706           set 'layout'       => 'main';
707
708           sub set_flash {
709               my $message = shift;
710
711               session flash => $message;
712           }
713
714           sub get_flash {
715               my $msg = session('flash');
716               session->delete('flash');
717
718               return $msg;
719           }
720
721           sub connect_db {
722               my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database'))
723                   or die $DBI::errstr;
724
725               return $dbh;
726           }
727
728           sub init_db {
729               my $db     = connect_db();
730               my $schema = read_text('./schema.sql');
731               $db->do($schema)
732                   or die $db->errstr;
733           }
734
735           hook before_template_render => sub {
736               my $tokens = shift;
737
738               $tokens->{'css_url'}    = request->base . 'css/style.css';
739               $tokens->{'login_url'}  = uri_for('/login');
740               $tokens->{'logout_url'} = uri_for('/logout');
741           };
742
743           get '/' => sub {
744               my $db  = connect_db();
745               my $sql = 'select id, title, text from entries order by id desc';
746
747               my $sth = $db->prepare($sql)
748                   or die $db->errstr;
749
750               $sth->execute
751                   or die $sth->errstr;
752
753               template 'show_entries.tt', {
754                   msg           => get_flash(),
755                   add_entry_url => uri_for('/add'),
756                   entries       => $sth->fetchall_hashref('id'),
757               };
758           };
759
760           post '/add' => sub {
761               if ( not session('logged_in') ) {
762                   send_error("Not logged in", 401);
763               }
764
765               my $db  = connect_db();
766               my $sql = 'insert into entries (title, text) values (?, ?)';
767
768               my $sth = $db->prepare($sql)
769                   or die $db->errstr;
770
771               $sth->execute(
772                   body_parameters->get('title'),
773                   body_parameters->get('text')
774               ) or die $sth->errstr;
775
776               set_flash('New entry posted!');
777               redirect '/';
778           };
779
780           any ['get', 'post'] => '/login' => sub {
781               my $err;
782
783               if ( request->method() eq "POST" ) {
784                   # process form input
785                   if ( body_parameters->get('username') ne setting('username') ) {
786                       $err = "Invalid username";
787                   }
788                   elsif ( body_parameters->get('password') ne setting('password') ) {
789                       $err = "Invalid password";
790                   }
791                   else {
792                       session 'logged_in' => true;
793                       set_flash('You are logged in.');
794                       return redirect '/';
795                   }
796               }
797
798               # display login form
799               template 'login.tt', {
800                   err => $err,
801               };
802
803           };
804
805           get '/logout' => sub {
806               app->destroy_session;
807               set_flash('You are logged out.');
808               redirect '/';
809           };
810
811           init_db();
812           start;
813
814       Advanced route moves
815
816       There's a lot more to route matching than shown here. For example, you
817       can match routes with regular expressions, or you can match pieces of a
818       route like "/hello/:name" where the ":name" piece magically turns into
819       a named parameter in your handler for manipulation.
820
821       You can explore this and other advanced concepts by reading the
822       Dancer2::Manual.
823

Part II: Taking Advantage of the "dancer2" Utility to Set Up New Apps

825       In Part I, we took an ordinary Perl script and turned it into a simple
826       web app to teach you basic Dancer2 concepts. While starting with a
827       simple script like this helped make it easier to teach these concepts,
828       it did not demonstrate how a typical app is built by a Dancer2
829       developer. So let's show you how things really get done.
830
831   Creating a new app
832       So now that you have a better idea of what goes into building an app
833       with Dancer2, it's time to cha-cha with the "dancer2" utility which
834       will save you a lot of time and effort by setting up directories,
835       files, and default configuration settings for you.
836
837       The "dancer2" utility was installed on your machine when you installed
838       the Dancer2 distribution. Hop over to the command line into a directory
839       you have permission to write to and issue the following command:
840
841           dancer2 gen -a Dancr2
842
843       That command should output something like the following to the console:
844
845           + Dancr2
846           + Dancr2/config.yml
847           + Dancr2/Makefile.PL
848           + Dancr2/MANIFEST.SKIP
849           + Dancr2/.dancer
850           + Dancr2/cpanfile
851           + Dancr2/bin
852           + Dancr2/bin/app.psgi
853           + Dancr2/environments
854           + Dancr2/environments/development.yml
855           + Dancr2/environments/production.yml
856           + Dancr2/lib
857           + Dancr2/lib/Dancr2.pm
858           + Dancr2/public
859           + Dancr2/public/favicon.ico
860           + Dancr2/public/500.html
861           + Dancr2/public/dispatch.cgi
862           + Dancr2/public/404.html
863           + Dancr2/public/dispatch.fcgi
864           + Dancr2/public/css
865           + Dancr2/public/css/error.css
866           + Dancr2/public/css/style.css
867           + Dancr2/public/images
868           + Dancr2/public/images/perldancer.jpg
869           + Dancr2/public/images/perldancer-bg.jpg
870           + Dancr2/public/javascripts
871           + Dancr2/public/javascripts/jquery.js
872           + Dancr2/t
873           + Dancr2/t/001_base.t
874           + Dancr2/t/002_index_route.t
875           + Dancr2/views
876           + Dancr2/views/index.tt
877           + Dancr2/views/layouts
878           + Dancr2/views/layouts/main.tt
879
880       What you just did was create a fully functional app in Dancer2 with
881       just one command! The new app, named "Dancr2," won't do anything
882       particularly useful until you add your own routes to it, but it does
883       take care of many of the tedious tasks of setting up an app for you.
884
885       The files and folders that were generated and that you see listed above
886       provide a convenient scaffolding, or skeleton, upon which you can build
887       your app. The default skelelton provides you with basic error pages,
888       css, javascript, graphics, tests, templates and other files which you
889       are free to modify and customize to your liking.
890
891       If you don't like the default skeleton provided to you by Dancer, the
892       "dancer2" command allows you to generate your own custom skeletons.
893       Consult "BOOTSTRAPPING-A-NEW-APP" in Dancer2::Manual for further
894       details on this and other capabilities of the "dancer2") utility.
895
896   Getting the new app up and running with Plack
897       In Part I, we used the "start" command in our script to launch a server
898       to serve our app. Things are a little different when using "dancer2",
899       however. You'll notice that the "dancer2" utility created a "bin/"
900       directory with a file in it called "app.psgi". This is the file we use
901       to get our app up and running.
902
903       Let's see how to to do that by first changing into the Dancr2 directory
904       and then starting the server using the "plackup" command:
905
906           cd Dancr2;
907           plackup -p 5000 bin/app.psgi
908
909       If all went well, you'll be able to see the Dancr2 home page by
910       visiting:
911
912           http://localhost:5000
913
914       The web page you see there gives you some very basic advice for tuning
915       and modifying your app and where you can go for more information to
916       learn about developing apps with Dancer2 (like this handy tutorial!).
917
918       Our Dancr2 app is served on a simple web server provided by Plack.
919       Plack is PSGI compliant software, hence the "psgi" extension for our
920       file in the "bin/" directory. Plack and PSGI is beyond the scope of
921       this tutorial but you can learn more by visiting the Plack website
922       <http://plackperl.org/>.
923
924       For now, all you need to know is that if you are deploying an app for
925       use by just yourself or a handful of people on a local network, Plack
926       alone may do the trick. More typically, you would use Plack in
927       conjunction with other server software to make your app much more
928       robust. But in the early stages of your app's development, a simple
929       Plack server is more than likely all you need.
930
931       To learn more about the different ways for deploying your app, see the
932       Dancer2 Deployment Manual
933
934   Porting dancr.pl over to the new Dancr2 app
935       Ok, so now that we've got our new Dancr2 app up and running, it's time
936       to learn how to take advantage of what the "dancer2" utility set up for
937       us by porting our dancr.pl script created in Part I into Dancr2.
938
939       The "lib/" directory
940
941       The "lib/" directory in our Dancr2 app is where our "app.psgi" file
942       will expect our code to live. So let's take a peek at the file
943       generated for us in there:
944
945           cat lib/Dancr2.pm
946
947       You'll see something like the following bit of code which provides a
948       single route to our app's home page and loads the index template:
949
950           package Dancr2;
951           use Dancer2;
952
953           our $VERSION = '0.1';
954
955           get '/' => sub {
956               template 'index' => { title => 'Dancr2' };
957           };
958
959           true;
960
961       The first thing you'll notice is that instead of a script, we are using
962       a module, "Dancr2" to package our code. Modules make it easer to pull
963       off many powerful tricks like packaging our app across several discrete
964       modules. We'll let the manual explain this more advanced technique.
965
966       Updating the Dancr2 module
967
968       Now that we know where to put our code, let's update the "Dancr2.pm"
969       module with our original "dancr.pl" code. Remove the existing sample
970       route in "Dancr2.pm" and replace it with the code from our "dancr.pl"
971       file. You'll have to make a couple of adjustments to the "dancr.pl"
972       code like removing the "use Dancer2;" line since it's already provided
973       by our module. You'll also want to be sure to remove the "start;" line
974       as well from the end of the file.
975
976       When you're done, "Dancr2.pm" should look something close to this:
977
978           package Dancr2;
979           use Dancer2;
980
981           our $VERSION = '0.1';
982
983           # Our original dancr.pl code with some minor tweaks
984           use DBI;
985           use File::Spec;
986           use File::Slurper qw/ read_text /;
987           use Template;
988
989           set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
990           set 'session'  => 'YAML';
991           ...
992
993           <snip> # The rest of the stuff </snip>
994
995           ...
996
997           sub init_db {
998               my $schema = read_text('./schema.sql');
999               $db->do($schema)
1000                   or die $db->errstr;
1001           }
1002
1003           get '/logout' => sub {
1004               app->destroy_session;
1005               set_flash('You are logged out.');
1006               redirect '/';
1007           };
1008
1009           init_db();
1010
1011       Finally, to avoid getting an error in the "init_db") subroutine when it
1012       tries to load our schema file, copy over the "schema.db" file to the
1013       root directory of the Dancr2 app:
1014
1015           cp /path/to/dancr.pl/schema.db /path/to/Dancr2;
1016
1017       Ok, now that we've got the code moved over, let's move the assets from
1018       dancr.pl to our new app.
1019
1020       The "public/" directory
1021
1022       As mentioned in Part I, our static assets go into our "public/"
1023       directory. If you followed along with the tutorial in Part I, you
1024       should have a "public/" directory with a "public/css" subdirectory and
1025       a file called "style.css" within that.
1026
1027       Dancer2 has conveniently generated the "public/css" directory for us
1028       which has a default css file. Let's copy the style sheet from our
1029       original app so our new app can use it:
1030
1031           # Note: This command overwrites the default style sheet. Move it or copy
1032           # it if you wish to preserve it.
1033
1034           cp /path/to/dancr.pl/public/css/style.css /path/to/Dancr2/public/css;
1035
1036       The "views" directory
1037
1038       Along with our "public/" directory, Dancer has also provided a "views/"
1039       directory, which as we covered, serves as the a home for our templates.
1040       Let's get those copied over now:
1041
1042           # NOTE: This command will overwrite the default main.tt tempalte file. Move
1043           # it or copy it if you wish to preserve it.
1044
1045           cp -r /path/to/dancr.pl/views/* /path/to/Dancr2/views;
1046
1047       Does it work?
1048
1049       If you followed the instructions here closely, your Dancr2 app should
1050       be working.  Shut down any running Plack servers and then issue the
1051       same plackup command to see if it runs:
1052
1053           cd /path/to/Dancr2
1054           plackup -p 5000 bin/app.psgi
1055
1056       If you see any errors, get them resolved until the app loads.
1057
1058   Configuring Your App
1059       In Part I, you configured your app with a series of "set" statements
1060       near the top of your file. Now we will show you a better way to
1061       configure your app using Dancer2's configuration files.
1062
1063       Your skeleton provides your app with three different configuration
1064       files. The first two files we'll discuss, found in the "environments/"
1065       folder of your app, are "development.yml" and "production.yml". As you
1066       can probably guess, the "development.yml" file has settings intended to
1067       be used while developing the app. The "production.yml" file has
1068       settings more appropriate for running your app when used by others. The
1069       third configuration file is found in the root directory of your app and
1070       is named "config.yml". This file has the settings that are common to
1071       all environments but that can be overridden by the environment
1072       configuration files. You can still override any configuration file
1073       settings in your modules using the "set" command.
1074
1075       We will take a look at the "development.yml" file first. Open that file
1076       in your text editor and take a look inside. It has a bunch of helpful
1077       comments and the following five settings sprinkled throughout:
1078
1079           logger: "console"
1080           log: "core"
1081           show_errors: 1
1082           startup_info: 1
1083
1084       The first four settings duplicate many of the settings in our new
1085       Dancr2 app. So in the spirit of DRY (don't repeat yourself), edit your
1086       Dancr2 module and delete the four lines that correspond to these four
1087       settings.
1088
1089       Then, in the configuration file, be sure to change the value for the
1090       "log" setting from "core" to "debug" so it matches the value we had in
1091       our module.
1092
1093       We will leave it up to you what you want to do with the fourth setting,
1094       "startup_info". You can read about that setting, along with all the
1095       other settings, in the configuration manual.
1096
1097       Finally, let's add a new setting to the configuration file for
1098       "session" with the following line:
1099
1100           session: "Simple"
1101
1102       Then delete the corresponding setting from your Dancr2 module.
1103
1104       Alright, our Dancr2 app is a little leaner and meaner. Now open the
1105       main "config.yml" file and look for the settings in there that are also
1106       duplicated in our app's module. There are two:
1107
1108           layout: "main"
1109           template: "simple"
1110
1111       Leave "layout" as is but change the template setting to
1112       "template_toolkit".  Then edit your Dancr2 module file and delete these
1113       two settings.
1114
1115       Finally, add the following configuration settings to the .yml file:
1116
1117           username: "admin"
1118           password: "password"
1119
1120       Then you delete these two settings from the Dancr2 module, as well.
1121
1122       So, if you have been following along, you now have only the following
1123       "set" command in your Dancr2 module, related to the database
1124       configuration:
1125
1126           set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
1127
1128       We will get rid of this setting in Part III of the tutorial. All the
1129       rest of the settings have been transferred to our configuration files.
1130       Nice!
1131
1132       We still have a little more cleanup we can do. Now that Dancer2 knows
1133       we are using Template::Toolkit, we can delete the "use Template;" line
1134       from our module.
1135
1136       Now start the app "plackup" command and check to see that everything
1137       works. By default, Dancer2 will load the development environment
1138       configuration. When it comes time to put your app into production, you
1139       can load the "production.yml" file configuration with plackup's "--env"
1140       switch like so:
1141
1142           plackup -p 5000 --env production bin/app.psgi
1143
1144   Keep on Dancing!
1145       This concludes Part II of our tutorial where we showed you how to take
1146       advantage of the "dancer2" utility to set up a app skeleton to make it
1147       really easy to get started developing your own apps.
1148
1149       Part III will refine our app a little further by showing you how to use
1150       plugins so you can start capitalizing on all the great work contributed
1151       by other Dancer2 developers.
1152

Part III: Plugins, Your Many Dancing Partners

1154       Dancer2 takes advantage of the open source software revolution by
1155       making it exceedingly easy to use plugins that you can mix into your
1156       app to give it new functionality. In Part III of this tutorial, we will
1157       update our new Dancr2 app to use the Dancer2::Plugin::Database to give
1158       you enough skills to go out and explore other plugins on your own.
1159
1160   Installing plugins
1161       Like Dancer2 itself, Dancer2 plugins can be found on the CPAN. Use your
1162       favorite method for downloading and installing the
1163       Dancer2::Plugin::Database module on your machine. We recommend using
1164       "cpanminus" like so:
1165
1166           cpanm Dancer2::Plugin::Database
1167
1168   Using plugins
1169       Using a plugin couldn't be easier. Simply add the following line to
1170       your Dancr2 module below the "use Dancer2;" line in your module:
1171
1172           use Dancer2::Plugin::Database;
1173
1174   Configuring plugins
1175       Plugins can be configured with the YAML configuration files mentioned
1176       in Part II of this tutorial. Let's edit the "development.yml" file and
1177       add our database configuration there. Below the last line in that file,
1178       add the following lines, being careful to keep the indentation as you
1179       see it here:
1180
1181         plugins:                 # all plugin configuration settings go in this section
1182           Database:              # the name of our plugin
1183             driver: "SQLite"     # driver we want to use
1184             database: "dancr.db" # where the database will go in our app
1185                                  # run a query when connecting to the datbase:
1186             on_connect_do: [ "create table if not exists entries (id integer primary key autoincrement, title string not null, text string not null)" ]
1187
1188       Here, we direct our database plugin to use the "SQLite" driver and to
1189       place the database in the root directory of our Dancr2. The
1190       "on_connect_db" setting tells the plugin to run an SQL query when it
1191       connects with the database to create a table for us if it doesn't
1192       already exist.
1193
1194   Modifying our database code in the Dancr2 module
1195       Now it's time to modify our Dancr2 module so it will use the plugin to
1196       query the database instead of our own code. There are a few things to
1197       do. First, we will delete the code we no longer need.
1198
1199       Since our configuration file tells the plugin where our database is, we
1200       can delete this line:
1201
1202           set 'database' => File::Spec->catfile(File::Spec->tmpdir(), 'dancr.db');
1203
1204       And since the database plugin will create our database connection and
1205       initialize our database for us, we can scrap the following two
1206       subroutines and line from our module:
1207
1208           sub connect_db {
1209               my $dbh = DBI->connect("dbi:SQLite:dbname=".setting('database'))
1210                   or die $DBI::errstr;
1211
1212               return $dbh;
1213           }
1214
1215           sub init_db {
1216               my $db = connect_db();
1217               my $schema = read_text('./schema.sql');
1218               $db->do($schema)
1219                   or die $db->errstr;
1220           }
1221
1222           init_db(); # Found at the bottom of our file
1223
1224       With that done, let's now take advantage of a hook the plugin provides
1225       us that we can use to handle certain events by adding the following
1226       command to our module to handle database errors:
1227
1228           hook 'database_error' => sub {
1229               my $error = shift;
1230               die $error;
1231           };
1232
1233       Now let's make a few adjustments to the bits of code that make the
1234       database queries. In our "get '/'" route, change all instances of $db
1235       with "database" and remove all the "die" calls since we now have a hook
1236       to handle the errors for us. When you are done, your route should look
1237       something like this:
1238
1239           get '/' => sub {
1240               my $sql = 'select id, title, text from entries order by id desc';
1241               my $sth = database->prepare($sql);
1242               $sth->execute;
1243               template 'show_entries.tt', {
1244                   msg           => get_flash(),
1245                   add_entry_url => uri_for('/add'),
1246                   entries       => $sth->fetchall_hashref('id'),
1247               };
1248           };
1249
1250       Make the same changes to the "post '/add'" route to transform it into
1251       this:
1252
1253           post '/add' => sub {
1254               if ( not session('logged_in') ) {
1255                   send_error("Not logged in", 401);
1256               }
1257
1258               my $sql = 'insert into entries (title, text) values (?, ?)';
1259               my $sth = database->prepare($sql);
1260               $sth->execute(
1261                   body_parameters->get('title'),
1262                   body_parameters->get('text')
1263               );
1264
1265               set_flash('New entry posted!');
1266               redirect '/';
1267           };
1268
1269       Our last step is to get rid of the following lines which we no longer
1270       need, thanks to our plugin:
1271
1272         use DBI;
1273         use File::Spec;
1274         use File::Slurper qw/ read_text /;
1275
1276       That's it! Now start your app with "plackup" to make sure you don't get
1277       any errors and then point your browser to test the app to make sure it
1278       works as expected. If it doesn't, double and triple check your
1279       configuration settings and your module's code which should now look
1280       like this:
1281
1282           package Dancr2;
1283           use Dancer2;
1284           use Dancer2::Plugin::Database;
1285
1286           our $VERSION = '0.1';
1287
1288           my $flash;
1289
1290           sub set_flash {
1291               my $message = shift;
1292
1293               $flash = $message;
1294           }
1295
1296           sub get_flash {
1297               my $msg = $flash;
1298               $flash  = "";
1299
1300               return $msg;
1301           }
1302
1303           hook before_template_render => sub {
1304               my $tokens = shift;
1305
1306               $tokens->{'css_url'}    = request->base . 'css/style.css';
1307               $tokens->{'login_url'}  = uri_for('/login');
1308               $tokens->{'logout_url'} = uri_for('/logout');
1309           };
1310
1311           hook 'database_error' => sub {
1312               my $error = shift;
1313               die $error;
1314           };
1315
1316           get '/' => sub {
1317               my $sql = 'select id, title, text from entries order by id desc';
1318               my $sth = database->prepare($sql);
1319               $sth->execute;
1320               template 'show_entries.tt', {
1321                   msg           => get_flash(),
1322                   add_entry_url => uri_for('/add'),
1323                   entries       => $sth->fetchall_hashref('id'),
1324               };
1325           };
1326
1327           post '/add' => sub {
1328               if ( not session('logged_in') ) {
1329                   send_error("Not logged in", 401);
1330               }
1331
1332               my $sql = 'insert into entries (title, text) values (?, ?)';
1333               my $sth = database->prepare($sql);
1334               $sth->execute(
1335                   body_parameters->get('title'),
1336                   body_parameters->get('text')
1337               );
1338
1339               set_flash('New entry posted!');
1340               redirect '/';
1341           };
1342
1343           any ['get', 'post'] => '/login' => sub {
1344               my $err;
1345
1346               if ( request->method() eq "POST" ) {
1347                   # process form input
1348                   if ( params->{'username'} ne setting('username') ) {
1349                       $err = "Invalid username";
1350                   }
1351                   elsif ( params->{'password'} ne setting('password') ) {
1352                       $err = "Invalid password";
1353                   }
1354                   else {
1355                       session 'logged_in' => true;
1356                       set_flash('You are logged in.');
1357                       return redirect '/';
1358                   }
1359               }
1360
1361               # display login form
1362               template 'login.tt', {
1363                   err => $err,
1364               };
1365
1366           };
1367
1368           get '/logout' => sub {
1369               app->destroy_session;
1370               set_flash('You are logged out.');
1371               redirect '/';
1372           };
1373
1374           true;
1375
1376   Next steps
1377       Congrats! You are now using the database plugin like a boss. The
1378       database plugin does a lot more than what we showed you here. We'll
1379       leave it up to you to consult the Dancer2::Plugin::Database to unlock
1380       its full potential.
1381
1382       There are many more plugins for you to explore. You now know enough to
1383       install and experiment with them. Some of the more popular and useful
1384       plugins are listed at Dancer2::Plugins. You can also search CPAN with
1385       "Dancer2::Plugin" for a more comprehensive listing.
1386
1387       If you are feeling really inspired, you can learn how to extend Dancer2
1388       with your own plugins by reading Dancer2::Plugin.
1389

Happy dancing!

1391       I hope these tutorials have been helpful and interesting enough to get
1392       you exploring Dancer2 on your own. The framework is still under
1393       development but it's definitely mature enough to use in a production
1394       project.
1395
1396       Happy dancing!
1397
1398   One more thing: Test!
1399       Before we go, we want to mention that Dancer2 makes it very easy to run
1400       automated tests on your app to help you find bugs. If you are new to
1401       testing, we encourage you to start learning how. Your future self will
1402       thank you.  The effort you put into creating tests for your app will
1403       save you many hours of frustration in the long run. Unfortunately,
1404       until we get Part IV of this tutorial written, you'll have to consult
1405       the Dancer2 testing documentation for more details on how to test your
1406       app.
1407
1408       Enjoy!
1409

SEE ALSO

1411       •   <http://perldancer.org>
1412
1413       •   <http://github.com/PerlDancer/Dancer2>
1414
1415       •   Dancer2::Plugins
1416
1418       The CSS stylesheet is copied verbatim from the Flaskr example
1419       application and is subject to their license:
1420
1421       Copyright (c) 2010, 2013 by Armin Ronacher and contributors.
1422
1423       Some rights reserved.
1424
1425       Redistribution and use in source and binary forms of the software as
1426       well as documentation, with or without modification, are permitted
1427       provided that the following conditions are met:
1428
1429       •   Redistributions of source code must retain the above copyright
1430           notice, this list of conditions and the following disclaimer.
1431
1432       •   Redistributions in binary form must reproduce the above copyright
1433           notice, this list of conditions and the following disclaimer in the
1434           documentation and/or other materials provided with the
1435           distribution.
1436
1437       •   The names of the contributors may not be used to endorse or promote
1438           products derived from this software without specific prior written
1439           permission.
1440

AUTHOR

1442       Dancer Core Developers
1443
1445       This software is copyright (c) 2022 by Alexis Sukrieh.
1446
1447       This is free software; you can redistribute it and/or modify it under
1448       the same terms as the Perl 5 programming language system itself.
1449
1450
1451
1452perl v5.36.0                      2023-01-20              Dancer2::Tutorial(3)
Impressum