Perl基础学习10之perl模块学习
目录
模块基础知识
-
查看某模块是否安装
/usr/bin/perldoc Llamas
No documentation found for "Llamas". #模块不存在报错
/usr/bin/perldoc CGI
#模块存在输出说明文档(一下显示部分内容)
NAME
CGI - Handle Common Gateway Interface requests and responses
SYNOPSIS
use strict;
use warnings;
use CGI;
# create a CGI object (query) for use
-
列出所有模块
cpan -a
re 0.19_01 0.37 SHAY/perl-5.30.2.tar.gz
sigtrap 1.06 1.09 SHAY/perl-5.30.2.tar.gz
sort 2.01 2.04 SHAY/perl-5.30.2.tar.gz
strict 1.07 1.11 SHAY/perl-5.30.2.tar.gz
strictures::extra undef undef HAARG/strictures-2.000006.tar.gz
。。。。。。。。。。。。。。。。。。。。。。。
输出四列内容:
模块名/版本号/最新版本号/CPAN中的位置
-
Perl脚本中导入模块
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;#导入模块File::Basename
-
Perl脚本中导入模块指定的函数
#!/usr/bin/perl
use strict;
use warnings;
#以下两种方法等价
#use File::Basename qw(basename dirname);
use File::Basename ('basename','dirname');
-
将路径下模块添加到Perl环境变量
手动安装的包,安装到了一个非默认的查找路径下,这时可以通过设置.bashrc 中PERL5LIB环境变量,perl会从这个环境变量中去查找模块,例如
export PERL5LIB=/home/perl_packge/lib/perl5:$PERL5LIB
-
执行程序时显式指定模块查找路径
perl -I/home/perl_packge/lib/perl5 test.pl
#-I与路径之间不能有空格
-
查看Perl模块安装目录
Perl中Perl模块路径存储在变量@INC中。
输出@INC中默认存储的Perl模块路径:
perl -e '{print "$_\n" foreach @INC}'
-
手动编译安装Perl模块
以Parallel::ForkManager模块为例
以下步骤寻找链接地址
-
linux终端安装
wget https://cpan.metacpan.org/authors/id/Y/YA/YANICK/Parallel-ForkManager-2.02.tar.gz .
tar -zxvf Parallel-ForkManager-2.02.tar.gz
cd Parallel-ForkManager-2.02
ll
上图显示为Makefile.PL,使用make编译;有的软件为Build.PL,使用build编译。
perl Makefile.PL INSTALL_BASE= /home/perl_packge
Checking if your kit is complete...
Looks good
Generating a Unix-style Makefile
Writing Makefile for Parallel::ForkManager
Writing MYMETA.yml and MYMETA.json
make
cp lib/Parallel/ForkManager.pm blib/lib/Parallel/ForkManager.pm
cp lib/Parallel/ForkManager/Child.pm blib/lib/Parallel/ForkManager/Child.pm
Manifying 2 pod documents
make install
Manifying 2 pod documents
Installing /home/perl_packge/lib/perl5/Parallel/ForkManager.pm
Installing /home/perl_packge/lib/perl5/Parallel/ForkManager/Child.pm
Installing /home/perl_packge/man/man3/Parallel::ForkManager.3pm
Installing /home/perl_packge/man/man3/Parallel::ForkManager::Child.3pm
#红色部分为模块安装路径。
-
检查模块是否安装成功
/usr/bin/perldoc Parallel::ForkManager
No documentation found for "Parallel::ForkManager".
原因是没有将模块安装的路径添加到PERL5LIB环境变量中。有两种解决办法如下:
-
模块路径添加到PERL5LIB环境变量中
-
方法一:
vi ~/.bashrc
输入上面模块安装指定的路径输入,如下内容:
export PERL5LIB=/home/perl_packge/lib/perl5:$PERL5LIB
保存
source ~/.bashrc
再次检测模块是否安装成功
/usr/bin/perldoc Parallel::ForkManager
显示帮助文档安装成功
NAME
Parallel::ForkManager - A simple parallel processing fork manager
VERSION
version 2.02
SYNOPSIS
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new($MAX_PROCESSES);
DATA_LOOP:
foreach my $data (@all_data) {
# Forks and returns the pid for the child:
my $pid = $pm->start and next DATA_LOOP;
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
-
方法二:
在perl脚本中指定模块安装路径
#!/usr/bin/perl
use strict;
use warnings;
use lib qw(/home/perl_packge/lib/perl5/);#指定模块安装路径
use Parallel::ForkManager;
cpan非root权限安装Perl模块
-
1 配置个人cpan
wget -O- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib
eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.bashrc
echo 'export MANPATH=$HOME/perl5/man:$MANPATH' >> ~/.bashrc
source ~/.bashrc
- wget -O- http://cpanmin.us | perl - -l ~/perl5 App::cpanminus local::lib
wget -O- http://cpanmin.us fetches the latest version of cpanm and prints it to STDOUT which is then piped to perl - -l ~/perl5 App::cpanminus local::lib. The first - tells perl to expect the program to come in on STDIN, this makes perl run the version of cpanm we just downloaded.perl passes the rest of the arguments to cpanm. The -l ~/perl5 argument tells cpanm where to install Perl modules, and the other two arguments are two modules to install. [App::cpanmins]1 is the package that installs cpanm. local::lib is a helper module that manages the environment variables needed to run modules in local directory.
- eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`
to set the environment variables needed to use the local modules and then
- echo 'eval `perl -I ~/perl5/lib/perl5 -Mlocal::lib`' >> ~/.bashrc
.bashrc会随着linux系统不同而不同,如.profile .bash_profile
to ensure we will be able to use them the next time we log in.(环境变量配置)
- echo 'export MANPATH=$HOME/perl5/man:$MANPATH' >> ~/.bashrc
will hopefully cause man to find the man pages for your local modules.
- source ~/.bashrc
立马生效
-
2 cpan下载模块
经过第一步配置,可以像root账户一样*下载模块了,以安装Module::Runtime模块为例。
cpan Module::Runtime
方法参考资料:https://*.com/questions/2980297/how-can-i-use-cpan-as-a-non-root-user
cpan常用参数介绍和配置
h,? 获得帮助;
- o conf 查看当前 CPAN 的配置信息,如下(部分内容)
- o conf 第1列关键字 修改信息 即可以修改对应第2列的配置信息,例如
#常用配置1,修改cpan下载源:添加阿里云源和清华源,不加push会直接覆盖默认内容
o conf urllist push https://mirrors.aliyun.com/CPAN/ https://mirrors.tuna.tsinghua.edu.cn/CPAN/
#常用配置2,修改CPAN 模块安装位置
#Makefile.PL 需要make的模块
o conf makepl_arg INSTALL_BASE=/home/perl15
#Build.PL 需要build的模块
o conf mbuild_arg INSTALL_BASE=/home/perl15
#常用配置3,配置自动提交,修改后以后再修改不用使用o conf commit
o conf auto_commit 1
##以上配置结束后,执行以下命令才能生效,类似linux中source
o conf commit
- q 退出cpan终端
cpan下载源
cpan默认调用的是cpan.org 的源,安装速度慢,可以修改为国内源。
-
常用国内下载源
cpan上公示常用的国内源见http://mirrors.163.com/cpan/SITES.html
China
AnHui
ftp://mirrors.ustc.edu.cn/CPAN/
http://mirrors.ustc.edu.cn/CPAN/
rsync://mirrors.ustc.edu.cn/CPAN/
Gansu
http://mirror.lzu.edu.cn/CPAN/
* SAR
ftp://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/
http://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/
http://mirror-hk.koddos.net/CPAN/
rsync://mirror-hk.koddos.net/CPAN/
Liaoning
http://mirrors.neusoft.edu.cn/cpan/
其它一些源:(不单只有Perl源还有Python源)
阿里云源:
https://mirrors.aliyun.com/CPAN/
清华源:
https://mirrors.tuna.tsinghua.edu.cn/CPAN/
-
cpan下载源配置
- 方法一:
cpan端同时添加多个源。
#push指定阿里云源和清华源,不加push会直接覆盖默认内容
o conf urllist push https://mirrors.aliyun.com/CPAN/ https://mirrors.tuna.tsinghua.edu.cn/CPAN/
#提交,写入到磁盘配置文件
o conf commit
- 方法二:
直接修改cpan参数配置文件CPAN/MyConfig.pm,该文件位置可以通过如下获得
cpan[6]> o conf
$CPAN::Config options from /home/.local/share/.cpan/CPAN/MyConfig.pm:
#直接找到urllist添加想添加的源路径