πŸ“˜ The require keyword to use a module in Perl 6

πŸ“˜ The require keyword to use a module in Raku

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


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 Perl to pack all the arguments into a single array so that we can call the sub as sum(1, 2, 3). With no *, a syntax error will occur, as the sub expects an array but not three scalars.)

Now, require the module and use its sub.

require Math; 

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Retype the CAPTCHA code from the image
Change the CAPTCHA codeSpeak the CAPTCHA code