Perl Module management

perl It has its own module , There are also third-party modules . The module comes with perl Installed and installed , Third party modules need to be downloaded from CPAN(Comprehensive Perl Archive
Network) Download and install , It can be downloaded from <> Find the required third-party module in .

We can use perldoc 'MODULE_NAME' To indirectly query whether a module is installed , If it is installed, the corresponding document can be output normally , If not installed , A mistake is reported .

If you want to see all installed module, Then use cpan -a, But the first time you use it, you'll have to configure it , But generally speaking , Choose to let it be configured automatically .

for example , Filter out installed contains CGI Module of characters . The first field is the field name , The second field is the installed version number , The third field is the latest version number available , The fourth field is in the CPAN Location in , This position , Explain later .
[[email protected] ~]# cpan -a | grep CGI CGI 3.63 4.40 LEEJO/CGI-4.40.tar.gz
CGI::Fast 1.09 2.13 LEEJO/CGI-Fast-2.13.tar.gz FCGI 0.74 0.78
ETHER/FCGI-0.78.tar.gz CGI::Carp 3.51 4.40 LEEJO/CGI-4.40.tar.gz CGI::Cookie
1.30 4.40 LEEJO/CGI-4.40.tar.gz CGI::Pretty 3.46 4.40 LEEJO/CGI-4.40.tar.gz
CGI::Push 1.05 4.40 LEEJO/CGI-4.40.tar.gz CGI::Util 3.62 4.40
Comprehensive understanding of the recommended content :

* man CPAN

Module knowledge

Modules are actually all perl script . stay perl In culture , Pay great attention to the developers and maintainers of modules . In the metadata properties associated with each module , Will record their information .

The following is the basic information of a module :
Module id = File::Utils CPAN_USERID PEKINGSAM (Yan Xueqing
<[email protected]>) CPAN_VERSION 0.000005 CPAN_FILE
P/PE/PEKINGSAM/File-Utils-0.0.5.tar.gz UPLOAD_DATE 2016-05-11 MANPAGE
File::Utils - Provide various of file operation INST_FILE
/usr/local/share/perl5/File/ INST_VERSION 0.0.5
* Module ID: modular ID, That is, the name of the module , Namely File::Utils

* CPAN_USERID: The person who uploaded this module is in CPAN Medium ID, After that is its registered name and email address . In terms of mailbox and name , Obviously, this is a Chinese

* CPAN_VERSION: The version number of this module

* CPAN_FILE: The integrity of this module ID, this ID be called Distribution id. this ID from 4 Part composition :
* The first 3 Part of it is the uploader of this module ID

* The first 1 Part of it is the uploader ID First letter of

* The first 2 Some of them are uploaders ID The first two letters of ,
* The first 4 Part is the name of the source package of the module

* UPLOAD_DATA: Upload time of module

* MANPAGE:man file , It can be done through man File::Utils Get help information for the module

* INST_FILE: Module installation path , If the module is not installed , Display the not installed

* INST_VERSION: The installed version number of the module
adopt Distribution id, You can directly infer the URL. for example , With Alibaba cloud's CPAN Source image ( take as an example , Well, this module package URL by :

Some tools need to be specified Distribution id,4 After all, it's complicated , In fact, the last two parts can be specified , Generally speaking, the first two parts can be supplemented automatically .

in addition ,CPAN When installing the module , Rely on make tool , So it has to be installed first make. Easier to install the entire development package .
yum -y install make
Manually compiling the installation module

for example , I found it on the Internet Data::Dumper modular , Want to install it manually .

