Modules in Perl 6 serve the purpose of keeping code in separate files. It may be a simple library consisting of a couple of functions developed by you, or it may be a big collection of classes that is developed by an external company. In any case, if you use a module, you get the power of the previous work and have an interface to reach that functionality.
In this chapter, we will talk about organizing code in modules and using them in a program.
Let us create our first module and let's take the simple task of adding numbers that we were exploiting in earlier chapters, for example, in Chapter 2, Writing Code.
So, we have an add function for adding up two numbers and the code that uses it:
sub add($a, $b) {
return $a + $b;
}
my $a = 10;
my $b = 20;
my $sum = add($a, $b);
say $sum; # 30
Our current goal is to put the code of the add function into a separate...