user266003
user266003

Reputation:

Perl 6 namespace and module system

I'm pretty new to Perl and Perl 6. And I can't get along with Perl 6 module/namespace/package system.

I'm working on a library. Say the name of my company is MyCompany. And the library is the "category" Net. So what should my file hierarchy look like? Should it be lib/MyCompany/Net/MyLibrary.pm? Or lib/Net/MyLibrary.pm? Or something else?

Also, should I wrap my class definition into something like this?

package MyCompany {
  class Net::MyLibrary { 

Upvotes: 5

Views: 452

Answers (2)

SES
SES

Reputation: 870

For the first part, the :: is still the directory separator so it should be clear how packages are set up by looking around on CPAN or Github. This gives classes meaning about what they do and how they are related, though it is subjective. For the second question, you are mixing Perl 5 and Perl6 syntax here. There are two ways to declare classes in Perl6. The first is the familiar way in Perl 5 (except with the much more descriptive class statement);

unit class MyCompany;

unit tells Perl 6 that the class will extend to the end of the file (just like package MyCompany; extends to the end of the file in a Perl 5 module, or until another package .. statement). You can also declare classes and roles with blocks as you did above.

class MyCompany { # so begins the MyCompany class
    method foo {
        return self;
    }

    ## ...other methods
} # the end of MyCompany

# code from other classes...

And everything in the block would belong to class you declared.

Upvotes: 3

Borodin
Borodin

Reputation: 126742

The answer is that it is really up to you. There are no rules about whether the name of a module should include your company name or its category name. You can just call it MyLibrary and it will work fine

If you're writing a large suite of libraries, or you want to fit in with an existing library structure, then you need to establish that structure and follow its rules

If you're concerned about names clashing with existing CPAN modules, then it's a good idea to use a special prefix. Your company name would do, so all your packages will begin with MyCompany::. What you put after that is your choice, but it makes sense to categorise your modules in some way

On the other hand, if you're writing something that you want to upload to CPAN that fits into the schemes there, then you should start your package name with Net::, HTML::, Math:: etc.

So, suppose you want to write a network module for use in programs that may also use some modules from CPAN. Starting the name with MyCompany:: will protect you from clashes with other packages. Then you may want Net:: to keep your own category or network modules. And then you will want a unique name within MyCompany::Net::, like MyModule, but I hope it would be more expressive

Then your package would be MyCompany::Net::MyModule and the source code would be in file MyCompany/Net/MyModule.pm. Note that all segments should start with a capital letter

Upvotes: 2

Related Questions