11

I need to install CPAN and some Perl modules automatically in a Scientific Linux (RHEL) installation script. Unfortunately the specific modules I want (at least one of them) cannot be found as RPM:s as far as I've seen.

So I need to install CPAN, configure it automatically (or with a config file) and then install the wanted modules (including dependencies) automatically as well.

This doesn't seem like a very unusual requirement, but I haven't seen any really good documentation on this. The problem is that whenever CPAN is launched for the first time an interactive configuration runs. Can this be skipped somehow? And how do I launch module installations directly from the command line?

0
5

You might want to look at cpanm for this; it's lightweight, shell scriptable, and much simpler than classic CPAN.

2
  • 1
    Overwhelmingly the right answer. Use cpanm. If you're using perlbrew, use perlbrew install-cpanm and you're done. – Craig Ringer Nov 15 '16 at 8:09
  • Posted a new question and answer at serverfault.com/q/815649/102814 because I'm annoyed by how relatively undiscoverable this is, and that perlbrew didn't come up anywhere. – Craig Ringer Nov 18 '16 at 0:37
28

Try setting this environment variable before running CPAN:

export PERL_MM_USE_DEFAULT=1

It makes perl automatically answer "yes" when CPAN asks "Would you like to configure as much as possible automatically? [yes]"

Source

2
  • This answer is much better than the accepted answer. – kapad Feb 23 '19 at 19:27
  • Nice & clean solution. If you rather to do it for just one command instead of exporting the env variable which changes the behavior on session, you can feed that env variable just to one command like this: PERL_MM_USE_DEFAULT=1 perl -MCPAN -e "install File::Tail::Multi" – Yashar Dec 10 '20 at 14:04
6

"And how do I launch module installations directly from the command line?"

This should do:

perl -MCPAN -e 'install Your::Package'
5

If it's a repetitive task, it may pay to spend a few hours with cpan2rpm and turn them into RPM's. In my experience, CPAN runs (even interactive) are too plagued by unexpected events (version quirks, network problems, bogus tests failures) to be relied upon in a (semi-)automatic install.

3

This should do the magic:

get_cpanm(){
    if [ \! -f /usr/local/bin/cpanm ]; then
            cd $TMP_DIR && curl --insecure -L http://cpanmin.us | perl - App::cpanminus
            if [ \! -f /usr/local/bin/cpanm ]; then
                    echo "Downloading from cpanmin.us failed, downloading from xrl.us"
                    curl -LO http://xrl.us/cpanm &&
            chmod +x cpanm &&
            mv cpanm /usr/local/bin/cpanm
            fi
    fi
    CPANM=$(which cpanm);
    if [ \! -f "$CPANM" ]; then
            echo "ERROR: Unable to find cpanm"
            return 1;
    fi
    return 0
}
cpanm Time::HiRes CGI Moose Config::JSON other::cpanmodules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.