Bootstrap FreeKB - Perl (Scripting) - Install a module using cpan
Perl (Scripting) - Install a module using cpan

Updated:   |  Perl (Scripting) articles

cpan (this article) or cpanm can be used to install a Perl module.

Before installing a module, you may want to use the perldoc command to determine if the module is installed.

perldoc -l Net::SSH:Perl

 

Or this command using the -e (inline) option.

perl -MNet::SSH::Perl -e 'print $INC{"Net/SSH/Perl.pm"}'

 

If you installed Perl from source, in whatever directory your installed Perl should be the cpan CLI. In this scenario, the cpan -l command can be used to list the modules that have been installed.

~]# /usr/local/bin/perl5.40.0/bin/cpan -l
Net::protoent   undef
Net::netent     undef
Net::servent    undef
Net::hostent    undef
Net::FTP        3.15
Net::Netrc      3.15
Net::Config     3.15
Net::Domain     3.15
Net::NNTP       3.15
Net::POP3       3.15
Net::SMTP       3.15
Net::Time       3.15
Net::Cmd        3.15
Net::Ping       2.76
Net::FTP::I     3.15
Net::FTP::E     3.15
Net::FTP::A     3.15
Net::FTP::dataconn      3.15
Net::FTP::L     3.15
Getopt::Std     1.14
Getopt::Long    2.57
Getopt::Long::Parser    2.57
IO::Zlib        1.15
IO::Uncompress::Bunzip2 2.212
IO::Uncompress::Unzip   2.212
IO::Uncompress::Base    2.212
IO::Uncompress::RawInflate      2.212
IO::Uncompress::AnyUncompress   2.212
IO::Uncompress::Inflate 2.212
IO::Uncompress::AnyInflate      2.212
IO::Uncompress::Gunzip  2.212
IO::Uncompress::Adapter::Bunzip2        2.212
IO::Uncompress::Adapter::Identity       2.212
IO::Uncompress::Adapter::Inflate        2.212
IO::Compress::Gzip      2.212
IO::Compress::Bzip2     2.212
IO::Compress::Deflate   2.212
IO::Compress::Base      2.212
IO::Compress::RawDeflate        2.212
IO::Compress::Zip       2.212
IO::Compress::Gzip::Constants   2.212
IO::Compress::Zlib::Extra       2.212
IO::Compress::Zlib::Constants   2.212
IO::Compress::Base::Common      2.212
IO::Compress::Adapter::Identity 2.212
IO::Compress::Adapter::Bzip2    2.212
IO::Compress::Adapter::Deflate  2.212
IO::Compress::Zip::Constants    2.212
IO::Socket::IP  undef

 

if you installed Perl using apt-get or dnf install or yum install, the dnf install or yum install command can be used to install cpan.

dnf install perl-CPAN

 

The cpan CLI without any options or flags will start cpan interactive prompt.

cpan

 

Then, use the install module_name (or force install) command to install a module. In this example, the SSH module is installed.

cpan> install Net::SSH::Perl

 

If the install fails, you can try with the force option. This basically ignores failed tests and proceeds with the installation.

cpan> force install Net::SSH::Perl

 

Sometimes, the cpan CLI followed by the name of the module you want to install can be used to install the module non-interactive.

cpan Net::SSH::Perl

 

Or, you can also try this command for non-interactive.

perl -MCPAN -e 'force install Net::SSH::Perl'

 

After the module is installed it's probably a good idea to create a boilterplate Perl script that contains the module.

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;

 

And then use the perl command with the -c option.

~]# perl -c /path/to/example.pl
example.pl syntax OK

 

On the other hand, if there is some issue, something like this may be returned.

~]$ perl -c /path/to/example.pl
Can't locate Net/SSH/Perl.pm in @INC (you may need to install the Net::SSH::Perl module) (@INC entries checked: /usr/local/bin/perl5.38.2/lib/site_perl/5.38.2/x86_64-linux /usr/local/bin/perl5.38.2/lib/site_perl/5.38.2 /usr/local/bin/perl5.38.2/lib/5.38.2/x86_64-linux /usr/local/bin/perl5.38.2/lib/5.38.2) at example.pl line 4.
BEGIN failed--compilation aborted at example.pl line 4.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 57a4bc in the box below so that we can be sure you are a human.