Download good module source package from the Internet , Then unzip it , Enter the source package directory :
wget tar
xf Data-Dumper-2.172.tar.gz cd Data-Dumper-2.172/
Then look at this directory , The existing file is Makefile.PL still Build.PL, Both files can be used to compile and install , If they exist at the same time , You can choose one at will .
[[email protected] Data-Dumper-2.172]# ls -l total 336 -rw-r--r-- 1 privoxy
privoxy 11559 Sep 19 22:36 Changes -rw-r--r-- 1 privoxy privoxy 45832 Sep 19
22:36 -rw-r--r-- 1 privoxy privoxy 51342 Sep 19 22:36 Dumper.xs
-rw-r--r-- 1 privoxy privoxy 1583 Sep 19 22:36 Makefile.PL -rw-r--r-- 1 privoxy
privoxy 618 Sep 19 22:36 MANIFEST -rw-r--r-- 1 privoxy privoxy 418 Sep 19 22:36
MANIFEST.SKIP -rw-r--r-- 1 privoxy privoxy 863 Sep 19 22:36 META.json
-rw-r--r-- 1 privoxy privoxy 548 Sep 19 22:36 META.yml -rw-r--r-- 1 privoxy
privoxy 200069 Sep 19 22:36 ppport.h drwxr-xr-x 3 privoxy privoxy 4096 Sep 19
22:36 t -rw-r--r-- 1 privoxy privoxy 768 Sep 19 22:36 Todo
It's on top of it Makefile.PL, So install :
perl Makefile.PL make make install
If you want to specify the installation path , Then add INSTALL_BASE that will do :
perl Makefile.PL INSTALL_BASE=/home/perlapps
If it is Build.PL, be :
perl Build.PL ./Build ./Build install
If you want to specify the installation path , Then add perl Build.PL −−install_base /home/perlapps

If it is a manually specified installation path , You also need to set the module lookup path environment variable :
export PERL5LIB=/home/perlapps
Or in the perl Inside the program , Specify search path :
#!/usr/bin/perl use lib qw(/home/perlapps);
Manual compilation installation time , It is likely that compilation will be interrupted due to additional library dependency issues . generally speaking , For missing library file names prompted $foo Come on ,Debian/Ubuntu The corresponding library package on is
lib$foo-dev or $foo-dev,redhat On the series lib$foo-devel, Use the package management tool to roughly search .

Module management tool : Interactive cpan shell

Manual installation of modules is not convenient after all .perl Self CPAN Module provided shell It allows us to install modules quickly .
perl -MCPAN -e "shell"

It will enter cpan Interactive shell pattern . In this interactive mode , You can do a lot of things . For example, install the module , Download module , According to regular search module , Find an author's module , Upgrade module , List recent uploads to CPAN And so on .

cpan An order that can be executed , It can be done through h Key for help :
cpan[8]> h Display Information (ver 1.9800) command argument description
a,b,d,m WORD or /REGEXP/ about authors, bundles, distributions, modules i WORD
or /REGEXP/ about any of the above ls AUTHOR or GLOB about files in the
author's directory (with WORD being a module, bundle or author name or a
distribution name of the form AUTHOR/DISTRIBUTION) Download, Test, Make,
Install... get download clean make clean make make (implies get) look open
subshell in dist directory test make test (implies make) readme display these
README files install make install (implies test) perldoc display POD
documentation Upgrade r WORDs or /REGEXP/ or NONE report updates for
some/matching/all modules upgrade WORDs or /REGEXP/ or NONE upgrade
some/matching/all modules Pragmas force CMD try hard to do command fforce CMD
try harder notest CMD skip testing Other h,? display this menu ! perl-code eval
a perl command o conf [opt] set and query options q quit the cpan shell reload
cpan load again reload index load newer indices autobundle Snapshot
recent latest CPAN uploads
for example , adopt m Orders or i Command to find contains by regular expression "File::Util" Module of characters :
cpan[11]> m /^File::Util*/ Module < File::Util
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Cookbook
(TOMMY/File-Util-4.132140.tar.gz) Module < File::Util::Definitions
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Exception
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Exception::Diagnostic
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Exception::Standard
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Interface::Classic
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Interface::Modern
(TOMMY/File-Util-4.161950.tar.gz) Module < File::Util::Manual
(TOMMY/File-Util-4.132140.tar.gz) Module < File::Util::Manual::Examples
(TOMMY/File-Util-4.132140.tar.gz) Module < File::Util::Tempdir
(PERLANCAR/File-Util-Tempdir-0.030.tar.gz) Module < File::Utils
The module versions listed here may be more CPAN The versions listed on the page are lower .

