📘 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 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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s