Bailador Application in a module

In the previous examples using Bailador we put all the code in a single file. At one point we moved the HTML template to the views/ subdirectory, but for larger applications and for reusable code, we'll want to be able to put code in separate files. That's what we are going to do now.

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 template

The template does not have anything new. It accepts data in the $h variable and then uses that variable to display the version number and the current time - in seconds since the epoch.


% my ($h) = @_;
<!DOCTYPE html>
    <title>Bailador App</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
    <h1>Bailador App</h1>
      Version <%= $h<version> %> Current time: <%= $h<date> %>

The module

The file contains all the code in a class:

unit class Demo;

It loads Bailador in order to have the Domain Specific Language (DSL) that makes it easy for us to define the routes.

use Bailador;

Most importantly, it contains the routes.


unit class Demo;
use Bailador;
my $version = '0.01';

get '/' => sub {
    template '', { version => $version, date => time }

The script launching the application


use Bailador;
use lib callframe(0).file.IO.dirname ~ '/lib';
use Demo;


The part that might be interesting is this snippet:

use lib callframe(0).file.IO.dirname ~ '/lib';

It calculates the root directory of the project - assuming the file is in root directory - and then adds the /lib subdirectory to the list of places where perl is going to look for extra modules. This makes it possible for it to load the file that can be found in the lib/ subdirectory.

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:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2015-08-01


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.999484

Perl 6 Tricks and Treats newsletter

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