Raku Books / Perl 6 at a Glance / Code Organization

Modules

Basically, the Perl 6 modules are the files on disk containing the Perl 6 code. Modules are kept in files with the .pm extension. The disk hierarchy reflects the namespace enclosure, which means that a module named X::Y corresponds to the file X/Y.pm, which will be searched for in one of the predefined catalogues or in the location specified by the -I command line option. Perl 6 has more sophisticated rules for where and how to search for the real files (e. g., it can distinguish between different versions of the same module), but let us skip that for now.

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()";
    }
}

CodeBlockPlaceholder3raku-static unit module X;

sub x() is export { say “X::x()”; } CodeBlockPlaceholder4raku-static unit module Greet;

sub hey($name) is export { say “Hey, $name!”; } CodeBlockPlaceholder5

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!";
    }
}

CodeBlockPlaceholder8raku-static use Greet; use Greet::Polite;

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

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);

↻ Runs in Rakudo; the in-browser engine (Raku++) doesn’t support this program yet — see the expected output above.

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

↻ Runs in Rakudo; the in-browser engine (Raku++) doesn’t support this program yet — see the expected output above.

CodeBlockPlaceholder12raku-static unit module N;

our sub n() { say “N::n()”; } CodeBlockPlaceholder13raku-static need N;

N::n(); CodeBlockPlaceholder14raku-static unit module Math;

our sub sum(*@a) { return [+] @a; } CodeBlockPlaceholder15raku-static require Math;

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

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.

Also in this section

Course navigation

Sub overloading with subtypes   |   Import summary