package Dancer2::Tutorial; # ABSTRACT: An example to get you dancing =encoding utf8 =pod =head1 Tutorial Overview This tutorial is has three parts. Since they build on one another, each part is meant to be gone through in sequential order. B, the longest part of this tutorial, will focus on the basics of Dancer2 development by building a simple yet functional blog app, called C, that you can use to impress your friends, mates, and family. In B, you'll learn about the preferred way to get your own web apps up and running by using the C utility. We will take the script written in Part I and convert it into a proper Dancer2 app, called C, to help you gain an understanding of what the C utility does for you. Finally, in B, we give you a taste of the power of plugins that other developers have written and will show you how to modify the C app to use a database plugin. This tutorial assumes you have some familiarity with Perl and that you know how to create and execute a Perl script on your computer. Some experience with web development is also greatly helpful but not entirely necessary. This tutorial is mostly geared toward developers but website designers can get something out of it as well since the basics of templating are covered plus it might be good for a designer to have a decent idea of how Dancer2 works. =head1 Part I: Let's Get Dancing! Part I covers many of the basic concepts you'll need to know to lay a good foundation for your future development work with Dancer2 by building a simple micro-blogging app. =head2 What is Dancer2? Dancer2 is a micro-web framework, written in the Perl programming language, and is modeled after a Ruby web application framework called L. When we say "micro" framework, we mean that Dancer2 aims to maximize your freedom and control by getting out of your way. "Micro" doesn't mean Dancer2 is only good for creating small apps. Instead, it means that Dancer2's primary focus is on taking care of a lot of the boring, technical details of your app for you and by creating an easy, clean routing layer on top of your app's code. It also means you have almost total control over the app's functionality and how you create and present your content. You will not confined to someone else's approach to creating a website or app. With Dancer2, you can build anything from a specialized content management system to providing a simple API for querying a database over the web. But you don't have to reinvent the wheel, either. Dancer2 has hundreds of plugins that you can take advantage of. You can add only the capabilities your app needs to keep complexity to a minimum. As a framework, Dancer2 provides you with the tools and infrastructure you can leverage to deliver content on the web quickly, easily and securely. The tools, Dancer2 provides, called "keywords," are commands that you use to build your app, access the data inside of it, and deliver it on the internet in many different formats. Dancer2's keywords provide what is called a B (DSL) designed specifically for the task of building apps. But don't let the technical jargon scare you off. Things will become clearer in our first code example which we will look at shortly. =head3 Getting Dancer2 installed First, we need to make sure you have Dancer2 installed. Typically, you will do that with one of the following two commands: cpan Dancer2 # requires the cpan command to be installed and configured cpanm Dancer2 # requires you have cpanminus installed If you aren't familiar with installing Perl modules on your machine, you should L. You may also want to consult your OS's documentation or a knowledgeable expert. And, of course, your search engine of choice is always there for you, as well. =head3 Your first Dancer2 "Hello World!" app Now that you have Dancer2 installed, open up your favorite text editor and copy and paste the following lines of Perl code into it and save it to a file called C: #!/usr/bin/env perl use Dancer2; get '/' => sub { return 'Hello World!'; }; start; If you make this script executable and run it, it will fire up a simple, standalone web server that will display "Hello World!" when you point your browser to L. Cool! B We want to emphasize that writing a script file like this with a C command is B how you would typically begin writing a Dancer2 app. Part II of this tutorial will show you the recommended approach using the C utility. For now, we want to stay focused on the fundamentals. So, though our example app is very simple, there is a lot going on under the hood when we invoke C in our first line of code. We won't go into the gory details of how it all works. For now, it's enough for you to know that the Dancer2 module infuses your script with the ability to use Dancer2 keywords for building apps. Getting comfortable with the concept of keywords is probably the most important step you can take as a budding Dancer2 developer and this tutorial will do its best to help foster your understanding of them. The next line of code in our example (which spans three lines to make it more readable) is the B. Let's examine this line closely, because route handlers are at the core of how to build an app with Dancer2. The syntax of a Dancer2 C has three parts: =over =item * an B or B; in this example, we use the C keyword to tell Dancer2 that this route should apply to GET http requests. C is the first of many keywords that Dancer2 provides that we will cover in this tutorial. Those familiar with web development will know that a GET request is what we use to fetch information from a website. =item * the B; this is the bit of code that appears immediately after our C keyword. In this example it is a forward slash (C), wrapped in single quotes, and it represents the pattern we wish to match against the URL that the browser, or client, has requested. Web developers will immediately recognize that the forward slash symbolizes the root directory of our website. Experienced Perl programmers will pick up on the fact that the route pattern is nothing more than an argument for our C keyword. =item * the B; this is the subroutine that returns our data. More precisely, it is a subroutine reference. The route action in our example returns a simple string, C. Like the route pattern, the route action is nothing more than an argument to our C keyword. Note that convention has us use the fat comma (C<< => >>) operator between the route pattern and the action to to make our code more readable. But we could just as well have used a regular old comma to separate these argument to our C method. Gotta love Perl for its flexibility. =back So to put our route pattern in the example into plain English, we are telling our app, "If the root directory is requested with the GET http method, send the string 'Hello World!' back in our response." Of course, since this is a web app, we also have to send back headers with our response. This is quitely taken care of for us by Dancer2 so we don't have to think about it. The syntax for route handlers might seem a bit foreign for newer Perl developers. But rest assured there is nothing magical about it and it is all just plain old Perl under the hood. If you keep in mind that the keyword is a subroutine (or more precisely, a method) and that the pattern and action are arguments to the keyword, you'll pick it up in no time. Thinking of these keywords as "built-ins" to the Dancer2 framework might also eliminate any initial confusion about them. The most important takeaway here is that we build our app by adding route handlers which are nothing more than a collection of, HTTP verbs, URL patterns, and actions. =head2 How about a little more involved example? While investigating some Python web frameworks like L or L, I enjoyed the way they explained step-by-step how to build an example application which was a little more involved than a trivial example. This tutorial is modeled after them. Using the L sample application as my inspiration (OK, shamelessly plagiarised) I translated that application to the Dancer2 framework so I could better understand how Dancer2 worked. (I'm learning it too!) So "dancr" was born. dancr is a simple "micro" blog which uses the L database engine for simplicity's sake. You'll need to install sqlite on your server if you don't have it installed already. Consult your OS documentation for getting SQLite installed on your machine. =head3 Required Perl modules Obviously you need L installed. You'll also need the L