Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

In a makefile.pl, I would like to check if a module is installed and if not, give the user the option of installing it or not. I have seen this before: it is basically a yes/no question. But how do I set that up in the makefile? I know how to check if a module is installed, but how do I carry out the rest of the stuff?

Replies are listed 'Best First'.
Re: Asking a question in Makefile.PL
by pc88mxer (Vicar) on Apr 20, 2008 at 03:24 UTC
    What you are seeing is probably cpan asking the question because the Makefile.PL has defined PREREQ_PM as in:
    use ExtUtils::MakeMaker; WriteMakefile( ... PREREQ_PM => { 'DBI' => 1.00, 'DBD::Oracle' => 1.12, ... }, );
    How cpan deals will pre-requisites depends on the setting of the prequisites_policy config variable. It can be set to either ask (always ask to install missing pre-requisites), follow (don't ask, always install) or ignore (don't install).
Re: Asking a question in Makefile.PL
by tachyon-II (Chaplain) on Apr 20, 2008 at 05:14 UTC

    If you want to control the process yourself remember that Makefile.PL is just perl so before the call to WriteMakefile do whatever you want:

    use ExtUtils::MakeMaker; eval{require Some::Module}; if ($@) { my $reply = prompt("Some::Module not installed. Install (Y/n)", "Y +"); install("Some::Module") unless $reply =~ m/n/i; } sub install { my $module = shift; eval{require CPAN}; die "Aaaaagh! $@\n" if $@; CPAN::Shell->install($module); } WriteMakefile( ... );

    Update: In line with grinders post changed to EUMM prompt routine.

      And to back up what bingos says. NO, don't write your own prompt routine. You already have one in the EUMM distribution, and as a bonus it will play nicely with unattended installations.

      • another intruder with the mooring in the heart of the Perl

      NO, please don't invoke CPAN in Makefile.PL

      It is extremely bad practice. It plays havoc with CPANPLUS based CPAN tester rigs ( like mine ), and for people who don't use CPAN but use CPANPLUS for installing modules.

      Update

      The best way is to just let CPAN/CPANPLUS deal with installing the module for you as a prereq.

      use ExtUtils::MakeMaker; my %prereqs = ( "Blah::Blah" => 0, "Foo::Bar" => 1.0, ); eval{require Some::Module}; if ($@) { my $reply = prompt("Some::Module not installed. Install (Y/n)", "Y +"); $prereqs{"Some::Module"} = 0 unless $reply =~ m/n/i; } WriteMakefile( PREREQ_PM => \%prereqs, );

        While I'll accept it might not be best practice or suit testing rigs I would be interested to know what the preferred alternatives are.

        Okay, but what would be the alternative in this case? Could I check if CPANPLUS is what's being used and use that or use CPAN instead?