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 Perl 5, please check out the Perl 5 tutorial.

The easiest way to get Perl 6 on you system is to install Rakudo Star. For MS Windows an Mac OSX there are binary packages. On Linux you need to compile it yourself, but the instructions are straight forward.

Once you have Rakudo Start on your system make sure you can run perl6 on the command line.

Rakudo Star comes with zef, the command line package installer.

In orer to install Bailador you need to type:

zef install Bailador

Then you can start simple.

Create a 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://127.0.0.1:3000
[2015-01-03T22:39:13Z] Started HTTP server.

In Bailador we need to map HTTP request methods, and the paths on the server to anonymous subroutines that will return 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://127.0.0.1:3000/ you will see this text.

We can also map other paths. For example this code:

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

will map the http://127.0.0.1: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. The 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://127.0.0.1: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 2017-06-01



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: 2.3563113

Perl 6 Tricks and Treats newsletter

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