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– Jeef2014-02-18 10:55:28 +00:00Commented 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.user3248333– user32483332014-02-18 11:00:00 +00:00Commented Feb 18, 2014 at 11:00
2 Answers
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.
7 Comments
cpan tool. It's just a Perl program.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.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 4Use CPAN::Shell, from the docs:
CPAN::Shell->install("Acme::Meta");