In Modules§

See primary documentation in context for EXPORT

You can export arbitrary symbols with an EXPORT sub. EXPORT must return a 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.

# lib/MyModule.rakumod 
 
class MyModule::Class { }
 
sub EXPORT {
    Map.new:
      '$var'      => 'one',
      '@array'    => <one two three>,
      '%hash'     => %one => 'two'three => 'four' ),
      '&doit'     => sub { say 'Greetings from exported sub' },
      'ShortName' => MyModule::Class
}

Which is going to be used from this main file:

# main.raku 
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 ShortName.new# OUTPUT: «MyModule::Class.new␤» 

Please note that EXPORT can't be declared inside a package because it is part of the compunit rather than the package.

Whereas UNIT::EXPORT packages deal with the named parameters passed to use, the EXPORT sub handles positional parameters. If you pass positional parameters to use, they will be passed to EXPORT. If a positional is passed, the module no longer exports default symbols. You may still import them explicitly by passing :DEFAULT to use along with your positional parameters.

# lib/MyModule 
 
class MyModule::Class {}
 
sub EXPORT($short_name?{
    Map.new: 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!" }

Used from this main program

# main.raku 
use lib 'lib';
use MyModule 'foo';
say foo.new(); # OUTPUT: «MyModule::Class.new␤» 
 
always();      # OK   - is imported 
shy();         # FAIL - «shy used at line 8. Did you mean 'say'?» 

You can combine EXPORT with type captures for interesting effect. This example creates a ? postfix which will only work on Cools; please note that, by using $_ as an argument, we don't need to use a variable in the routine body and use just .so, acting by default on the topic variable.

# lib/MakeQuestionable.rakumod 
sub EXPORT(::Questionable{
    my multi postfix:<?>(Questionable $_{ .so };
    Map.new: '&postfix:<?>' => &postfix:<?>,
}

Which is used from here:

use lib 'lib';
use MakeQuestionable Cool;
say ( 0?1?{}?, %=> "b" )? ).join(' '); # OUTPUT: «False True False True␤»