如何为不与内置或CPAN软件包名称冲突的自定义Perl模块选择软件包名称? [英] How do I choose a package name for a custom Perl module that does not collide with builtin or CPAN packages names?

查看:119
本文介绍了如何为不与内置或CPAN软件包名称冲突的自定义Perl模块选择软件包名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于模块的perldoc ,但是我没有看到有关命名的建议一个程序包,这样它就不会与内置或CPAN模块/程序包名称发生冲突.

过去,为了开发本地Session.pm模块,我使用公司名称创建了本地目录,例如:

package Company::Session;

...和Session.pm将在目录Company/中找到.

但是我不喜欢这种命名约定.我宁愿将包层次结构命名为更接近代码的功能.但这通常是在CPAN上完成的方式...

我觉得我缺少基本的东西.我还查看了达米安(Damian)的 Perl最佳实践,但我可能没有在正确的位置寻找内容.

是否有关于避免包名称空间冲突的正确建议?

具有相关问题的更新:如果 软件包名称冲突,Perl如何选择要使用的软件包?谢谢大家.

解决方案

名称空间Local::已被保留用于此目的.以该前缀开头的模块将不会被CPAN或核心所接受.或者,您可以在顶级名称中使用下划线(例如My_Corp::Session或仅My_Session).带有下划线的所有类别也已保留. (在 perlmodlib 中,在选择模块名称"下对此进行了提及.)

请注意,这两个保留都仅适用于顶级名称.例如,有名为Time::LocalText::CSV_XS的CPAN模块.但是Local::TimeText_CSV::XS是保留名称,不会在CPAN上接受.

在公司命名之后也可以命名模块. (好吧,除非您为某些真正通用的发声公司工作.)使用反向域名可能会过大,除非您打算将模块分发给其他人. (但在这种情况下,您可能应该注册一个正常的模块名称.)

Perl如何解决冲突:

Perl在@INC中的目录中搜索具有指定名称的模块.找到的第一个模块是使用的模块.因此,@INC中的目录顺序决定了将使用哪个模块(如果在不同位置安装了具有相同名称的模块).

perl -V将报告@INC的内容(优先级最高的目录首先列出).但是,也有很多方法可以在运行时操纵@INC.

顺便说一句,Raku可以处理由不同作者同名的多个模块,甚至在一个程序中使用多个.那是一个不同的解决方案.

I have read the perldoc on modules, but I don't see a recommendation on naming a package so it won't collide with builtin or CPAN module/package names.

In the past, to develop a local Session.pm module, I have created a local directory using my company's name, such as:

package Company::Session;

... and Session.pm would be found in directory Company/.

But I'm just not a fan of this naming convention. I would rather name the package hierarchy closer to the functionality of the code. But that's how it's done on CPAN generally...

I feel like I am missing something fundamental. I also looked in Damian's Perl Best Practices but I may not have been looking in the right place...

Any recommendations on avoiding package namespace collisions the right way?

Update w/ Related Question: if there is a package name conflict, how does Perl choose which one to use? Thanks everyone.

解决方案

The namespace Local:: has been reserved for just this purpose. No module that starts with that prefix will be accepted to CPAN or the core. Alternatively, you can use an underscore in the top-level name (like My_Corp::Session or just My_Session). All categories with an underscore have also been reserved. (This is mentioned in perlmodlib, under "Select a name for the module".)

Note that both those reservations apply only to the top-level name. For example, there are CPAN modules named Time::Local and Text::CSV_XS. But Local::Time and Text_CSV::XS are reserved names and would not be accepted on CPAN.

Naming modules after your company is fine too. (Well, unless you work for some really generic sounding company.) Using the reverse domain name is probably overkill, unless you intend to distribute your modules to others. (But in that case, you should probably register a normal module name.)

How Perl resolves a conflict:

Perl searches the directories in @INC for a module with the specified name. The first module found is the one used. So the order of directories in @INC determines which module would be used (if you have modules with the same name installed in different locations).

perl -V will report the contents of @INC (the highest-priority directories are listed first). But there are lots of ways to manipulate @INC at runtime, too.

BTW, Raku can handle multiple modules with the same name by different authors, and even use more than one in a single program. That's a different solution.

这篇关于如何为不与内置或CPAN软件包名称冲突的自定义Perl模块选择软件包名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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