Day 12: Modules and Exporting


Today I’d like to talk about a fairly fundamental subject: libraries.

To write a library in Perl 6, we use the “module” keyword:

module Fancy::Utilities {
    sub lolgreet($who) {
        say "O HAI " ~ uc $who;

Put that in Fancy/ somewhere in $PERL6LIB and we can use it like the following:

use Fancy::Utilities;

That’s hardly ideal.  Just like in Perl 5, we can indicate that some symbols from the module should be made available in the lexical scope of the code loading the module.  We’ve got a rather different syntax for it, though:

module Fancy::Utilities {
  sub lolgreet($who) is export {
    say "O HAI " ~ uc $who;

use Fancy::Utilities;

If you don’t specify further, symbols marked “is export” are exported by default.  We can also choose to label symbols as being exported as part of a different named group:

module Fancy::Utilities {
 sub lolgreet($who) is export(:lolcat, :greet) {
  say "O HAI " ~ uc $who;
 sub nicegreet($who) is export(:greet, :DEFAULT) {
  say "Good morning, $who!"; # Always morning?
 sub shortgreet is export(:greet) {
  say "Hi!";
 sub lolrequest($item) is export(:lolcat) {
  say "I CAN HAZ A {uc $item}?";

Those tags can be referenced in the code loading this module to choose which symbols to import:

use Fancy::Utilities; # Just get the DEFAULTs
use Fancy::Utilities :greet, :lolcat;
use Fancy::Utilities :ALL; # Everything marked is export

Multi subs are export by default, so you only need to label them if you want to change that.

multi sub greet(Str $who) { say "Good morning, $who!" }
multi sub greet() { say "Hi!" }
multi sub greet(Lolcat $who) { say "O HAI " ~ $ }

Classes are just a specialization of modules, so you can export things from them as well.  In addition, you can export a method to make it available as a multi sub.  For example, the setting exports the “close” method from the IO class so you can call “close($fh);”

class IO {
    method close() is export {

Perl 6 does support importing symbols by name from a library, but Rakudo does not yet implement it.

4 Responses to “Day 12: Modules and Exporting”

  1. bened Says:

    Finally no “use Exporter … blablabla …” anymore! I still don’t have this stuff memorized after several years of Perl 5 Programming… Maybe that’s a good rule when developing the syntax of a feature of a programming language: It’s not good if you haven’t memorized it after using it a few times.

  2. Gorm Says:

    Interesting. Some quick questions:

    Is it possible to explicitly specify none of the methods for export (maybe you are trying to minimize namespace pollution)?

    Can the ‘use’ statement export methods of the module being used by name regardless of tags?

    Lastly, for classes, to make use of export methods, that means the programmer has to call ‘use <classname>;’ at some point right? Or maybe export happens when the class is instantiated (yikes!)?

  3. Sid Burn Says:

    Can i don’t choose the name under which a function gets imported? Like Sub::Exporter under Perl 5 does provide it?

    use Module format => { -as => ‘module_format’ }

    The shown is just good for the module writers but for the user that uses the module there is no benefit. If you have different modules that all want to import “format” then you have the same problem like in Perl 5. Calling it with the module name or doing the importing yourself because the language is not flexible enough.

  4. dwarring Says:

    > Multi subs are export by default
    You now need to declare and export a proto.

    proto greet is export {*};
    multi sub greet(Str $who) { return “Good morning, $who!” }
    multi sub greet() { return “Hi!” }

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s


Get every new post delivered to your Inbox.

Join 50 other followers

%d bloggers like this: