是否可以在运行时将子例程附加到 Raku 模块?

问题描述 投票:0回答:1

我希望能够在运行时将

sub
添加到模块
Foo

在 Perl 中,我会做类似的事情:

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

我知道 Raku 没有像 Perl 那样的 TypeGlobbing。理想情况下,它会是这样的:

use MONKEY;

module Foo {};

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

这可能吗?

metaprogramming raku
1个回答
0
投票

这有效:

module foo { our sub bar { 42 } }
import foo;
say foo::bar; # 42

module bar { foo::<&baz> = sub { 99 } }
say foo::baz; # 99

这可能无法解决您的用例,但我写了另一个答案,可能会提供有关某些可用选项的有用的进一步信息。我还写了一个可能有价值的nanwser(也可能没有价值;在失去互联网连接之前我只有几分钟的时间来写这个答案)。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.