๐Ÿ“˜ Programming for the Internet in Perl 6

The simplest way to build a web server in Perl 6 is to use a PSGI server called Bailador. This is a module that you can find on the official page with the list of Perl 6 modules: modules.perl6.org. If you are using the Rakudo Star distribution, use the panda* command line utility to install the module.

$ panda install Bailador

Bailador copies the interface of the well-known framework Dancer for Perl 5. The name is the same but in Spanish.

Here is the minimal programme that implements the web server.

use Bailador; 

get '/' => sub {
ย ย ย  'Hello, world!'


The programme describes the action, which the server does in response to the request to its home page. The baile (dance in Spanish) method starts the main loop of the PSGI server.

Run the programme:

$ perl6 web1.pl

You will get the output informing you that the server is ready to accept requests.

Entering the development dance floor:
[2016-12-27T20:27:34Z] Started HTTP server.

Open that page in a browser, and you will see the desired output: โ€œHello, world!โ€

The next step is to parse the URL and respond accordingly. Bailador allows extract parameters from the URL with the colon syntax:

get '/:name' => sub ($name) {
ย ย ย  "Hello, $name!"

Please note that you cannot omit the space after the sub keyword. There is an alternative. As the sub is anonymous, you may use the pointy block instead:

get '/:name' => -> $name {
ย  ย  "Hello, $name!"

Add it to the programme, restart the server, and go to, for example, You should get the โ€œHello, abcโ€ output in the browser.

Bailador is happy to accept regexes instead of the fixed URLs. For example, letโ€™s create the URL /square-of/N, where the N can be any non-negative integer.

get / 'square-of/' (<digit>+) / => sub ($n) {
ย ย ย  $n * $n

The regex pattern / ‘square-of/’ (<digit>+) / contains the capturing part, and so the variable $n will be set to the number from the URL. As Bailador reads the address patterns in the order they appear in the file, make sure to put the method above the handler of /name. Now, test how it works at; it should print 25.

It is possible to access some environment variables in the URL handler. Use the request method and take the request.env hash from it, as is demonstrated in the example:

get '/ua' => sub {
ย ย ย  request.env<HTTP_USER_AGENT> ~
ย ย ย  '<br />' ~
ย ย ย  request.env<QUERY_STRING>

The page will now print the user agent name and list the query parameters of the request.

After we generate the output from the Perl code, let us move to using templates. Bailador will search for the template files in the views directory.

Save a simple text template to views/test.tt, and use it in the server like this:

use Bailador;ย 

get '/form' => sub {
ย ย ย  template 'test.tt';


To print something inside the template, pass the data in hash:

get '/form/:name' => sub ($name) {
ย ย ย  template 'name.tt', {name => $name}

You can access the data from a template. It receives the argument containing everything that you just passed.

% my ($params) = @_;ย 
Hi, <%= $params<name> %>!

At the moment of writing this book, panda was about to become outdated, and the new recommended tool will be zef. Please refer to the documentation of your Perl 6 distribution on how to install modules.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s