Perl6:隐式和显式导入 [英] Perl6: implicit and explicit import

查看:117
本文介绍了Perl6:隐式和显式导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以这样的方式编写模块:当模块使用 d而没有显式导入时,所有子例程都被导入,当它是使用显式导入的 d只有这些显式导入的子程序可用吗?

Is it possible to write a module in a way that when the module is used with no explicit import all subroutines are imported and when it is used with explicit import only theses explicit imported subroutines are available?

#!/usr/bin/env perl6
use v6;
use Bar::Foo;

# all subroutines are imported
sub-one();
sub-two();
sub-three();







#!/usr/bin/env perl6
use v6;
use Bar::Foo :sub-one, :sub-two;

sub-one();
sub-two();
# sub-three not imported


推荐答案

给你的潜艇特殊标签:DEFAULT 以及出口时的专用标签,例如

Give your subs both the special label :DEFAULT as well as a dedicated one when exporting, eg

unit module Bar;
sub one is export(:DEFAULT, :one) { say "one" }
sub two is export(:DEFAULT, :two) { say "two" }

现在,您可以使用普通的使用Bar 导入所有这些,或者可以通过选择特定的一个使用条形:一个;

Now, you can import all of them with a plain use Bar, or can select specific ones via use Bar :one;

这篇关于Perl6:隐式和显式导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