简体   繁体   English

CPAN 在哪里安装模块?

[英]Where does CPAN install modules?

I can't find an authoritative/comprehensive description of where CPAN installs its files.我找不到 CPAN 在哪里安装其文件的权威/全面描述。 I assume there must be a set of rules and that it's not as simple as "XYZ directory" because, for example, multiple users on a Linux box can run CPAN even though there's a single Perl installation and it still somehow works.我假设必须有一组规则,它不像“XYZ 目录”那么简单,因为例如,Linux 机器上的多个用户可以运行 CPAN,即使只有一个 Perl 安装,它仍然以某种方式工作。 So, what are those rules?那么,这些规则是什么?

A second part of this question: The documentation for the PERL5LIB environment variable says that it is "A list of directories in which to look for Perl library files before looking in the standard library and the current directory."这个问题的第二部分: PERL5LIB 环境变量的文档说它是“在查找标准库和当前目录之前查找 Perl 库文件的目录列表。”

I assume that CPAN doesn't install into the standard library location, since presumably that is fixed for a particular Perl version.我假设 CPAN 没有安装到标准库位置,因为这可能是针对特定 Perl 版本修复的。 So maybe CPAN installs into PERL5LIB?那么也许 CPAN 安装到 PERL5LIB 中?

And finally, as I already alluded to, how does CPAN handle the fact that multiple users might be running the same Perl installation?最后,正如我已经提到的,CPAN 如何处理多个用户可能正在运行相同的 Perl 安装这一事实? Sorry if that's a separate question but it seems probably related.对不起,如果这是一个单独的问题,但它似乎可能相关。

Perl specifies three sets of installation locations. Perl 指定了三组安装位置。

  • perl , for modules included with Perl itself. perl ,用于 Perl 本身包含的模块。
  • vendor , for modules installed by the provider of your perl binary. vendor ,用于由perl二进制文件的提供者安装的模块。
  • site , for modules installed using cpan . site ,用于使用cpan安装的模块。

Each of these sets provides installation locations for a number of files types.这些集合中的每一个都提供了多种文件类型的安装位置。

                        Installation location
                        --------------------------------------------------------
Type of file            perl             vendor                 site
----------------------  ---------------  ---------------------  -------------------
Build-specific modules  installarchlib   installvendorarch      installsitearch
Modules                 installprivlib   installvendorlib       installsitelib
Binary programs         installbin       installvendorbin       installsitebin
Other programs          installscript    installvendorscript    installsitescript
man pages for scripts   installman1dir   installvendorman1dir   installsiteman1dir
man pages for modules   installman3dir   installvendorman3dir   installsiteman3dir
html docs for scripts   installhtml1dir  installvendorhtml1dir  installsitehtml1dir
html docs for modules   installhtml3dir  installvendorhtml3dir  installsitehtml3dir

You can obtain the path for any of these locations using the following:您可以使用以下方法获取任何这些位置的路径:

perl -V:{var}        # Substitute `{var}` for the var name.

You can obtain all the paths for these locations using the following:您可以使用以下方法获取这些位置的所有路径:

perl -V:'install.*'

Those are the defaults use by the installers [1] .这些是安装程序[1]使用的默认值。 However, the two most commonly used installers allow the user doing to installation to override any and all of these.但是,两个最常用的安装程序允许用户进行安装以覆盖任何和所有这些。 If a module is installed in a non-standard location, PERL5LIB can be used to let perl know where to find the module.如果模块安装在非标准位置,可以使用PERL5LIBperl知道在哪里可以找到模块。


  1. CPAN doesn't install modules. CPAN 不安装模块。 It's a repository.它是一个存储库。

    cpan doesn't install modules. cpan不安装模块。 cpan download distributions from CPAN and runs the installer provided within, be it Makefile.PL or Build.PL . cpan从 CPAN 下载发行版并运行其中提供的安装程序,无论是Makefile.PL还是Build.PL (Same goes for cpanm and cpanp .) (同样适用于cpanmcpanp 。)

    These scripts mostly use ExtUtils::MakeMaker or Module::Build install the distribution (though other installers exist).这些脚本主要使用ExtUtils::MakeMakerModule::Build安装发行版(尽管存在其他安装程序)。

CPAN doesn't actually install files. CPAN 实际上并不安装文件。 It runs the install script embedded in each distribution, which then performs the actual install.它运行嵌入在每个发行版中的安装脚本,然后执行实际安装。

For distributions using ExtUtils::MakeMaker , the defaults are documented here: https://metacpan.org/pod/ExtUtils::MakeMaker#make-install (and the default value of INSTALLDIRS is site ).对于使用ExtUtils::MakeMaker 的发行版,默认值记录在此处: https : //metacpan.org/pod/ExtUtils::MakeMaker#make-installINSTALLDIRS的默认值是site )。 For Module::Build , see https://metacpan.org/pod/Module::Build#INSTALL-PATHS .对于Module::Build ,请参阅https://metacpan.org/pod/Module::Build#INSTALL-PATHS

