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...

In reply to Auto Install of Modules by hubb0r

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.