0

I want a perl script that will install all the cpan modules automatically when running the perl script.Can you help this?

  • What platform are you on? I can make a suggestion how to do this on linux but its not strictly perl-based. – Jeef Feb 18 '14 at 10:55
  • Actually I am working now in Windows,but if you can tell in Linux then also it is fine for me. – user3248333 Feb 18 '14 at 11:00

2 Answers2

2

The CPAN client is just a Perl module which is wrapped by the command line tool. You therefore directly invoke it from a Perl script.

However, you may require system administrator priveleges to install new modules. One way to circumvent that is to use local::lib, or to ask the user for the necessary privileges. On Unixes, you could refuse to run your script if not run as root. However, that opens up many security issues and should be avoided if possible.

A fairly sane solution would be to require your users to install the cpanm client, and specify your dependencies in a cpanfile. Your users then just have to do cpanm --installdeps /dir/of/your/script.

But if we are already going through all that trouble, you might as well make a proper CPAN distribution. You don't have to upload it to CPAN, but you can distribute the tarball, and have your users install that. This has many advantages: Mature toolchains, automated testing, and wonderful helpers like Dist::Zilla that make creating a distribution a breeze.

amon
  • 57,091
  • 2
  • 89
  • 149
  • How can I capture the output of installation – user3248333 Feb 18 '14 at 11:17
  • 1
    Look at the source for the `cpan` tool. It's just a Perl program. – brian d foy Feb 18 '14 at 16:47
  • I'm on win 7. And I am still not sure how you answered OP's question. I'm in need to install a few modules within my script during execution on users PC. I also tried the suggestion below, and it didn't work as well. My perl complained that I'm missing CPAN::Shell. I'm on perl ver 5.18.2. If the script to install missing modules itself requires users to install another module `cpanm` , then something is wrong w/ that suggestion? I can't expect my users to install something as simple an application like Firefox or Chrome. – YouHaveaBigEgo Nov 04 '16 at 01:57
  • @YouHaveaBigEgo That's weird, CPAN::Shell is a core module and should always be available. What Perl distribution are you using? ActiveState Perl or Strawberry Perl or something else? Anyway, my above answer discusses how to automate module dependency installation, and isn't supposed to be OS-specific. If you're having more specific trouble, please consider asking a new question. – amon Nov 04 '16 at 06:14
  • I am using ActiveState Perl 5.18.2. Over the years I am afraid to ask a new question here on SOF Anyways,.. My distribution still doesn't have CPAN::Shell `use strict; use warnings; use CPAN::Shell; sub try_load { my $mod = shift; eval("use $mod"); if ($@) { return(0); } else { return(1);}} my $module = 'CPAN::Shell'; if (try_load($module)) { print "loaded\n"; } else { print "not loaded\n"; system("ppm install cpanm"); }` gives me error : Can't locate package CPAN::Debug for @CPAN::Shell::ISA at checkmodule.pl line 4 – YouHaveaBigEgo Nov 04 '16 at 08:15
  • Also I thought you said to install `cpanm` – YouHaveaBigEgo Nov 04 '16 at 08:20
  • @YouHaveaBigEgo I suggested cpanm since that makes it easy to install a list of dependencies from a `cpanfile`. Anyway, your output shows that you do have CPAN::Shell installed (surprise!). However, CPAN::Debug was not loaded. That's because the CPAN modules assume that you `use CPAN`, which then loads the other modules such as CPAN::Shell and CPAN::Debug. If you change that line, your script should print out "loaded". You can also verify that the shell works by running the shell: `perl -MCPAN -e shell` – amon Nov 04 '16 at 08:54
1

Use CPAN::Shell, from the docs:

CPAN::Shell->install("Acme::Meta"); 
Dr.Avalanche
  • 1,944
  • 2
  • 28
  • 37