Is it possible to append subroutines to a Raku module at runtime?
Asked Answered
A

1

8

I would like to be able to add a sub to a module Foo at runtime.

In Perl, I would do something like:

*{'My::Module::foo'} = \sub { 'FOO!' };

I know Raku doesn't have TypeGlobbing like Perl. Ideally it would be something like:

use MONKEY;

module Foo {};

Foo.^add-sub('foo', sub { 'FOO!' });

Is this possible?

Adularia answered 17/3, 2024 at 15:15 Comment(0)
E
7
module Foo {}
Foo::<&added-sub> = sub { 99 }
say Foo::added-sub; # 99

See also:

Excurrent answered 17/3, 2024 at 19:14 Comment(3)
nanswer? (spelling...).Hermaphroditus
@jubiliatious1 A magic spello. 😊 I write "nanswer" to refer to any SO answer I write that is an answer in at least some senses (in particular, that I've posted it using SO's answer feature) but arguably not an answer in some other senses. (The word "nanswer" is a play on the IEEE float value representing what normally is a number when in fact it's not a number, aka NaN.) If someone assumes it's a spello, that's OK. If you click the link you'll see it's an example of what I do if I decide to refer to an answer as a nanswer; I explain why I think it is arguably not an answer.Excurrent
But you wrote "nanwser" ( not "nanswer"). Presuming you want last four letters "SWER" ??Hermaphroditus

© 2022 - 2025 — McMap. All rights reserved.