During use , Keys can be very difficult to use . If you need to delete a typed character , You need to press and hold at the same time ctrl+back Key .

for example , install File::Util modular :
cpan[12]> install File::Utils
You can also install the perl Module source package , even to the extent that get The command can download the source package directly . for example , install File::Rename
modular , Its source code package address is
cpan[8]> get RMBARKER/File-Rename-1.09_02.tar.gz
After successful download , It will prompt for the saved path :
Checksum for
/root/.cpan/sources/authors/id/R/RM/RMBARKER/File-Rename-1.09_02.tar.gz ok
Then use install Command to install the module source package :
cpan[9]> install RMBARKER/File-Rename-1.09_02.tar.gz
there distribution id We can add the first two parts , The first two parts can also be omitted .

It is required to install a module by default make test, But some modules do test Very often , Such as installation CPANPLUS Dependent module when module File::Fetch
, It may test Very long time , This is fine CTRL+C sign out , Skip when installing again test stage .
cpan[10]> notest install CPANPLUS
If you want to configure CPAN, Need to use o conf, This command is critical , There's a lot of information about configuration , Only the following modifications are introduced here CPAN Source method . For example, modify CPAN Source of Alibaba cloud :
# 1. appoint aliyun Of CPAN o conf urllist push #
2. Submit , Write to disk profile o conf commit # 3. View current CPAN source o conf urllist
about cpan interactive , as well as o conf as well as CPAN Configuration file for the module , sure man CPAN Get more usage .

Note also , stay perl In the program code , It can be done through CPAN::Shell->COMMAND("arguments"); Implement the same operation as in interactive mode :

for example , stay perl Install module in program code :
#!/usr/bin/perl use CPAN; CPAN::Shell->install("Acme::Meta");
CPAN client :cpan command ( script )

cpan The order is as follows perl One installed together perl script , It allows us to cpan Free from that disgusting interaction . But its internal function is mainly to call, and cpan Interactive is the same .

About how many cpan Common options for scripts :
-a: establish Of autobundle -D
module: View the detailed property information of the module . For example, whether to install , Installed version number , Latest version number , Corresponding module path , Corresponding source code package file path , Who maintained it -g
module: Download the latest version of the module to the current directory -i module: Install the specified module -j appoint CPAN Configuration data file
-J: with Same format dump Current profile -O: List expired modules -v: output cpan The version number of the script and Version number of
for example , install File::Util::Manual modular , It's simple .
cpan -i File::Util::Manual
View module information :
[[email protected] ~]# cpan -D File::Utils Reading '/root/.cpan/Metadata' Database
was generated on Wed, 19 Sep 2018 20:17:03 GMT File::Utils
------------------------------------------------------------------------- (no
description) P/PE/PEKINGSAM/File-Utils-0.0.5.tar.gz # Modular distribution id
/usr/local/share/perl5/File/ # Installation path of the module Installed: 0.0.5 # Installed module version number
CPAN: 0.000005 up to date # CPAN The latest module version number in Yan Xueqing (PEKINGSAM) #
Author's name and CPAN Medium ID [email protected]
cpan -J For understanding and modifying CPAN Module configuration files are very helpful , You can try it .

CPAN client :CPANPLUS modular

CPANPLUS Provides and Similar functions , But it has more features .

first , Install it :
cpan[20]> notest install CPANPLUS
cpanp Commands can enter the interactive , You can also use command line mode , Command line mode is also very rich . But the usage is very simple , and cpan It's all similar , sure cpanp -h Take a general look .

for example , install MongoDB modular :
cpanp -i MongoDB
uninstall MongoDB modular :
cpanp -u MongoDB
CPAN client :cpanm

This is really a full one click installation , No configuration required . and , It has no interactive mode .

first , Where to install this tool cpanminus modular .:
install App::cpanminus
The options and usage are simple , sure cpanm -h Go and have a look .

To install a module :
cpanm File::Utils
By default , It searches CPAN The source is, Can be specified CPAN source :
cpanm --mirrors File::Utils
Unload module

