What could be the reason that `require` doesn't work in some places?

$ cat XXX.pm6
unit class XXX;

$ cat ZZZ.pm6
module ZZZ {
    require XXX;
    XXX.new;
    say "OK";
}

$ perl6 -I. -e 'use ZZZ;'
===SORRY!===
You cannot create an instance of this type (XXX)

From the documentation:

require loads a compunit and imports definite symbols at runtime.

You are doing a runtime load of a module while also expecting the symbols for that module to exist at compile time. Instead you should use indirect name lookup (as shown at the bottom of the documentation page linked earlier):

$ cat XXX.pm6
unit class XXX;

$ cat ZZZ.pm6
module ZZZ {
    require XXX;
    ::("XXX").new;
    say "OK";
}

$ perl6 -I. -e 'use ZZZ;'
OK

I think it's because require is a runtime load whereas use is compile time.

Generally I'd use use unless I have a need for dynamic module loading at runtime.


use loads and imports a module at compile-time while require only loads a module at runtime.

Since the namespace is checked at compile-time, you can't just access it as you would a module that is loaded and imported with use.

You can get around that by symbolic references, but you can also capture the namespace into a variable.
(Assuming there is only a single namespace in the module, and it is the same as the name used to load it)

Foo.pm6:

unit module Foo;

sub fubar () { say 'fubar' }
my \Foo = do require Foo;
Foo::fubar(); # fubar␤

(Note that the name of the variable doesn't have to be the same.)

Tags:

Raku

Require