4

I know it's easy to install a module with 'force' using CPAN from command prompt. I am trying to achieve same through the script:

use CPAN;
eval "use Filesys::DiskSpace" or do {
    CPAN::install("Filesys::DiskSpace");
};

Is there any way to add the option 'force' to the code? I am having the following error while compiling the module:

  make test had returned bad status, won't install without force

The warnings could not be serious, so I would like to proceed with the installation. Thanks.

Andrew
  • 241
  • 3
  • 12
  • 1
    If you are attempting to [automatically install missing modules from CPAN](http://stackoverflow.com/q/8183293), don't do it! Instead declare the dependencies, see examples in http://stackoverflow.com/a/7664993 and http://stackoverflow.com/a/2606677. – daxim Feb 09 '12 at 10:19

3 Answers3

5

Looks like you'll need to instantiate CPAN to a variable and call the force() method on it

my $cpan = CPAN->new;
$cpan->force();
$cpan->install("Filesys::DiskSpace");
Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
3

So long as you Really Know What You Are Doing:

eval "use Filesys::DiskSpace; 1" or do {
    CPAN::Shell->force("install","Filesys::DiskSpace");
};

The use builtin doesn't return anything useful, even when it is successful, so it is necessary to include the ";1" in the string eval.

mob
  • 117,087
  • 18
  • 149
  • 283
  • It's working, but CPAN ask 'Would you like me to configure as much as possible automatically?' and I need to type 'yes'. Anyway, thanks for the help. – Andrew Feb 09 '12 at 15:14
3

It looks like you are only making sure that Filesys::DiskSpace is installed:

unless( eval { require Filesys::DiskSpace } ){
  require CPAN;
  CPAN::Shell->force("install","Filesys::DiskSpace");
}

If you want to make sure that Filesys::DiskSpace is loaded, and install it if it's not available:

BEGIN{
  unless( eval { require Filesys::DiskSpace } ){
    require CPAN;
    CPAN::Shell->force("install","Filesys::DiskSpace");
  }
}
use Filesys::DiskSpace;

NOTE:

If you are having problems with your Perl programs working, it is probably because you just installed a broken module.

That particular module hasn't had an official release since 1999.
It also has a fair number of bug reports:

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129