安装缺少的 Perl 模块的最简单方法是什么?

I get this error:

Can't locate Foo.pm in @INC

Is there an easier way to install it than downloading, untarring, making, etc?

转载于:https://stackoverflow.com/questions/65865/whats-the-easiest-way-to-install-a-missing-perl-module

csdnceshi75
衫裤跑路 If the module in question is a core module, consult How to fix “Can't locate Archive/Tar.pm”.
接近 3 年之前 回复

23个回答

On Unix:

usually you start cpan in your shell:

# cpan

and type

install Chocolate::Belgian

or in short form:

cpan Chocolate::Belgian

On Windows:

If you're using ActivePerl on Windows, the PPM (Perl Package Manager) has much of the same functionality as CPAN.pm.

Example:

# ppm
ppm> search net-smtp
ppm> install Net-SMTP-Multipart

see How do I install Perl modules? in the CPAN FAQ

Many distributions ship a lot of perl modules as packages.

  • Gentoo: category dev-perl
  • Debian: apt-cache search 'perl$'

You should always prefer them as you benefit from automatic (security) updates and the ease of removal. This can be pretty tricky with the cpan tool itself.

For Gentoo there's a nice tool called g-cpan which builds/installs the module from CPAN and creates a Gentoo package (ebuild) for you.

weixin_41568174
from.. For UNIX and MacOS, need to execute as root or with sudo. Otherwise, there will be a permission error.
大约 3 年之前 回复
csdnceshi72
谁还没个明天 bash: cpan: command not found
3 年多之前 回复
csdnceshi74
7*4 ppm uses hyphens - because you specify the distribution that you want to install, which is a lot more honest. For instance, if you use cpan to install LWP, LWP::Simple or LWP::UserAgent it will install the distribution libwww-perl for you. The majority of distributions contain a single module with an equivalent name, but there are several exceptions like that
6 年多之前 回复
csdnceshi63
elliott.david Note that ppm uses dashes for the module path separator not the usual double colons. (Perhaps because colons mean drives in dos?) That has got me a few times ;)
大约 7 年之前 回复
csdnceshi53
Lotus@ For Debian this is a great resource: deb.perl.it/debian/cpan-deb
大约 7 年之前 回复
csdnceshi60
℡Wang Yan Cpan command line also works on cygwin under windows.
7 年多之前 回复
csdnceshi75
衫裤跑路 Just "ppm" opens the GUI, but followed by commands it stays at the command line. So, "ppm help", "ppm install ...", etc.
8 年多之前 回复
csdnceshi76
斗士狗 IIRC the latest ActivePerl no longer has command-line ppm, it now opens a fancy GUI.
接近 12 年之前 回复
csdnceshi52
妄徒之命 The short form is just "cpan Chocolate::Belgian" from the command line :)
接近 12 年之前 回复

A couple of people mentioned the cpan utility, but it's more than just starting a shell. Just give it the modules that you want to install and let it do it's work.

$prompt> cpan Foo::Bar

If you don't give it any arguments it starts the CPAN.pm shell. This works on Unix, Mac, and should be just fine on Windows (especially Strawberry Perl).

There are several other things that you can do with the cpan tool as well. Here's a summary of the current features (which might be newer than the one that comes with CPAN.pm and 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.

Otto made a good suggestion. This works for Debian too, as well as any other Debian derivative. The missing piece is what to do when apt-cache search doesn't find something.

$ sudo apt-get install dh-make-perl build-essential apt-file
$ sudo apt-file update

Then whenever you have a random module you wish to install:

$ cd ~/some/path
$ dh-make-perl --build --cpan Some::Random::Module
$ sudo dpkg -i libsome-random-module-perl-0.01-1_i386.deb

This will give you a deb package that you can install to get Some::Random::Module. One of the big benefits here is man pages and sample scripts in addition to the module itself will be placed in your distro's location of choice. If the distro ever comes out with an official package for a newer version of Some::Random::Module, it will automatically be installed when you apt-get upgrade.

On ubuntu most perl modules are already packaged, so installing is much faster than most other systems which have to compile.

To install Foo::Bar at a commmand prompt for example usually you just do:

sudo apt-get install libfoo-bar-perl

Sadly not all modules follow that naming convention.

On Windows with the ActiveState distribution of Perl, use the ppm command.

2 ways that I know of :

USING PPM :

With Windows (ActivePerl) I've used ppm

from the command line type ppm. At the ppm prompt ...

ppm> install foo

or

ppm> search foo

to get a list of foo modules available. Type help for all the commands

USING CPAN :

you can also use CPAN like this (*nix systems) :

perl -MCPAN -e 'shell'

gets you a prompt

cpan>

at the prompt ...

cpan> install foo  (again to install the foo module)

type h to get a list of commands for cpan

csdnceshi55
~Onlooker You can also just say "$ cpan Foo::Bar" to install directly from the command line. No fancy one-liner or CPAN shell necessary. :)
接近 12 年之前 回复

sudo perl -MCPAN -e 'install Foo'

Lots of recommendation for CPAN.pm, which is great, but if you're using Perl 5.10 then you've also got access to CPANPLUS.pm which is like CPAN.pm but better.

And, of course, it's available on CPAN for people still using older versions of Perl. Why not try:

$ cpan CPANPLUS
csdnceshi74
7*4 Heh, that's cute. Use CPAN.pm to replace itself with CPANPLUS. :)
接近 12 年之前 回复

Use cpan command as cpan Modulename

$ cpan HTML::Parser

To install dependencies automatically follow the below

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

I prefer App::cpanminus, it installs dependencies automatically. Just do

$ cpanm HTML::Parser

Many times it does happen that cpan install command fails with the message like "make test had returned bad status, won't install without force"

In that case following is the way to install the module:

perl -MCPAN -e "CPAN::Shell->force(qw(install Foo::Bar));"
csdnceshi51
旧行李 Unfortunately this yields: Can't locate CPAN.pm in @INC (you may need to install the CPAN module) (@INC contains: /etc/perl
3 年多之前 回复
共23条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