📘 Creating a module in Perl 6

The keyword module declares a module. The name of the module is given after the keyword. There are two methods of scoping the module. Either it can be a bare directive in the beginning of a file, or the whole module can be scoped in the code block within the pair of braces.

In the first option, the rest of the file is the module definition (note the presence of the unit keyword).

unit module X; 

sub x() {
    say "X::x()";
}

In the second option, the code looks similar to the way you declare classes (more on classes in Chapter 4).

module X {
    sub x() {
        say "X::x()";
    }
}

📘 Using a module in Perl 6

To use a module in your code, use the keyword use.

An example. Let us first create the module Greet and save it in the file named Greet.pm.

unit module Greet; 

sub hey($name) is export {
    say "Hey, $name!";
}

Then, let us use this module in our programme by saying use Greet.

use Greet; 

hey("you"); # Hey, you!

Module names can be more complicated. With is export, all the exported names will be available in the current scope after the module is used.

In the following example, the module Greet::Polite sits in the Greet/Polite.pm file.

module Greet::Polite {
    sub hello($name) is export {
        say "Hello, $name!";
    }
}

The programme uses both of these modules and can access all the exported subs.

use Greet;
use Greet::Polite; 

hey("you");     # a sub from Greet
hello("Mr. X"); # from Greet::Polite

📘 Importing a module in Perl 6

The use keyword automatically imports the names from modules. When a module is defined in the current file in the lexical scope (please note that the module can be declared as local with my module), no import will be done by default. In this case, importing the names should be done explicitly with the import keyword.

my module M {
    sub f($x) is export {
        return $x;
    }
} 

import M; 

say f(42);

The f name will only be available for use after it is imported. Again, only the names marked as is export are exported.

As import happens in the compile-time, the import instruction itself can be located even after some names from the module are used.

my module M {
    sub f($x) is export {
        return $x;
    }
} 

say f(1); # 1
import M;
say f(2); # 2

📘 The require keyword to use a module in Perl 6

The require keyword loads a module at a runtime unlike the use, which loads it at the compile-time.

For example, here is a module with a single sub, which returns the sum of its arguments.

unit module Math; 

our sub sum(*@a) {
    return [+] @a;
}

(The star in *@a is required to tell Perl to pack all the arguments into a single array so that we can call the sub as sum(1, 2, 3). With no *, a syntax error will occur, as the sub expects an array but not three scalars.)

Now, require the module and use its sub.

require Math; 

say Math::sum(24..42); # 627

Before the import Math instruction, the programme will not be able to call Math::sum() because the name is not yet known. A single import Math; will not help as the import happens at compile-time when the module is not loaded yet.