【发布时间】:2014-01-17 06:41:40
【问题描述】:
我熟悉在 node.js 中使用 package.json,在 Ruby 中使用 Gemfile,在 Objective-C 中使用 Podfile,等等。
Perl 的等效文件是什么,使用的语法是什么?
我使用 cpanm 安装了几个包,并希望将包名称和版本保存在一个文件中,以便团队成员执行。
【问题讨论】:
标签: perl cpan package-managers
我熟悉在 node.js 中使用 package.json,在 Ruby 中使用 Gemfile,在 Objective-C 中使用 Podfile,等等。
Perl 的等效文件是什么,使用的语法是什么?
我使用 cpanm 安装了几个包,并希望将包名称和版本保存在一个文件中,以便团队成员执行。
【问题讨论】:
标签: perl cpan package-managers
对于简单的用例,写一个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 和安装模块所需的其他基础设施。
然后您可以分发该压缩包,并像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
【讨论】:
[AutoPrereqs]而不是[Prereqs]。
Makefile.PL 通常(以及一些其他文件;Perl 的软件包比您提到的任何其他语言都更长,并且在这里有点不雅)。
Module Starter 是开始编写包的明智方式。它有一个getting started 指南。
【讨论】: