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

Hello O Glorious Monks!

I'm attempting to include some code in my script that will automatically take care of the installation of the modules I require without user intervention. This will make the initial installation of this script much simpler (it is for in-house use, but will be distributed to many servers).

I've done the following:
#!/usr/local/bin/perl -w use strict; print "You must be root to run this program\n" and exit 1 unless ($> = += 0); BEGIN { unless (eval { require Config::Auto }) { print "Need to install Config::Auto module. I'll do that for +you now:\n"; my $ret = system("perl -MCPAN -e 'install Config::Auto'"); print $ret; } }
and all has gone as planned. Horray! The problem is, what I want to do is the following:
#!/usr/local/bin/perl -w use strict; print "You must be root to run this program\n" and exit 1 unless ($> = += 0); BEGIN { foreach my $module ( qw{Config::Auto Mail::Sendmail} ) { unless (eval { require $module }) { print "Need to install $module module. I'll do that for y +ou now:\n"; my $ret = system("perl -MCPAN -e 'install $module'"); print $ret; } } }
but for some reason, the eval is returning false even though the modules do exist, and forcing my cpan installation to be attempted again.

AHA!! I just figured it out, after re-reading perldoc -f require. I'll post the question anyways in the hopes of enlightening someone in the future that may run into the same issue. The trick is:
eval {"require $module"}
Which I somehow glossed over on my first reading of the perldoc. I'm glad I figured it out!

I guess I'll change the question to this: Does anyone feel this is a good or bad design decision to do this type of auto-installation for a script? Should I attempt to make the installation portion interactive, perhaps prompting the user as to whether or not they really want to install the modules?

Thanks for your time...

Replies are listed 'Best First'.
Re: Auto Install of Modules
by polettix (Vicar) on Aug 04, 2005 at 16:57 UTC
    As a user, I notice that ExtUtils::MakeMaker already includes mechanisms to signal the fact that some needed modules aren't present. This is used by CPAN to ask the user if the dependencies have to be added to the list of modules to be installed. These two modules should be a good starting point to figure things out.

    Moreover, maybe you should also take a look at PAR. I don't know it at all, but it probably already includes mechanisms to skip the installation of already present modules, among other goodies.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Auto Install of Modules
by blazar (Canon) on Aug 04, 2005 at 17:31 UTC
    You may use some code in %INC trickery to (try to) install some modules (if not already available) and return filehandles to them. See e.g. Re: Using a module more than once for a very trivial example of such trickeries, and the following node for a pointer to the relevant documentation.