Home > Raku > Being pragmat-ish

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.

Categories: Raku
  1. No comments yet.
  1. June 29, 2021 at 13:06

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: