📘 Importing a module in Perl 6

The use keyword automatically imports the names from modules. When a module is defined in the current file in the lexical scope (please note that the module can be declared as local with my module), no import will be done by default. In this case, importing the names should be done explicitly with the import keyword.

my module M {
    sub f($x) is export {
        return $x;
    }
} 

import M; 

say f(42);

The f name will only be available for use after it is imported. Again, only the names marked as is export are exported.

As import happens in the compile-time, the import instruction itself can be located even after some names from the module are used.

my module M {
    sub f($x) is export {
        return $x;
    }
} 

say f(1); # 1
import M;
say f(2); # 2

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