I have the following project
$ tree
.
├── lib
│ └── MyModule.raku
└── main.raku
$ cat lib/MyModule.raku
use v6;
unit module MyModule;
sub hello { say 'hello' }
$ cat lib/main.raku
use v6;
use MyModule;
MyModule.hello();
I would like to run main.raku using the latest rakudo-star image. However the following occurs
$ docker run -i --rm -u $(id -u) \
--workdir /work \
--volume $PWD:/work \
--entrypoint bash \
rakudo-star perl6 -I ./lib main.raku
===SORRY!===
Could not find MyModule at line 3 in:
file#/work/lib
inst#/.perl6
inst#/usr/share/perl6/site
inst#/usr/share/perl6/vendor
inst#/usr/share/perl6
ap#
nqp#
perl5#
I have also tried inserting use lib '/work/lib' before use MyModule in main.raku with the same result.
There are several problems.
Modules don't end with
.raku. They end with.rakumodor.pm6(for now).(Technically after you install, it doesn't actually matter what the extension is as long as you have it properly declared in
META6.json.)Subroutines are lexically scoped by default (
my), and are also not exported by default.So there is no way to access
hello()outside of the module it is defined in.Modules don't have methods, so you can't call
helloas a method.Even if they did have methods they wouldn't start with the
subkeyword.You can globally scope the subroutine with
our:lib/MyModule.rakumodmain.rakuYou could export it instead:
lib/MyModule.rakumodmain.rakuIn addition to
is export, there are other more fine grained ways to export.I would recommend that if you are going to export, that you also make it global with
our. That way if someone uses your module, but doesn't want to import your subs; they still have access to them.