为perl项目定义所需的包和版本



我熟悉将package.json与node.js一起使用,Gemfile用于Ruby,Podfile用于Objective-C等。

Perl的等效文件是什么?使用的语法是什么?

我已经使用cpanm安装了几个包,并希望将包名称和版本保存在一个可以由团队成员执行的文件中。

对于简单的用例,编写cpanfile是一个不错的选择。示例文件可能看起来像

requires 'Marpa::R2', '2.078';
requires 'String::Escape', '2010.002';
requires 'Moo', '1.003001';
requires 'Eval::Closure', '0.11';
on test => sub {
requires 'Test::More', '0.98';
};

也就是说,它实际上是一个Perl脚本,而不是一种数据格式。然后可以像一样安装依赖项

$ cd /path/to/your/module
$ cpanm --installdeps .

这不会安装您的模块!但它确保所有依赖项都得到满足,所以我们可以这样做:

use lib '/path/to/your-module/lib';  # add the location as a module search root
use Your::Module; # works! yay

这通常就足够了,例如,对于您希望其他人修改的git存储库。


如果你想创建一个可以轻松分发和安装的tarball,我建议使用Dist::Zilla(尽管它是针对CPAN版本的)。我们使用dist.ini:而不是cpanfile

name    = Your-Module
version = 1.2.3
author  = Your Self <you@example.com>
license = GPL_3
copyright_holder = Your Self
[@Basic]
[Prereqs]
Marpa::R2       = 2.078
String::Escape  = 2010.002
Moo             = 1.003001
Eval::Closure   = 0.11
[Prereqs / TestRequires]
Test::More      = 0.98

然后:

$ dzil test # sanity checks, and runs your tests
$ dzil build  # creates a tarball

Dist::Zilla负责创建Makefile.PL和安装模块所需的其他基础设施。

然后,您可以分发该tarball,并像cpanm Your-Module-1.2.3.tar.gz一样安装它。依赖关系得到解决,包被复制到一个永久位置,现在您可以在任何脚本中使用use Your::Module,而无需指定位置。

注意,您应该遵守Perl模块的标准目录布局:

./
lib/
Your/
Module.pm    # package Your::Module
Module/
Helper.pm  # package Your::Module::Helper
t/  # tests to verify the module works on the target syste,
foo.t
bar.t
xt/  # optional: Author tests that are not run on installation
baz.t
bin/ # optional: scripts that will later end up in the target system's $PATH
command-line-tool

Makefile.PL通常(以及其他一些文件;Perl的包比您提到的任何其他语言都要长,并且在这里有点不雅)。

Module Starter是开始编写程序包的明智方法。它有一个入门指南。

最新更新