📘 Importing a module in Perl 6

📘 Importing a module in Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.


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

Leave a Reply