๐Ÿ“˜ Using a module in Perl 6

๐Ÿ“˜ Using a module in Raku

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


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 Greet;ย 

hey("you"); # Hey, you!

Module names can be more complicated. With is export, all the exported names will be available in the current scope after the module is used.

In the following example, the module Greet::Polite sits in the Greet/Polite.pm file.

module Greet::Polite {
ย ย ย  sub hello($name) is export {
ย ย ย ย ย ย ย  say "Hello, $name!";
ย ย ย  }
}

The programme uses both of these modules and can access all the exported subs.

use Greet;
use Greet::Polite;ย 

hey("you");ย ย ย  ย # a sub from Greet
hello("Mr. X"); # from Greet::Polite

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