Being pragmat-ish
The question was raised if one can provide a custom pragma. As it happens, today I needed just that. API::Discord is a bit chatty. It is outputting debug and status info right to the terminal. Since I got the Discord bot now also being an IRC bot, the clutter got a bit much. My first thought was to wrap note
and warn
to filter messages out I don’t want. But there is also $*ERR.print
in the mix. So I went and meta6 --fork-module=API::Discord
.
The goal is to use API::Discord::Debug;
to change the behaviour of the module. That is what a pragma does. It changes the way how the compiler or the runtime work. I also want two subs to make the whole thing .wrap
-able, to allow feeding the debug output into a Supply
.
use v6.d;
my $active = False;
sub debug-print(|c) {
$*ERR.print: |c if $active;
}
sub debug-say(|c) {
$*ERR.say: |c if $active;
}
multi sub EXPORT('FROM-MODULE') {
%(
'&debug-print' => &debug-print,
'&debug-say' => &debug-say,
)
}
multi sub EXPORT() {
$active = True;
%()
}
The trick is to have to multi sub EXPORT
so we can do use API::Discord::Debug
to switch debugging output on and use API::Discord::Debug <FROM-MODULE>
to get two functions exported to be called in the various .rakumod
-files of the distribution.
We can’t really define custom pragmas. But use
is kindly calling a function we can define, to get at least some of the behaviour we need.
-
June 29, 2021 at 13:062021.26 R3 – Rakudo Weekly News