我用 CPAN 成功安装了一个模块,但是 perl 找不到它.为什么? [英] I installed a module successfully with CPAN, but perl can't find it. Why?

查看:78
本文介绍了我用 CPAN 成功安装了一个模块,但是 perl 找不到它.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了这样的 CPAN 模块:

I installed a CPAN module like this:

cpan Acme

根据输出,安装成功:

Running install for module 'Acme'
...
All tests successful.
Files=2, Tests=3,  0 wallclock secs ( 0.02 usr  0.00 sys +  0.04 cusr  0.00 csys =  0.06 CPU)
Result: PASS
  INGY/Acme-1.11111111111.tar.gz
  /usr/bin/make test -- OK
Running make install
Manifying 1 pod document
Installing /home/foo/perl5/lib/perl5/Acme.pod
Installing /home/foo/perl5/lib/perl5/Acme.pm
Installing /home/foo/perl5/man/man3/Acme.3pm
Appending installation info to /home/foo/perl5/lib/perl5/x86_64-linux-thread-multi/perllocal.pod
  INGY/Acme-1.11111111111.tar.gz
  /usr/bin/make install  -- OK

但是当我尝试使用该模块时,出现错误:

But when I try to use the module, I get an error:

$ perl -MAcme -e1
Can't locate Acme.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).
BEGIN failed--compilation aborted.

为什么 perl 安装成功却找不到模块?我该如何解决这个问题?

Why can't perl find the module even though it was installed successfully? How can I fix this?

推荐答案

模块安装在这里:

/home/foo/perl5/lib/perl5/Acme.pm

但是 perl 在 @INC 中寻找模块,在本例中包含:

But perl looks for modules in @INC, which in this case contains:

/usr/local/lib64/perl5
/usr/local/share/perl5
/usr/lib64/perl5/vendor_perl
/usr/share/perl5/vendor_perl
/usr/lib64/perl5
/usr/share/perl5
.

(. 指的是当前工作目录.)

(. refers to the current working directory.)

由于模块不在 @INC 中,perl 无法在没有帮助的情况下找到它.

Since the module is not in @INC, perl can't find it without some help.

一个常见的原因是配置 CPAN 以引导 local::lib 以便安装模块在您的主目录中,而不是在系统 Perl 目录中.如果您有 CPAN 1.9463 或更高版本,并且您在默认安装路径,第一次运行CPAN会提示:

A common cause is configuring CPAN to bootstrap local::lib so that modules are installed in your home directory instead of in the system Perl directories. If you have CPAN 1.9463 or higher and you don't have write permissions in the default install path, the first time you run CPAN you will be prompted:

Warning: You do not have write permission for Perl library directories.

To install modules, you need to configure a local Perl library directory or
escalate your privileges.  CPAN can help you by bootstrapping the local::lib
module or by configuring itself to use 'sudo' (if available).  You may also
resolve this problem manually if you need to customize your setup.

What approach do you want?  (Choose 'local::lib', 'sudo' or 'manual')
 [local::lib]

如果您选择引导 local::lib(默认),模块将安装在 ~/perl5 中.您可能还会收到类似以下提示:

If you choose to bootstrap local::lib (the default), the module will be installed inside ~/perl5. You may also be prompted something like:

Would you like me to append that to /home/foo/.bashrc now? [yes]

如果您选择 yes(默认),一些变量将被添加到您的 .bashrc(或您的 shell 的等效项)中,这样当您将来运行 CPAN 时,模块将继续安装在您的主目录中:

If you choose yes (the default), some variables will be added to your .bashrc (or the equivalent for your shell) so that when you run CPAN in the future, modules will continue to be installed in your home directory:

PATH="/home/foo/perl5/bin${PATH+:}${PATH}"; export PATH;
PERL5LIB="/home/foo/perl5/lib/perl5${PERL5LIB+:}${PERL5LIB}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/foo/perl5${PERL_LOCAL_LIB_ROOT+:}${PERL_LOCAL_LIB_ROOT}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base "/home/foo/perl5""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/foo/perl5"; export PERL_MM_OPT;

我该如何解决?

如果 CPAN 将上述环境变量添加到您的 .bashrc(或等效的)中,最简单的做法是启动一个新的 shell(或源您的 .bashrc).这将设置 PERL5LIB 以便 perl 可以找到安装在~/perl5.

How can I fix it?

If CPAN added the above environment variables to your .bashrc (or equivalent), the simplest thing to do is start a new shell (or source your .bashrc). This will set PERL5LIB so that perl can find modules installed in ~/perl5.

另一方面,如果您有 sudo 访问权限并且希望 CPAN 在系统 Perl 目录而不是主目录中安装模块,请从 .bashrc 中删除环境变量设置(或等效),启动 CPAN shell,然后运行:

On the other hand, if you have sudo access and you want CPAN to install modules in the system Perl directories instead of in your home directory, delete the environment variable settings from your .bashrc (or equivalent), start the CPAN shell, and run:

o conf init

这将重置 CPAN 配置.如果你想引导 local::lib,你会被再次提示;改为输入sudo".一般来说,我不建议这样做;通常最好使用发行版的包管理器(例如 yum、apt)在系统 Perl 目录中安装模块.

This will reset the CPAN configuration. You will be prompted again if you want to bootstrap local::lib; enter "sudo" instead. In general, I wouldn't recommend doing this; it's usually better to use your distro's package manager (e.g. yum, apt) to install modules in the system Perl directories.

另见:如何在不在@INC 的目录中使用"Perl 模块?

这篇关于我用 CPAN 成功安装了一个模块,但是 perl 找不到它.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