To uninstall a module , Can be installed pmuninstall modular , It provides pm-uninstall command , Modules can be unloaded quickly .
cpan App::pmuninstall
for example , uninstall MongoDB modular :
pm-uninstall MongoDB
A prompt will be given by default , Do you want to confirm the uninstall . Can be specified "-f" Option to force the uninstall , No interactive prompts are required :
pm-uninstall -f MongoDB
of course , except pm-uninstall,cpan,cpanp,cpanm Both have the function of unloading .


By default , The installed third-party modules will perl Put them together , And for those who are not root user , No write access to some directories , Failed to install , can only sudo. have access to
local::lib, Custom installation path .

first , Install this module :
cpan[9]> install local::lib
And then back to it bash, You can view the environment variables exported by this module :
$ perl -Mlocal::lib PATH="/home/fairy/perl5/bin${PATH:+:${PATH}}"; export
PATH; PERL5LIB="/home/fairy/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export
export PERL_LOCAL_LIB_ROOT; PERL_MB_OPT="--install_base \"/home/fairy/perl5\"";
export PERL_MB_OPT; PERL_MM_OPT="INSTALL_BASE=/home/fairy/perl5"; export
local::lib It is by modifying some environment variables ( Pay attention to the PERL5LIB) Let them go perl Built in path @INC, So as to influence CPAN Directory path of client installation .

Then use the cpan Client's -I switch , On the basis of local::lib To install ( notes : Some cpan The client does not -I option ).
cpan -I Text::Levenshtein
If used cpanm, It's a little bit smarter , If you have already set up local::lib
, It will be installed directly according to the settings , If not set , It checks for write access to the default installation directory , without , Set it automatically local::lib modular . therefore ,cpanm No settings are required
local::lib, However, it is still possible to explicitly specify the use of local::lib To install :
cpanm --local-lib HTML::Parser
If still in use cpan client , Specify the installation path . You can modify two parameters :
cpan[1]> o conf makepl_arg INSTALL_BASE=/home/fairy/perl5 cpan[2]> o conf
mbuild_arg "--install_base /home/fairy/perl5" cpan[3]> o conf commit
It can be downloaded from man CPAN Get the explanation of these two parameters : They are passed to perl Makefile.PL and ./Build
Parameters of . in other words , By setting these two parameters , No matter what makefile Format installation , still build Format installation , Will be installed in the given directory .

CPAN Module configuration files and configuration recommendations

When we first entered CPAN shell When , Will let us configure CPAN, We can choose to have it configured automatically . After automatic configuration , It will prompt you where the configuration file has been saved .

We can also cpan Use in interactive mkmyconfig Regenerate the profile that belongs to the current user , It will be saved to ~/.cpan/CPAN/, At the same time, it will
local::lib Is appended to the ~/.bashrc in .

get into cpan interactive shell, typing o conf The current configuration can be output . There are many content items , Several items are suggested to be revised :

1. modify CPAN Source of Alibaba cloud
# 1. appoint aliyun Of CPAN o conf urllist push #
2. Submit , Write to disk profile o conf commit # 3. View current CPAN source o conf urllist

above push The order is to aliyun Of CPAN Source push to urllist Last position in the array of (key yes urllist,value It's a url The composition of the array ), You can also use pop Remove the last element of the array , use shift Remove the first element of the array , use unshift Push to the first element of the array . If you don't give these orders , Direct to url, Replace the entire array .

If you want to empty urllist, have access to o conf init urllist.

2. modify make and build How to implement , add sudo. This is mainly aimed at the non-government root User's
o conf make_install_make_command 'sudo /usr/bin/make' o conf
mbuild_install_build_command 'sudo ./Build'
3. Specify the installation path . This is mainly aimed at the non-government root User's
cpan[1]> o conf makepl_arg INSTALL_BASE=/home/fairy/perl5 cpan[2]> o conf
mbuild_arg "--install_base /home/fairy/perl5"
4. Configure auto submit
o conf auto_commit 1
5. Configuration dependency policy
o conf prerequisites_policy follow
Except for the above items , use cpan Time , It is also recommended to update and install the following modules :
install CPAN ExtUtils::MakeMaker Module::Build Bundle::CPAN
Installing these modules can solve many unnecessary troubles .