Documentation for compunit
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 ::();
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 run-time symbol lookup with require, to avoid compile-time# package installation:try require ::('Foo');if ::('Foo') ~~ Failure