Perl6: implicit and explicit import

╄→尐↘猪︶ㄣ 提交于 2019-12-06 01:37:28

问题


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

回答1:


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" }

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



来源:https://stackoverflow.com/questions/36999241/perl6-implicit-and-explicit-import

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!