6

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
raiph
  • 31,607
  • 3
  • 62
  • 111
sid_com
  • 24,137
  • 26
  • 96
  • 187
  • Do you (@sid_com) understand my queued edit to your question (s/export/import/) as explained in my answer (Export ≠ Import) below? Do you agree with it? – raiph May 08 '16 at 14:33
  • @raiph: For me your edit sounds fine (the fond size is a little irritating). I felt uncomfortable in the first place with the "export" but still used it. – sid_com May 08 '16 at 15:20

1 Answers1

9

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;

Christoph
  • 164,997
  • 36
  • 182
  • 240