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?

1 Answer 1

7
module Foo {}
Foo::<&added-sub> = sub { 99 }
say Foo::added-sub; # 99

See also:

Sign up to request clarification or add additional context in comments.

3 Comments

nanswer? (spelling...).
@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.
But you wrote "nanwser" ( not "nanswer"). Presuming you want last four letters "SWER" ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.