raku - 使用模块时如何避免导入默认符号?
问题描述
我将Hash::Merge
以此为例。考虑:
use v6;
use Hash::Merge; # <-- imports all symbols marked with "is export" from Hash::Merge
my %hash1 = a1 => [1, 2, 3], b => "xxx", c => { ca => 1 }, e => 5;
my %hash2 = a1 => [1, 5, 3], b => "yyyy", c => { ca => 5, f => "a" }, d => 4;
my %res = merge-hash(%hash1, %hash2, :no-append-array);
假设我不想在使用模块时污染我的命名空间(这里Hash::Merge
作为例子)。我可以在 Perl 5 中通过指定一个空参数列表来实现这一点use
:
use Hash::Merge (); # <-- No symbols will be imported into the current namespace
然后我会merge-hash
使用它的完全限定名称调用子例程:
Hash::Merge::merge-hash
.
根据这个错误报告,这似乎在 Perl 6 中是不可能的。这是正确的吗?
解决方案
要在不导入的情况下加载模块,请need
改用:
need Hash::Merge;
对于有问题的模块,它没有声明它导出的东西our
,不幸的是,这意味着将其称为:
Hash::Merge::merge-hash(...)
不起作用,因为它没有安装在包中。但是,仍然可以手动从导出中挖掘符号:
need Hash::Merge;
say Hash::Merge::EXPORT::DEFAULT::merge-hash({ a => 1 }, { b => 2 })
而且,为了更方便,它可以别名:
need Hash::Merge;
my constant &merge-hash = &Hash::Merge::EXPORT::DEFAULT::merge-hash;
say merge-hash({ a => 1 }, { b => 2 });
沿线有一个推测的语法use Hash::Merge :MY<&merge-hash>
,它在当前的 Perl 6 版本中没有实现,但可能与constant
这里显示的技巧具有相同的语义。
推荐阅读
- automated-tests - 如何在 Dynamics 365 on-premises 中使用 EasyRepro 自动登录
- python - 在特定坐标上添加图像(OpenCV、matplotlib)
- wpf - 呈现模板时 WPF 等待消息
- arrays - 如何将多个列值转换为配置单元中的行?
- haskell - Haskells 弱头范式
- xamarin.android - 加载此活动时如何在活动中显示加载微调器(Xamarin.Android)?
- ios - 无法提取 x86_64
- javascript - React:如何呈现没有键的列表?
- python - 在 Python 中随时间绘制分类数据
- javascript - 如何在 Angular 8 中加载外部 Javascript 和 HTML 文件