Perl6: loading functions into another namespace

I want to use Perl6 modules to group some functions that I often use. Since these functions are all related to each other, I do not like to add them to the class.

I like the idea usewhere you can choose which functions to import, but I don’t like that the imported functions are then stored in the global namespace.

For example, if I have a file my_util.pm6:

#content of my_util.pm6
unit module my_util;
our sub greet($who) is export(:greet) {
    say $who;
}
sub greet2($who) is export(:greet2) {
    say $who;
}
sub greet3($who) is export(:greet3) {
    say $who;
}

and file test.p6:

#!/usr/bin/perl6
#content of test.p6
use v6.c;
use lib '.';
use my_util :greet2;

greet("Bob");    #should not work (because no namespace given) and also doesn't work
greet2("Bob");   #should not work (because no namespace given) but actually works
greet3("Bob");   #should not work (because no namespace given) and also doesn't work
my_util::greet("Alice");     #works, but should not work (because it is not imported)
my_util::greet2("Alice");    #should work, but doesn't work
my_util::greet3("Alice");    #should not work (because it is not imported) and also doesn't work

I would like to call all functions through my_util::greet(), not just through greet().

greet(), my_util.pm6, , , . , , , (.. )

- , ?

+6
1

...

- .

  • my .

  • our , public symbol table .

  • use .
    "".

  • :: ndash; .. foo::greet greet foo.
    - "".

...

, ... , .

...

sub foo::greet($who) is export(:greet) { say "Hello, $who!" }
# This subroutine is now literally called "foo::greet".

... ( 4 ), " ", , , , :

foo::greet "Sam";          # Could not find symbol '&greet'
::<&foo::greet>( "Sam" );  # Hello, Sam!

, - ...

  • our , , use .
    :
  • , (, ), :
unit module foo;
sub foo-greet($who)  is export(:greet)  { ... }
sub foo-greet2($who) is export(:greet2) { ... }
sub foo-greet3($who) is export(:greet3) { ... }
+5

All Articles