Course of Raku / Advanced / Modules / Modules basics / Exercises / Load at run time
Solution: Load at run time
Here is a possible solution to the task.
Code
The program, require-import.raku:
sub MAIN(Bool :$quiet) {
if $quiet {
say 'Silence.';
}
else {
require Greeting <&hello>;
say hello('Sam');
}
}🦋 You can find both source files in the exercises/advanced/modules-basics/require-at-runtime directory.
Output
$ raku -I. require-import.raku
Hello, Sam!
$ raku -I. require-import.raku --quiet
Silence.Comments
requireloads the module at run time rather than at compile time. On its own it imports nothing, which is why a barehellowould be unknown.The
<&hello>list tellsrequireto import that one symbol, so after the statementhello('Sam')can be called directly, givingHello, Sam!.This is what
requireis for: because it runs at run time, it can sit inside anif. When--quietis given, that branch is skipped and the module is never loaded — something a compile-timeusecould not avoid.