Using modules
In the previous section, we created a simple module with a single function in it, and used the module in a program. In this section, we will examine other methods of how Perl 6 can load the module and its functions.
There are four keywords that we are going to explore— use
, need
, require
and import
. They all are used in the context of loading modules but behave a bit differently.
Using a module assumes at least two things—first, the module file has to be found and compiled; second, the names from the module (such as subroutines or variables) should become visible to the program.
The need keyword
Theneed
keyword loads a module at compile time but does not import the names from it. Loading a module also means that all the instructions within it will be executed. Also, the BEGIN
block will be run. Let us add a couple of printing instructions to the module and see how it changes the output.
Here is the new module:
unit module Add; say 'Start'; sub add($a, $b) is export { return...