Fetching contributors…
Cannot retrieve contributors at this time
826 lines (618 sloc) 28.6 KB
=begin pod :tag<perl6>
=TITLE Modules
=SUBTITLE How to create, use and distribute Perl 6 modules
=head1 Creating and using modules
A module is usually a source file or set of source files
N<Technically a module is a set of I<compunits> which are usually files but could come from anywhere as long as there is a I<compunit repository> that can provide it. See L<S11|>.>
expose Perl 6 constructs. These are typically packages
L<roles|/language/objects#Roles>, L<grammars|Grammar>),
L<subroutines|/language/functions>, and sometimes
L<variables|/language/variables>. In Perl 6 I<module> can also refer
to a type of package declared with the C<module> keyword (see
L<Module Packages|/language/module-packages> and the examples
below) but here we mostly mean "module" as a set of source
files in a namespace.
=head2 Looking for and installing modules.
L<C<zef>|> is the application using for installing modules in Perl 6. Modules are listed in L<the Perl 6 ecosystem|> and can be searched there or from the command line using C<zef search>
=for code :lang<shell>
zef search WWW
will return a list of modules that includes WWW in their name, for instance. Then,
=for code :lang<shell>
zef install WWW
will install the module with that particular name, if it is not already installed.
=head2 Basic structure
Module distributions (in the I<set of related source files> sense) in Perl 6
have the same structure as any distribution in the Perl family of languages:
there is a main project directory containing a C<README> and a C<LICENSE> file,
a C<lib> directory for the source files, which may be individually
referred to as modules and/or may themselves define modules with the C<module>
N<As L<synopsis S11|> says: Confusing? Yes it is.>
, a C<t> directory for tests, and possibly a C<bin>
directory for executable programs and scripts.
Source files generally use the C<.pm6> extension, and scripts or
executables use the C<.p6>. Test files use the C<.t> extension. Files which contain documentation use the C<.pod6> extension.
=head2 Loading and basic importing
Loading a module makes the packages in the same namespace declared
within available in the file scope of the loader. Importing from a
module makes the symbols exported available in the lexical scope of
the importing statement.
=head3 C<need>X<|need>
C<need> loads a C<compunit> at compile time.
=for code :skip-test
need MyModule;
Any packages in the namespace defined within will also be available.
=begin code :skip-test
# MyModule.pm6
unit module MyModule;
class MyModule::Class {}
=end code
C<MyModule::Class> will be defined when C<MyModule> is loaded.
=comment class Class { ... } won't get exported automatically on loading...not sure if bug or..
=head3 C<use> X<|use>
C<use> loads and then imports from a compunit at compile time. It will look for
files that end in C<.pm6> (C<.pm> is also supported). See
for where the runtime will look for modules.
=for code :skip-test
use MyModule;
It is equivalent to:
=begin code :allow<L> :skip-test
L<need|/language/modules#need> MyModule;
import MyModule;
=end code
See also L<Selective Importing|/language/modules#Exporting_and_selective_importing>
to restrict what you import.
=head3 C<require>X<|require>
C<require> loads a compunit and imports definite symbols at runtime.
say "loading MyModule";
require MyModule;
The compunit name can be in a runtime variable if you put it inside an
indirect lookup.
my $name = 'MyModule';
require ::($name);
The symbols provided by the loaded module will not be imported into the
current scope. You may use
L<dynamic lookup|/language/packages#index-entry-::()> or
L<dynamic subsets|/language/typesystem#subset> to use them by providing
the fully qualified name of a symbol, for instance:
require ::("Test");
my &mmk = ::("Test::EXPORT::DEFAULT::&ok");
mmk('oi‽'); # OUTPUT: «ok 1 - ␤»
The FQN of C<ok> is C<Test::EXPORT::DEFAULT::&ok>. We are aliasing it to
C<mmk> so that we can use that symbol provided by C<Test> in the current
To import symbols you must define them at compile time. B<NOTE:>
C<require> is lexically scoped:
sub do-something {
require MyModule <&something>;
say ::('MyModule'); # MyModule symbol exists here
something() # &something will be defined here
say ::('MyModule'); # This will NOT contain the MyModule symbol
# &something will not be defined here
If C<MyModule> doesn't export C<&something> then C<require> will fail.
A C<require> with compile-time symbol will install a placeholder
C<package> that will be updated to the loaded module, class, or package.
Note that the placeholder will be kept, B<even if require failed to load
the module.> This means that checking if a module loaded like this is
# *** WRONG: ***
try require Foo;
if ::('Foo') ~~ Failure { say "Failed to load Foo!"; }
# *** WRONG: ***
As the compile-time installed package causes C<::('Foo')> to never be
a C<Failure>. The correct way is:
# Use return value to test whether loading succeeded:
(try require Foo) === Nil and say "Failed to load Foo!";
# Or use a runtime symbol lookup with require, to avoid compile-time
# package installation:
try require ::('Foo');
if ::('Foo') ~~ Failure {
say "Failed to load Foo!";
=head2 Lexical module loading
Perl 6 takes great care to avoid global state, i.e. whatever you do in
your module, it should not affect other code. That's why e.g. subroutine
definitions are lexically (C<my>) scoped by default. If you want others
to see them, you need to explicitly make them our scoped or export them.
Classes are exported by default on the assumption that loading a module
will not be of much use when you cannot access the classes it contains.
This works as advertised with a small but important caveat: those
classes are not only visible in the computation unit that loads the
module, but globally. This means that as soon as some code loads a
module, those classes are immediately visible everywhere.
For example, given a module C<Foo>:
=for code :skip-test
unit class Foo;
use Bar;
And your own program:
=for code :skip-test
use Foo;
my $foo =; # works as expected
my $bar =; # huh!? Where is Bar coming from?
This doesn't sound so bad (it at least saves you some typing), except
for that it makes another feature of Perl 6 impossible to have: the
ability to load multiple versions of a module at the same time in
different parts of your program:
=for code :skip-test
use Baz:ver(v1);
my $old-baz =;
use Baz:ver(v2);
my $shiny-new-baz =;
This will explode as on loading Baz:ver(v2), Rakudo will complain about "Baz"
already being defined.
To fix this, we no longer register loaded classes globally but only in the scope
which loaded them in the first place. Coming back to our first example, we would
need to explicitly load Bar in the main program:
=for code :skip-test
use Foo;
use Bar;
my $foo =; # still works of course
my $bar =; # now it's clear where Bar is coming from
So if you suddenly get an "Undeclared name: Bar" error message after upgrading
to a newer Perl 6 compiler, you will most probably just need to add a: "use
Bar;" to your code.
=head2 Exporting and selective importing
=head3 is export
Packages, subroutines, variables, constants and enums are exported by marking
them with the L<is export> trait (also note the tags available for indicating authors and versions).
=begin code :skip-test
unit module MyModule:ver<1.0.3>:auth<John Hancock (>;
our $var is export = 3;
sub foo is export { ... };
constant FOO is export = "foobar";
enum FooBar is export <one two three>;
# Packages like classes can be exported too
class MyClass is export {};
# If a subpackage is in the namespace of the current package
# it doesn't need to be explicitly exported
class MyModule::MyClass {};
=end code
As with all traits, if applied to a routine, "is export" should appear after
any argument list.
=begin code :skip-test
sub foo(Str $string) is export { ... }
=end code
You can pass named parameters to C<is export> to group symbols for exporting
so that the importer can pick and choose. There are three predefined
=begin code :skip-test
# lib/MyModule.pm6
unit module MyModule;
sub bag is export { ... }
sub pants is export(:MANDATORY) { ... } # objects with tag ':MANDATORY' are always exported
sub sunglasses is export(:day) { ... }
sub torch is export(:night) { ... }
sub underpants is export(:ALL) { ... }
=end code
=begin code :skip-test
# main.p6
use lib 'lib';
use MyModule; # bag, pants
use MyModule :DEFAULT; # the same
use MyModule :day; # pants, sunglasses
use MyModule :night; # pants, torch
use MyModule :ALL; # bag, pants, sunglasses, torch, underpants
=end code
B<Note>: there currently is no way for the user to import a single
object if the module author hasn't made provision for that, and it is
not an easy task at the moment (see
L<RT #127305|>).
One way
the author can provide such access is to give each C<export> trait its
own unique tag. (And the tag can be the object name!) Then the user can
either (1) import all objects:
=begin code :skip-test
use Foo :ALL;
=end code
or (2) import one or more objects selectively:
=begin code :skip-test
use Foo :bar, :s5;
=end code
1. The C<:MANDATORY> tag on an exported sub ensures it will be exported
no matter whether the using program adds any tag or not.
2. All exported subs without an explicit tag are implicitly C<:DEFAULT>.
3. The space after the module name and before the tag is mandatory.
4. Multiple import tags may be used (separated by commas). For example:
=begin code :skip-test
# main.p6
use lib 'lib';
use MyModule :day, :night; # pants, sunglasses, torch
=end code
5. Multiple tags may be used in the C<export> trait, but they must
all be separated by either commas, or whitespace, but not both.
=begin code
sub foo() is export(:foo :s2 :net) {}
sub bar() is export(:bar, :s3, :some) {}
=end code
=head3 UNIT::EXPORT::*
Beneath the surface, C<is export> is adding the symbols to a C<UNIT>
scoped package in the C<EXPORT> namespace. For example, C<is
export(:FOO)> will add the target to the C<UNIT::EXPORT::FOO>
package. This is what Perl 6 is really using to decide what to import.
=begin code :skip-test
unit module MyModule;
sub foo is export { ... }
sub bar is export(:other) { ... }
=end code
Is the same as:
=begin code :skip-test
unit module MyModule;
my package EXPORT::DEFAULT {
our sub foo { ... }
my package EXPORT::other {
our sub bar { ... }
=end code
For most purposes C<is export> is sufficient but the C<EXPORT>
packages are useful when you want to produce the exported symbols
dynamically. For example:
=begin code :skip-test
# lib/MyModule.pm6
unit module MyModule;
my package EXPORT::DEFAULT {
for <zero one two three four>.kv -> $number, $name {
for <sqrt log> -> $func {
OUR::{'&' ~ $func ~ '-of-' ~ $name } := sub { $number."$func"() };
=end code
=begin code :skip-test
# main.p6
use MyModule;
say sqrt-of-four; # OUTPUT: «2␤»
say log-of-zero; # OUTPUT: «-Inf␤»
=end code
=head3 EXPORT
X<|sub EXPORT>
You can export arbitrary symbols with an C<EXPORT> sub. C<EXPORT>
must return a L<Map>, where the keys are the symbol names and
the values are the desired values. The names should include the sigil
(if any) for the associated type.
=begin code :skip-test
# lib/MyModule.pm6
class MyModule::Class { }
sub EXPORT {
'$var' => 'one',
'@array' => <one two three>,
'%hash' => %( one => 'two', three => 'four' ),
'&doit' => sub { say 'Greetings from exported sub' },
'ShortName' => MyModule::Class
=end code
=begin code :skip-test
# main.p6
use lib 'lib';
use MyModule;
say $var; # OUTPUT: «one␤»
say @array; # OUTPUT: «(one two three)␤»
say %hash; # OUTPUT: «{one => two, three => four}␤»
doit(); # OUTPUT: «Greetings from exported sub␤»
say; # OUTPUT: «␤»
=end code
Note, C<EXPORT> can't be declared inside a package because
it is part of the compunit rather than the package.
Whereas C<UNIT::EXPORT> packages deal with the named parameters passed
to C<use>, the C<EXPORT> sub handles positional parameters. If you
pass positional parameters to C<use> they will be passed to
C<EXPORT>. If a positional is passed the module no longer exports
default symbols. You may still import them explicitly by
passing C<:DEFAULT> to C<use> along with your positional parameters.
=begin code
# lib/MyModule
class MyModule::Class {}
sub EXPORT($short_name?) {
do $short_name => MyModule::Class if $short_name
sub always is export(:MANDATORY) { say "works" }
#import with :ALL or :DEFAULT to get
sub shy is export { say "you found me!" }
=end code
=begin code :skip-test
# main.p6
use lib 'lib';
use MyModule 'foo';
say; # OUTPUT: «␤»
always(); # OK - is imported
shy(); # FAIL - won't be imported
=end code
You can combine C<EXPORT> with type captures for interesting
effect. This example creates a C<?> postfix which will only work on
=begin code :skip-test
# lib/MakeQuestionable.pm6
sub EXPORT(::Questionable) {
my multi postfix:<?>(Questionable $_) { .so };
'&postfix:<?>' => &postfix:<?>,
=end code
=begin code :skip-test
use MakeQuestionable Cool;
say ( 0?, 1?, {}?, %( a => "b" )? ).join(' '); # OUTPUT: «False True False True␤»
=end code
=head2 Introspection
To list exported symbols of a module first query the export tags supported by
the module.
use URI::Escape;
say URI::Escape::EXPORT::.keys;
Then use the tag you like and pick the symbol by its name.
say URI::Escape::EXPORT::DEFAULT::.keys;
# OUTPUT: «(&uri-escape &uri-unescape &uri_escape &uri_unescape)␤»
my &escape-uri = URI::Escape::EXPORT::DEFAULT::<&uri_escape>;
Be careful I<not> to put C<sub EXPORT> after L«C<unit> declarator|/syntax/unit».
If you do so, it'll become just a sub inside your package, rather than the special
export sub:
=begin code :skip-test<unit module can't work here>
unit module Bar;
sub EXPORT { %(Foo => &say) } # WRONG!!! Sub is scoped wrong
# ---------
sub EXPORT { %(Foo => &say) } # RIGHT!!! Sub is outside the module
unit module Bar;
=end code
=head2 Finding modules
It is up to the module installer to know where C<compunit> expects modules to
be placed. There will be a location provided by the distribution and in the
current home directory. In any case, letting the module installer deal with
your modules is a safe bet.
=begin code :skip-test<io>
cd your-module-dir
zef --force install .
=end code
X<|use lib>
A user may have a collection of modules not found in the normal ecosystem,
maintained by a module or package manager, but needed regularly. Instead of
using the C<use lib> pragma one can use the C<PERL6LIB> environment variable to
point to module locations. For example:
=for code :lang<shell>
export PERL6LIB=/path/to/my-modules,/path/to/more/modules
Note that the comma (',') is used as the directory separator.
The include path will be searched recursively for any modules when Rakudo is
started. Directories that start with a dot are ignored and symlinks are
=head1 Distributing modules
If you've written a Perl 6 module and would like to share it with the
community, we'd be delighted to have it listed in the L<Perl 6 modules directory|>. C<:)>
Currently there are two different module ecosystems (module distribution
networks) available:
=item B<CPAN>
This is the same ecosystem Perl5 is using. Modules are uploaded as I<.zip>
or I<.tar.gz> files on L<PAUSE|>.
=item B<p6c>
Up until recently the only ecosystem. It is based
on Github repositories which are directly accessed. It has only limited
capability for versioning.
The process of sharing your module consists of two steps, preparing the module
and uploading the module to one of the ecosystems.
=head2 Preparing the module
For a module to work in any of the ecosystems, it needs to follow a certain
structure. Here is how to do that:
=item Create a project directory named after your module. For
example, if your module is C<Vortex::TotalPerspective>, then create a
project directory named C<Vortex-TotalPerspective>.
=begin item
Make your project directory look like this:
=begin code :lang<text>
├── lib
│ └── Vortex
│ └── TotalPerspective.pm6
├── META6.json
└── t
└── basic.t
=end code
If your project contains other modules that help the main module do
its job, they should go in your lib directory like so:
=begin code :lang<text>
└── Vortex
├── TotalPerspective.pm6
└── TotalPerspective
├── FairyCake.pm6
└── Gargravarr.pm6
=end code
=end item
=item If you have any additional files (such as templates or a dynamic
library) that you wish to have installed so you can access them at
runtime, they should be placed in a C<resources> sub-directory of your project.
=item The C<> file is a L<markdown-formatted|>
text file, which will later be automatically rendered as HTML by GitHub.
=item Regarding the C<LICENSE> file, if you have no other preference,
you might just use the same one that Rakudo Perl 6 uses. Just
copy/paste the raw form of L<its license|>
into your own C<LICENSE> file.
=item The license field in META6.json
The license field should be one of the standardized names listed here:
L<>, in the case of the B<Artistic 2.0> license which
is what many of our ecosystem modules use, its identifier is
C<Artistic-2.0>. Having standardized identifiers make it easy for humans
and computers alike to know which license was actually used by looking at
the metadata!
=item I<Your license is not on or you have your own license>:
If you can't find your license on C<> or you use your own license,
you should put the licenses name in the license field, and under
more details here L<>.
=item If you don't yet have any tests, you can leave out the C<t>
directory and C<basic.t> file for now. For more info on how to write
tests (for now), you might have a look at how other modules use
=begin item
To document your modules, use L<Perl 6 Pod |> markup inside your modules. Module documentation is most appreciated and will be especially important once the Perl 6 module directory (or some other site) begins rendering Pod docs as HTML for easy browsing. If you have extra docs (in addition to the Pod docs in your module(s)), create a C<doc> directory for them. Follow the same folder structure as the C<lib> directory like so:
=begin code :lang<text>
└── Vortex
└── TotalPerspective.pod6
=end code
=end item
Note, described above is a minimal project directory. If your project
contains scripts that you'd like distributed along with your module(s),
put them in a C<bin> directory. If you'd like a graphical logo to
appear next to your module at the module directory, create a
C<logotype> directory and put into it a C<logo_32x32.png> file. At some
point, you might also consider adding C<CONTRIBUTORS>, C<NEWS>,
C<TODO>, or other files.
=begin item
Make your X<C<META6.json>|META6.json> file look something like this:
=begin code :allow<R> :lang<javascript>
"perl" : "6.c",
"name" : "Vortex::TotalPerspective",
"auth" : "github:SomeAuthor",
"api" : "1",
"version" : "0.0.1",
"description" : "Wonderful simulation to get some perspective.",
"authors" : [ "R<Your Name>" ],
"license" : "Artistic-2.0",
"provides" : {
"Vortex::TotalPerspective" : "lib/Vortex/TotalPerspective.pm6"
"depends" : [ ],
"build-depends" : [ ],
"test-depends" : [ ],
"resources" : [ ],
"tags": [
"Vortex", "Total", "Perspective"
"source-url" : "git://<you>/Vortex-TotalPerspective.git"
=end code
For choosing a version numbering scheme, try and use
"major.minor.patch" (see L<the spec on versioning |> for further
details). This will go into the C<version> key of C<META6.json>.
The C<name> key is compulsory, and C<zef> will fail if you do not include it. Even if you have created a META6.json file just to express the dependencies for a series of scripts, this section must be included.
The C<auth> section identifies the author in GitHub or other repository hosting site, such as Bitbucket.
The C<authors> section includes a list of all the module authors. In
the case there is only one author, a single element list must be
In the C<provides> section, include all the namespaces provided by
your distribution and that you wish to be installed, only module
files that are explicitly included here will be installed and
available with C<use> or C<require> in other programs.
Set C<perl> version to the minimum perl version your module works with.
Optionally, you can set an C<api> field. Incrementing this indicates that
the interface provided by your module is not backwards compatible with a
previous version. You can use it if you want to adhere to L<Semantic Versioning |>. Simply keep the C<api> field to the same value as
your major version number. A dependency can then depend on your module by
including a C<:api> part, which will ensure backwards incompatible releases
will not be pulled in.
The C<resources> section is optional, but, if present, should contain a
list of the files in your C<resources> directory that you wish to be
installed. These will be installed with hashed names alongside your
library files and their installed location can be determined through the
C<%?RESOURCES> C<Hash> indexed on the name provided. C<tags> is also optional, but is used to describe the module in the Perl 6 ecosystem.
C<depends>, C<build-depends> and C<test-depends> include different modules that are used in those phases of the of installation. The last two are optional, but convenient.
Finally, C<source-url> indicates the URL of the repository where the module is developed.
The L<Test::META module |>
can help you check the correctness of the META6.json file.
There are more fields described in the L<META design documents |>, but not all of these are
implemented by existing package managers. Hence you should stick to the
fields described in the above example block to ensure compatibility with
existing package managers. You can also check L<Moritz Lenz's repository of all modules for examples|>.
=end item
=begin item
If you want to test your module you can use the following command to install the
module directly from the module folder you just created.
=begin code :lang<shell>
zef install ./your-module-folder
=end code
Note that doing so precompiles and installs your module. If you make changes to
the source, you'll need to re-install the module. (see C<use lib> pragma,
C<-I> command line switch, or C<PERL6LIB> env variable, to include a path
to your module source while developing it, so you don't have to install it at all).
=end item
=head2 Upload your module to CPAN
Uploading a module to CPAN is the preferred way of distributing Perl 6 modules.
it requires having a L<PAUSE|>
user account. If you don't have an account already go there and apply for an
account. The process takes about 5 minutes and some e-mail back and forth.
=begin item
Create a package of your module:
=begin code :lang<shell>
cd your-module-folder
tar czf Vortex-TotalPerspective-0.0.1.tar.gz --transform s/^\./Vortex-TotalPerspective-0.0.1/ --exclude-vcs --exclude=.[^/]*
=end code
If you use git you can also use the following command to create a package directly for a given commit.
=begin code :lang<shell>
git archive --prefix=Vortex-TotalPerspective-0.0.1/ -o ../Vortex-TotalPerspective-0.0.1.tar.gz HEAD
=end code
=end item
=item Go to L<PAUSE|>, log in and click on
L<Upload a file to CPAN|>.
=item Make sure you select C<Perl6> as the I<Target Directory>. For
your first upload, you have to enter the string C<Perl6> in the text field.
On subsequent uploads, you can select the C<Perl6> directory from select
box above the input field.
=begin item
Select your file and click I<Upload>!
I<Make sure you have a META6.json file in your dist and that the dist
version you're uploading is higher than the currently uploaded version.
Those are the most common upload errors.>
=end item
=head2 Upload your module to p6c
If you want to use the I<p6c> ecosystem you need to use git for your module's
version control. The instructions herein assume that you have a
L<GitHub|> account so that your module can be shared from its
GitHub repository, however another provider such as
L<GitLab|> should work as long as it works in a
similar way.
=item Put your project under git version control if you haven't done so
=item Once you're happy with your project, create a repository for it on GitHub.
See L<GitHub's help docs|> if necessary. Your
GitHub repository should be named the same as your project directory. Immediately
after creating the GitHub repo, GitHub shows you how to configure your
local repository to know about your GitHub repository.
=item Push your project to GitHub.
=item Consider setting up automated testing (see L<>).
=item Create a PR on L<ecosystem|> adding
your module to META.list, or ping someone on IRC (#perl6 at freenode) to
get help having it added.
=item After the pull request has been accepted, wait for an hour. If
your module doesn't show up on L<>, please
view the log file at L<> to see
if it identifies an error with your module or meta file.
B<That's it! Thanks for contributing to the Perl 6 community!>
If you'd like to try out installing your module, use the X<zef> module
installer tool which is included with Rakudo Star Perl 6:
=begin code :lang<shell>
zef install Vortex::TotalPerspective
=end code
This will download your module to its own working directory (C<~/.zef>),
build it there, and install the module into your local Perl 6 installation directory.
To use C<Vortex::TotalPerspective> from your scripts, just write
C<use Vortex::TotalPerspective>, and your Perl 6 implementation will
know where to look for the module file(s).
=head1 Modules and tools related to module authoring
You can find a list of modules and tools that aim to improve the experience of
writing/test modules at L<Modules Extra|/language/modules-extra>
=head2 Contact information
To discuss module development in general, or if your module would
fill a need in the ecosystem, naming, etc., you can use the
L<#perl6 on|irc://> IRC channel.
To discuss toolchain specific questions, you can use the
L<#perl6-toolchain on|irc://>
IRC channel. A repository to discuss tooling issues is also available at
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6