Last active
December 11, 2022 14:46
-
-
Save lucs/672fb1e434cf1bfd3f4bc65fbb233a43 to your computer and use it in GitHub Desktop.
Raku: ♪ USAGE() in a module, yeah ♪
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As explained in this talk by lizmat, | |
<https://www.youtube.com/watch?v=VoxWnNJ0gTI&t=8440s>, to speed up the | |
launch time of a Raku script, one can write all of its MAIN logic in a | |
module, make sure sub MAIN is exported, and create a script that | |
simply "use"s the module (the talk is more general and worth | |
watching). The launch time speedup will occur once the program has | |
been run at least once, since its modules will have been precompiled, | |
as opposed to the script itself. | |
But what if we want to have a custom USAGE() function? We can write | |
one in the script and it wll be invoked (if necessary), but we'd | |
prefer to benefit from having it too precompiled. The solution is to | |
also place it in the module and to export it, in the same way MAIN() | |
is. For example: | |
/tmp/myscript.raku: | |
#!/usr/bin/env raku | |
use lib $?FILE.IO.dirname; | |
use MyModule; | |
/tmp/MyModule.rakumod: | |
proto sub USAGE (|) is export {*} | |
multi sub USAGE { note "Command line arguments: {@*ARGS}" } | |
proto sub MAIN (|) is export {*} | |
multi sub MAIN ($foo) { say "Got foo: $foo" } | |
That's it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment