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

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

问题描述

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

以前为了开发本地Session.pm模块,我用我公司的名字创建了一个本地目录,如:

package Company::Session;

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

但我只是不喜欢这种命名约定.我宁愿将包层次结构命名为更接近代码的功能.但这就是它在 CPAN 上的一般做法......

我觉得我缺少一些基本的东西.我还查看了 Damian 的Perl Best Practices,但我可能没有找对地方...

关于以正确的方式避免包命名空间冲突的任何建议?

更新相关问题:如果包名冲突,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天全站免登陆