0

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

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

How can I capture the output of installation
Look at the source for the cpan tool. It's just a Perl program.
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 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.
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
|
1

Use CPAN::Shell, from the docs:

CPAN::Shell->install("Acme::Meta"); 

3 Comments

any possibility to install dependencies of a perl module through CPAN::Shell in perl script
I want to add proxy for cpanmodule through perl script,how can I do it?
Read the documentation, it clearly states how to do this. If you want to use this tool, read the manual

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.