Camelia

Getting Started with Bailador

Bailador is a bare-bones port of the Perl Dancer web development framework. It comes with a few examples. In this article we'll take a look at the basics to get started writing a web application.

Note! This site is about Perl 6.
If you are looking for a solution for the current production version of Perl 5, please check out the Perl 5 tutorial.

Rakudo Star already includes the latest version of Bailador, so once you installed Rakudo you don't need to worry about further installations.

You can just create a single file like this

bailador/first.pl

use v6;
use Bailador;

get '/' => sub {
    "hello world"
}

baile;

and run it as perl6 first.pl.

It will launch a small web-server and let you know that you can access it via port 3000:

perl6 files/bailador/first.pl 
Entering the development dance floor: http://0.0.0.0:3000
[2015-01-03T22:39:13Z] Started HTTP server.

In Bailador we need to map HTTP request methods, and the path on the server to anonymous subroutines that will returnt the content. In this example we mapped the get HTTP request for the root of the site / we told it to return the string hello world. If you launch the application and then browse to http://0.0.0.0:3000/ you will see this text.

We can also map other path-es. For example this code:

get '/about' => sub {
    "about me"
}

will map the http://0.0.0.0:3000/about url to return "about me".

Placeholder in the path

Parts of the path can be placeholders starting with a colon :

get '/hello/:name' => sub ($name) {
    "Hello $name!"
};

The :name part can match any string except of a slash / and the value it matched will be assigned to the $name variable of the anonymous subroutine.

You can have more than one such placeholders, and the actual name of the placeholder does not matter. The values caught by the placeholders will be assigned to the parameters of the function in the same order as they've appeared in the url.

get '/hello/:first/:family' => sub ($fname, $lname) {
    "Hello $fname! And hi $lname!"
};

In this example whatever the :first placeholder catches will be assigned to the $fname parameter, and whatever was caught by the :family placeholder, will be assigned to the $lname parameter. For example the url http://0.0.0.0:3000/hello/Foo/Bar will generate the response:

Hello Foo! And hi Bar!

Of course having the same placeholder and the same variable name would probably make the code easier to understand.

This is the full version of this second script:

bailador/second.pl

use v6;
use Bailador;

get '/' => sub {
    "hello world"
}

get '/hello/:first/:family' => sub ($fname, $lname) {
    "Hello $fname! And hi $lname"
};


baile;


The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Email:
Full name:
This is a newsletter temporarily running on my personal site (szabgab.com) using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-01-05



Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.4841720

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: