Where does CPAN install modules?

左心房为你撑大大i 提交于 2020-07-20 07:05:06

问题


I can't find an authoritative/comprehensive description of where CPAN installs its files. 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. 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."

I assume that CPAN doesn't install into the standard library location, since presumably that is fixed for a particular Perl version. So maybe CPAN installs into PERL5LIB?

And finally, as I already alluded to, how does CPAN handle the fact that multiple users might be running the same Perl installation? Sorry if that's a separate question but it seems probably related.


回答1:


Perl specifies three sets of installation locations.

  • perl, for modules included with Perl itself.
  • vendor, for modules installed by the provider of your perl binary.
  • site, for modules installed using 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]. 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.


  1. CPAN doesn't install modules. It's a repository.

    cpan doesn't install modules. cpan download distributions from CPAN and runs the installer provided within, be it Makefile.PL or Build.PL. (Same goes for cpanm and cpanp.)

    These scripts mostly use ExtUtils::MakeMaker or Module::Build install the distribution (though other installers exist).




回答2:


CPAN doesn't actually install files. 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). For Module::Build, see 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. The value of $Config{foo} can also be inspected by running 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. 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).

What people do instead is to configure ExtUtils::MakeMaker, Module::Build, etc to install into a user's home directory. 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. This is done with another environment variable, PERL5LIB. (PERL5LIB doesn't affect installation, it's purely used for loading.)

All of the above is automated and encapsulated in local::lib. (local::lib can also be used to e.g. create a per-project module subdirectory.)

The CPAN documentation also says:

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.


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). This is very easy with e.g. perlbrew.


Another note: You've just found a bug in the documentation for PERL5LIB. "and the current directory" is outdated: . has been removed from the default list of module locations for security reasons.




回答3:


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

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. Regarding shell variables, along with PERL5LIB, you also have PERL_LOCAL_LIB_ROOT.

Regarding your other question, I would say that root may install libraries system-wide. 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>;.

There is also perlbrew that along with local::lib allows a non-privileged user to install Perl and libraries in local directories.

Regarding ways to install modules from CPAN, my favourite is cpanminus. It is invoked with cpanm <library to install>. It never fails...



来源:https://stackoverflow.com/questions/46778215/where-does-cpan-install-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!