Automating CPAN Configuration and Perl Module Installation in Silent Mode for Linux Systems


2 views

When deploying Perl applications across multiple Scientific Linux/RHEL systems, manual CPAN configuration becomes impractical. The initial interactive setup (answering questions about mirrors, preferences, etc.) blocks automated deployment workflows.

The secret lies in CPAN::Config. Create this pre-configured file before first run:

mkdir -p ~/.cpan/CPAN
cat > ~/.cpan/CPAN/MyConfig.pm <<'EOF'
$CPAN::Config = {
  'build_dir' => q[/root/.cpan/build],
  'cpan_home' => q[/root/.cpan],
  'keep_source_where' => q[/root/.cpan/sources],
  'prefer_installer' => q[MB],
  'urllist' => [q[http://cpan.metacpan.org/]],
  'wait_list' => [q[never]],
};
EOF

Combine these techniques in your deployment script:

# Install CPAN if missing
yum install -y perl-CPAN

# Initialize silent configuration
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'install CPAN'

# Install modules with auto-confirmation
perl -MCPAN -e 'my $c = "CPAN::HandleConfig"; $c->load(doit => 1,autoconfig => 1); 
$c->edit(prerequisites_policy => "follow"); 
$c->edit(build_requires_install_policy => "yes"); 
install(Module::Name)'

For complex dependency trees, consider cpanm (App::cpanminus):

curl -L https://cpanmin.us | perl - --sudo App::cpanminus
cpanm --quiet --notest --skip-installed Module::One Module::Two

Here's a complete deployment script installing DBI and DBD::mysql:

#!/bin/bash
# Install prerequisites
yum install -y perl-CPAN gcc mysql-devel

# Configure CPAN silently
export PERL_MM_USE_DEFAULT=1
echo -e "\n\n" | cpan
cpan <<EOF
o conf prerequisites_policy follow
o conf commit
EOF

# Install modules
cpan -i DBI
cpan -i DBD::mysql

Common issues and fixes:

  • For SSL errors: yum install openssl-devel
  • Missing make/gcc: yum groupinstall "Development Tools"
  • Proxy settings: Add o conf urllist push http://proxy.example.com

When automating Perl environment setup, the biggest roadblock is CPAN's interactive first-run configuration. Here's how to bypass it completely:

# One-liner to initialize CPAN non-interactively
PERL_MM_USE_DEFAULT=1 perl -MCPAN -e 'my $c = "CPAN::HandleConfig"; $c->load(doit => 1, autoconfig => 1); $c->edit(prerequisites_policy => "follow"); $c->edit(build_requires_install_policy => "yes"); $c->commit'

For batch module installation, these approaches work reliably:

# Method 1: Using CPAN.pm
perl -MCPAN -e 'install Module::Name'

# Method 2: Using cpanm (recommended)
curl -L https://cpanmin.us | perl - --sudo Module::Name

# Method 3: Direct from tarball
perl Makefile.PL && make && make test && make install

Here's a complete solution for Scientific Linux/RHEL:

#!/bin/bash
# Ensure required packages
yum install -y perl perl-devel gcc make

# Install cpanminus (modern alternative)
curl -L https://cpanmin.us | perl - --sudo App::cpanminus

# Configure CPAN environment
export PERL_MM_USE_DEFAULT=1
export PERL_EXTUTILS_AUTOINSTALL="--defaultdeps"

# Install modules with dependency resolution
cpanm --notest Module::One
cpanm --notest Module::Two
cpanm --notest Module::Three

# Verify installations
perl -MModule::One -e 'print "Module::One v$Module::One::VERSION\n"'

For modules requiring manual intervention:

# Pre-seed configuration for problematic modules
export DEBIAN_FRONTEND=noninteractive
PERL5LIB=/root/.cpan/build/Module-XYZ-1.23 perl Makefile.PL < answers.txt

To handle complex dependency chains:

# Create a bundle package
cpanm --look Module::Problematic
# Then run interactive configuration once and save as template

# Alternatively, use Docker for isolation:
docker run -it --rm perl:slim sh -c "cpanm Module::Name && perl -e '...'"

For repeatable deployments, create a MyConfig.pm:

package CPAN::MyConfig;
use parent 'CPAN::Config';

$CPAN::Config = {
  'build_dir' => qq[$ENV{HOME}/.cpan/build],
  'prerequisites_policy' => 'follow',
  'auto_commit' => 1,
  # Additional config options...
};
1;