商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 编程开发 > cpanm安装及Perl模块安装教程

cpanm安装及Perl模块安装教程

  发布于2026-06-29 阅读(0)

扫一扫,手机访问

cpanm 是 Perl 模块安装领域最趁手的工具,没有之一。它最大的好处就是自动处理依赖关系,把那些令人头疼的层层嵌套的依赖包一股脑儿搞定。不过,用传统 CPAN shell 或者手动下载源码包安装模块时,遇到大量依赖的滋味,相信不少人都尝过——那叫一个酸爽。下面这个场景,就很典型。

cpanm安装及Perl模块安装教程

安装 MongoDB 模块

复制代码 代码如下:

# perl Makefile.PL
Warning: prerequisite Class::Method::Modifiers 0 not found.
Warning: prerequisite Data::Types 0 not found.
Warning: prerequisite DateTime 0 not found.
Warning: prerequisite DateTime::Tiny 0 not found.
Warning: prerequisite ExtUtils::MakeMaker 6.59 not found. We ha ve 6.30.
Warning: prerequisite File::Slurp 0 not found.
Warning: prerequisite File::Temp 0.17 not found. We ha ve 0.16.
Warning: prerequisite JSON 0 not found.
Warning: prerequisite Moose 0 not found.
Warning: prerequisite Test::Exception 0 not found.
Warning: prerequisite Test::Warn 0 not found.
Warning: prerequisite Tie::IxHash 0 not found.
Warning: prerequisite Try::Tiny 0 not found.
Warning: prerequisite boolean 0 not found.
Writing Makefile for MongoDB

看到这一长串缺失的依赖警告,是不是头都大了?别急,cpanm 就是来解决这个问题的。

1. 安装 cpanm

cpanm 本质上就是一个可执行文件,下载到 bin 目录,加上执行权限,就齐活了。

复制代码 代码如下:

# wget http://xrl.us/cpanm -O /usr/bin/cpanm; chmod +x /usr/bin/cpanm

2. 使用 cpanm 安装模块

cpanm 提供了丰富的参数选项,掌握几个常用的就能应付大部分场景:

复制代码 代码如下:

# cpanm -h
-v,--verbose Turns on chatty output
-q,--quiet Turns off the most output
--interactive 开启交互配置(required for Task:: modules)
-f,--force 强制安装
-n,--notest Do not run unit tests
--test-only 只测试不安装
-S,--sudo sudo to run install commands
--installdeps 只安装依赖模块
--showdeps 只显示依赖信息
--reinstall 重新安装
--mirror 指定镜像url (e.g. http://cpan.cpantesters.org/)
--mirror-only 只从镜像下载
--prompt Prompt when configure/build/test fails
-l,--local-lib Specify the install base to install modules
-L,--local-lib-contained Specify the install base to install all non-core modules
--self-contained Install all non-core modules, even if they're already installed.
--auto-cleanup Number of days that cpanm's work directories expire in. Defaults to 7

Examples:
cpanm Test::More # install Test::More
cpanm MIYAGAWA/Plack-0.99_05.tar.gz # full distribution path
cpanm http://example.org/LDS/CGI.pm-3.20.tar.gz # install from URL
cpanm ~/dists/MyCompany-Enterprise-1.00.tar.gz # install from a local file
cpanm --interactive Task::Kensho # Configure interactively
cpanm . # install from local directory
cpanm --installdeps . # install all the deps for the current directory
cpanm -L extlib Plack # install Plack and all non-core deps into extlib
cpanm --mirror http://cpan.cpantesters.org/ DBI # use the fast-syncing mirror

参数名直接为模块名称。比如安装 MongoDB 模块,只需一行命令:

复制代码 代码如下:

# cpanm MongoDB
--> Working on MongoDB
Fetching http://www.cpan.org/authors/id/F/FR/FRIEDO/MongoDB-0.702.0.tar.gz ... OK
Configuring MongoDB-0.702.0 ... OK
==> Found dependencies: DateTime, Tie::IxHash, Data::Types, DateTime::Tiny, Class::Method::Modifiers, boolean, Moose, File::Slurp, Try::Tiny, Test::Exception, ExtUtils::MakeMaker, Test::Warn, File::Temp, JSON
--> Working on DateTime
...... //自动解决依赖模块

为了加快下载速度,可以指定镜像并强制只从镜像下载:

复制代码 代码如下:

# cpanm --mirror http://mirrors.163.com/cpan --mirror-only MongoDB

3. 删除模块

安装 App::pmuninstall 模块:

复制代码 代码如下:

# cpanm App::pmuninstall

4. 删除模块

复制代码 代码如下:

# pm-uninstall MongoDB

本文转载于:https://www.jb51.net/article/56286.htm 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

产品推荐

热门关注