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