Here is a concise list of the keywords for working with modules. use loads and imports a module at compile time need loads a module at compile time but does not import anything from it import imports the names from the loaded module at compile time require loads a module at runtime without importing the … Continue reading “π Modules import summary in Perl 6”
Category: Chapter 3. Code organisation
π 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 … Continue reading “π Creating a module in Perl 6”
π 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 … Continue reading “π Using a module in Perl 6”
π 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 … Continue reading “π Importing a module in Perl 6”
π 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 … Continue reading “π The require keyword to use a module in Perl 6”