Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Programmatically installing modules

by morgon (Curate)
on Dec 23, 2013 at 10:27 UTC ( #1068166=perlquestion: print w/replies, xml ) Need Help??
morgon has asked for the wisdom of the Perl Monks concerning the following question:


I want to implement the following logic:

  1. check for the availability of a module
  2. if not available, install it from cpan
  3. if install fails bail out

For installing modules I currently use CPAN->install which has the problem that if I run it with a module I already have it updates it (so I need to check first) and I don't seem to get any indication wether or not the install succeeded, so I am forced to do something like this:

eval { require "Some::Module"; }; if($@) { # need to install CPAN->install("Some::Module"); eval { require "Some::Module" }; # check if we have it now if($@) { print "installation failed" } } };
Furthermore some module-installations prompt for input (e.g. "do you want to run extra-tests?" etc) that I would like to supress.

What is the proper way to do this?

Replies are listed 'Best First'.
Re: Programmatically installing modules
by LanX (Bishop) on Dec 23, 2013 at 10:34 UTC
    > if I run it with a module I already have it updates it (so I need to check first)

    from cpan

    -D module [ module ... ] Show the module details. This prints one line for each out- +of-date module (meaning, modules locally installed but have newer v +ersions on CPAN). Each line has three columns: module name, local +version, and CPAN version.

    since is a wrapper around it should be possible to automatize this (at least).

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I already check for the availability of the module (via a require) before I try to install.

      My main problem at the moment is making the installation non-interactive.

        I thought you wanted to avoid automatic updates?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re: Programmatically installing modules (cpan install build interactivity inactivity_timeout )
by Anonymous Monk on Dec 23, 2013 at 10:41 UTC
Re: Programmatically installing modules
by Anonymous Monk on Dec 23, 2013 at 14:51 UTC

    As for suppressing prompts, properly-coded versions of Makefile.PL and Build.PL should not prompt (and take the default) if environment variable PERL_MM_USE_DEFAULT is set to a true value. This is documented in the ExtUtils::MakeMaker and Module::Build::API documentation, respectively.

    The situation on tests that prompt is more confusing. The Oslo Consensus says to set environment variable AUTOMATED_TESTING where there is no recourse to a human operator. One of my first modules (before the Oslo Consensus' date of 2008) drew as its first RT ticket a complaint that I should respond to this environment variable, so I suspect that CPAN modules will generally respond to it. The Lancaster Consensus talks about setting NONINTERACTIVE_TESTING, but I do not know how wide-spread this is.

    Of course, none of this works with ill-behaved installations. A first cut might be to re-open STDIN on the null device -- but of course if the installation is ill-behaved, who knows what that will do?

    For specific modules, it is also possible that their Makefile.PL or Build.PL files will respond to command-line options if these are specified. The CPAN client provides a mechanism for configuring individual distributions, which I believe can provide arguments to Makefile.PL or Build.PL. See 'Distroprefs' in the 'CPAN' documentation (NOT 'cpan', which may be a problem if you have a case-blind file system). I can't speak to CPANP or cpanm

Re: Programmatically installing modules
by Khen1950fx (Canon) on Dec 23, 2013 at 16:13 UTC
    Here's what I use for my automated smokers:
Re: Programmatically installing modules
by aitap (Curate) on Dec 27, 2013 at 17:41 UTC

    By the way, there are some modules which already do this (watch for modules not being found and install them using or cpanm).

    Of course, using them requires them to be installed from CPAN, but, depending on what you need, they can be of some use.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1068166]
Approved by LanX
Front-paged by LanX
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-05-20 22:28 GMT
Find Nodes?
    Voting Booth?