When the documentation talks about $Config{foo} or %Config , it means the %Config variable provided by the Config module .当文档谈到$Config{foo}%Config ,它意味着Config 模块提供的%Config变量。 The value of $Config{foo} can also be inspected by running perl -V:foo . $Config{foo}值也可以通过运行perl -V:foo来检查。

(If you think this seems unnecessarily complicated, you're right.) (如果您认为这看起来不必要地复杂,那么您是对的。)

The short version is that perl has multiple "system directories", one of which is for "site specific" modules and thus used as the default installation target.简短版本是 perl 有多个“系统目录”,其中之一用于“站点特定”模块,因此用作默认安装目标。 You are right that this is a single directory (per perl install), which doesn't mesh well with a multi-user system: It is shared across all users, and you need root permissions to install modules (and doing so might upgrade/override modules from system packages, which is a bad idea).您是对的,这是一个单一目录(每个 perl 安装),它与多用户系统不匹配:它在所有用户之间共享,并且您需要 root 权限来安装模块(这样做可能会升级/覆盖系统包中的模块,这是一个坏主意)。

What people do instead is to configure ExtUtils::MakeMaker, Module::Build, etc to install into a user's home directory.人们所做的是配置 ExtUtils::MakeMaker、Module::Build 等以安装到用户的主目录中。 This can be done with environment variables.这可以通过环境变量来完成。 Then they tell perl to add this directory to @INC , so modules can actually be found and loaded.然后他们告诉 perl 将这个目录添加到@INC ,这样模块就可以被实际找到并加载。 This is done with another environment variable, PERL5LIB .这是通过另一个环境变量PERL5LIB ( PERL5LIB doesn't affect installation, it's purely used for loading.) PERL5LIB不影响安装,纯粹用于加载。)

All of the above is automated and encapsulated in local::lib .以上所有内容都是自动化的并封装在local::lib 中 (local::lib can also be used to eg create a per-project module subdirectory.) (local::lib 也可用于例如创建每个项目的模块子目录。)

The CPAN documentation also says: CPAN 文档还说:

As of CPAN 1.9463, if you do not have permission to write the default perl library directories, CPAN's configuration process will ask you whether you want to bootstrap local::lib , which makes keeping a personal perl library directory easy.从 CPAN 1.9463 开始,如果您没有写入默认 perl 库目录的权限,CPAN 的配置过程将询问您是否要引导local::lib ,这使得保持个人 perl 库目录变得容易。


You can sidestep the whole issue by installing a private perl in your home directory (in which case the "system" directory is just another subdirectory under your $HOME and thus isn't shared with anyone and can be written to by you).您可以通过在您的主目录中安装一个私有 perl 来回避整个问题(在这种情况下,“系统”目录只是您$HOME下的另一个子目录,因此不会与任何人共享,您可以写入)。 This is very easy with eg perlbrew .这对于例如perlbrew非常容易。


Another note: You've just found a bug in the documentation for PERL5LIB .另一个注意事项:您刚刚在PERL5LIB的文档中发现了一个错误。 " and the current directory " is outdated: . 和当前目录”已过时: . has been removed from the default list of module locations for security reasons.出于安全原因,已从模块位置的默认列表中删除。

This is a complex question.这是一个复杂的问题。 You can tell where core libraries are located by seeking the place of one of them:您可以通过查找其中一个库的位置来判断核心库所在的位置:

perldoc -l B

Will tell you where the B core module is located.会告诉你B核心模块的位置。 And you may try others with different results...你可能会尝试其他不同的结果......

Also, perl -V will tell you all shell variables that matter along with the value of @INC , the places where it will look for libraries.此外, perl -V会告诉您所有重要的 shell 变量以及@INC的值,它将查找库的位置。

Core libraries are usually found in different places than local libraries.核心库通常位于与本地库不同的地方。 Also, if you are using perlbrew and local::lib you may have more things to consider.此外,如果您使用perlbrewlocal::lib您可能需要考虑更多事情。 Regarding shell variables, along with PERL5LIB , you also have PERL_LOCAL_LIB_ROOT .关于 shell 变量,除了PERL5LIB ,还有PERL_LOCAL_LIB_ROOT

Regarding your other question, I would say that root may install libraries system-wide.关于你的另一个问题,我想说root可以在系统范围内安装库。 Any user will have those and then any local places included by means of shell variables or other means like command line options perl -I <lib location> , or within the code like use lib <lib location>;任何用户都将通过 shell 变量或其他方式(如命令行选项perl -I <lib location>或在代码中(如use lib <lib location>;包含这些和任何本地use lib <lib location>; . .

There is also perlbrew that along with local::lib allows a non-privileged user to install Perl and libraries in local directories.还有perlbrewlocal::lib允许非特权用户在本地目录中安装Perl和库。

Regarding ways to install modules from CPAN, my favourite is cpanminus .关于从 CPAN 安装模块的方法,我最喜欢的是cpanminus It is invoked with cpanm <library to install> .它通过cpanm <library to install>调用。 It never fails...它永远不会失败...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM