Perl Modules and CPAN

How to check if a module is installed
To check if a module, such as File::Path, is installed: perl -MFile::Path -le 'print "Module installed"'

If you also want to know the version of an installed module: perl -le 'eval "require $ARGV[0]" and print "Module installed (v.",$ARGV[0]->VERSION,")"' File::Path

Configure CPAN to take modules from a local mirror
To make the servers use a personal mirror when installing a module with CPAN, with cpan v1.9 or higher: rm -rf /root/.cpan perl -MCPAN -e shell Would you like me to configure as much as possible automatically? [yes] cpan[1]> o conf init urllist Is it OK to try to connect to the Internet? [yes] no Please enter your CPAN site: [] https://myserver.domain.com/CPAN/ Enter another URL or RETURN to quit: [] cpan[2]> o conf build_requires_install_policy yes cpan[3]> o conf prerequisites_policy follow cpan[4]> o conf ncftpget '' cpan[5]> o conf ftp_proxy '' cpan[6]> o conf http_proxy '' cpan[7]> o conf commit cpan[8]> quit

with cpan v1.7: rm -rf /root/.cpan perl -MCPAN -e shell cpan> o conf init Are you ready for manual configuration? [yes] no cpan> o conf urllist push https://myserver.domain.com/CPAN/ cpan> o conf build_requires_install_policy yes cpan> o conf prerequisites_policy follow cpan> o conf ncftpget '' cpan> o conf ftp_proxy '' cpan> o conf http_proxy '' cpan> o conf commit cpan> quit

Install a module with CPAN
To install a module using the CPAN utility: perl -MCPAN -e 'install MIME::Lite' or: cpan -i MIME::Lite

Install several modules with CPAN if not present
If you need to install several modules using the CPAN utility, after checking that they are not already installed, you can use this example script: for MODULE in CGI CGI::Simple CGI::Carp HTML::Template CGI::Session \ HTML::Mason MasonX::Request::WithApacheSession \ Perl6::Junction Regexp::Common File::MMagic Image::Size \ HTML::FillInForm Data::FormValidator Scalar::Util CGI::Cookie \ Email::Valid MIME::Lite IPC::System::Simple Log::Log4perl do   CHECK=$(perl -M${MODULE} -e '' 2>/dev/null ; echo $?) if [ "${CHECK}" -eq 2 ]; then perl -MCPAN -e "install ${MODULE}" fi done

CPAN::Mini
If the server is not connected to the Internet, installing Perl modules becomes a tedious activity: tar xfz File-HomeDir-1.004.tar.gz cd File-HomeDir-1.004/ perl Makefile.PL make make test make install cd .. tar xfz File-Path-2.16.tar.gz cd File-Path-2.16/ perl Makefile.PL make make test make install cd .. tar xfz CPAN-Mini-1.111016.tar.gz cd CPAN-Mini-1.111016/ perl Makefile.PL make make test make install cd ..

Thanks to this module, it is possible to create a minimal mirror of CPAN locally: minicpan -h Usage: minicpan [options] Options -l LOCAL   - where is the local minicpan? (required) -r REMOTE  - where is the remote cpan mirror? (required) -d 0###    - permissions (numeric) to use when creating directories -f         - check all directories, even if indices are unchanged -p         - mirror perl, ponie, and parrot distributions --debug    - run in debug mode (print even banal messages) -q         - run in quiet mode (don't print status) -qq        - run in silent mode (don't even print warnings) -c CLASS   - what class to use to mirror (default: CPAN::Mini) -C FILE    - what config file to use (default: ~/.minicpanrc) -h         - print help and exit -v         - print version and exit -x         - build an exact mirror, getting even normally disallowed files -t SEC     - timeout in sec. Defaults to 180 sec --offline  - operate in offline mode (generally: do nothing) --log-level - provide a log level; instead of --debug, -q, or -qq --remote-from TYPE - cpan remote from 'cpan' or 'cpanplus' configs

To create such a mirror: minicpan -l /opt/apache/htdocs/CPAN -r " https://www.cpan.org/ " -d 0755 -f -q