我得到这个错误:

找不到Foo。pm在@INC

有没有比下载、解压、制作等更简单的方法来安装它?


当前回答

看起来你已经得到了答案,但我想我应该插句话。这是我在Ubuntu(或debian服务器)上的一些脚本中所做的事情。

#!/usr/bin/perl

use warnings;
use strict;

#I've gotten into the habit of setting this on all my scripts, prevents weird path issues if the script is not being run by root
$ENV{'PATH'} = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';

#Fill this with the perl modules required for your project
my @perl = qw(LWP::Simple XML::LibXML MIME::Lite DBI DateTime Config::Tiny Proc::ProcessTable);

chomp(my $curl = `which curl`);

if(!$curl){ system('apt-get install curl -y > /dev/null'); }

chomp(my $cpanm = system('/bin/bash', '-c', 'which cpanm &>/dev/null'));

#installs cpanm if missing
if($cpanm){ system('curl -s -L http://cpanmin.us | perl - --sudo App::cpanminus'); }

#loops through required modules and installs them if missing
foreach my $x (@perl){
    eval "use $x";
    if($@){
        system("cpanm $x");
        eval "use $x";
    }
}

这对我来说很好,也许这里有你可以使用的东西。

其他回答

简单地在shell上执行cpan Foo::Bar就可以达到这个目的。

cpan命令作为cpan Modulename

$ cpan HTML::Parser

要自动安装依赖项,请执行以下操作

$ perl -MCPAN -e shell
cpan[1]>  o conf prerequisites_policy follow
cpan[2]>  o conf commit
exit

我更喜欢App::cpanminus,它会自动安装依赖项。只做

$ cpanm HTML::Parser

安全解决方案

许多回答都提到了cpan实用程序的使用(它使用cpan .pm),而没有提到安全性。CPAN 2.27及更早版本默认配置urllist使用http URL(即http://www.cpan.org/),允许MITM攻击,因此是不安全的。这是用来下载checksum文件的,因此需要将其更改为安全URL(例如https://www.cpan.org/)。

因此,在运行cpan并接受默认配置之后,您需要修改生成的MyConfig。PM文件(输出全路径)如下所示。取代

'urllist' => [q[http://www.cpan.org/]],

by

'urllist' => [q[https://www.cpan.org/]],

注意:https是不够的;你还需要一个你可以信任的网站。所以,如果你想选择任意的镜子,要小心。

然后就可以按常规方式使用cpan了。

我在rt.cpan.org上关于不安全URL的错误报告。

有几个人提到了cpan实用程序,但它不仅仅是启动一个shell。只要给它你想安装的模块,让它做它的工作。

$prompt> cpan Foo::Bar

如果你不给它任何参数,它就会启动CPAN。点壳。这可以在Unix、Mac上工作,在Windows上应该也可以(特别是Strawberry Perl)。

cpan工具还可以做其他一些事情。下面是当前特性的总结(可能比CPAN附带的特性更新)。PM和perl):

-a
Creates the CPAN.pm autobundle with CPAN::Shell->autobundle.

-A module [ module ... ]
Shows the primary maintainers for the specified modules

-C module [ module ... ]
Show the Changes files for the specified modules

-D module [ module ... ]
Show the module details. This prints one line for each out-of-date module (meaning,
modules locally installed but have newer versions on CPAN). Each line has three columns:
module name, local version, and CPAN version.

-L author [ author ... ]
List the modules by the specified authors.

-h
Prints a help message.

-O
Show the out-of-date modules.

-r
Recompiles dynamically loaded modules with CPAN::Shell->recompile.

-v
Print the script version and CPAN.pm version.

在Unix:

通常你在shell中开始cpan:

全面美元

和类型

安装巧克力::比利时

简而言之:

巧克力cpan::比利时

在Windows上:

如果您在Windows上使用ActivePerl, PPM (Perl包管理器)有许多与cpa .pm相同的功能。

例子:

ppm美元 Ppm >搜索net-smtp 安装Net-SMTP-Multipart

参见如何安装Perl模块?在CPAN常见问题解答中

许多发行版以包的形式发布了大量perl模块。

Debian/Ubuntu: apt-cache search 'perl$' Arch Linux: pacman -Ss '^perl-' Gentoo: category dev-perl

您应该总是喜欢它们,因为您可以从自动(安全)更新和易于删除中受益。这对于cpan工具本身来说是相当棘手的。

对于Gentoo,有一个很好的工具叫做g-cpan,它可以从CPAN构建/安装模块,并为你创建一个Gentoo包(ebuild)。