Frage

I am familiar with using package.json with node.js, Gemfile for Ruby, Podfile for Objective-C, et al.

What is the equivalent file for Perl and what is the syntax used?

I've installed a couple packages using cpanm and would like to save the package names and version in a single file that can be executed by team members.

War es hilfreich?

Lösung

For simple use cases, writing a cpanfile is a good choice. A sample file might look like

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';
};

That is, it's actually a Perl script, not a data format. The dependencies can then be installed like

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

This does not install your module! But it makes sure that all dependencies are satisfied, so we can do:

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

This is usually sufficient e.g. for a git repository which you want others to tinker with.


If you want to create a tarball that can be distributed and installed easily, I'd recommend Dist::Zilla (although it's geared towards CPAN releases). Instead of a cpanfile we use a dist.ini:

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

Then:

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

Dist::Zilla takes care of creating a Makefile.PL and other infrastructure that is needed to install the module.

You can then distribute that tarball, and install it like cpanm Your-Module-1.2.3.tar.gz. Dependencies are resolved, your packages are copied to a permanent location, and you can now use Your::Module in any script without having to specify the location.

Note that you should adhere to the standard directory layout for Perl modules:

./
  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

Andere Tipps

Makefile.PL usually (along with a few other files; Perl has had packages for longer then any of the other languages you mention and suffers from a bit of inelegance here).

Module Starter is a sensible way to start writing a package. It has a getting started guide.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top