Documentation for syntax
require assembled from the following types:
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 = 'MyModule';require ::();
The symbols provided by the loaded module will not be imported into the current scope. You may use dynamic lookup or dynamic subsets to use them by providing the fully qualified name of a symbol, for instance:
require ::("Test");my = ::("Test::EXPORT::DEFAULT::&ok");mmk('oi‽'); # OUTPUT: «ok 1 - ␤»
The FQN of
Test::EXPORT::DEFAULT::&ok. We are aliasing it to
mmk so that we can use that symbol provided by
Test in the current scope.
To import symbols you must define them at compile time. NOTE:
require is lexically scoped:
sub do-somethingsay ::('MyModule'); # This will NOT contain the MyModule symboldo-something();# &something will not be defined here
MyModule doesn't export
require will fail.
require with compile-time symbol will install a placeholder
package that will be updated to the loaded module, class, or package. Note that the placeholder will be kept, even if require failed to load the module. This means that checking if a module loaded like this is wrong:
# *** WRONG: ***try require Foo;if ::('Foo') ~~ Failure# *** WRONG: ***
As the compile-time installed package causes
::('Foo') to never be a
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
In the current (6.d) version of the language,
required symbols are no longer transitively exposed, which means that you need to import symbols from the module they were originally declared, not from the module where they have been imported.