I am not sure if what I am attempting is possible but I'll ask any ways. I have a script which performs various checks. This script will be deployed on at least 6 servers. Currently I only require it to use 2 modules, DateTime and DBI; The idea is that it will first check to see if those modules are indeed installed and if they are not, install and load them. I am looking into Module::Load but not sure if it can handle what I am trying to accomplish. I'd prefer not to use a module to do this. What I currently have is the following

#!/usr/bin/perl use warnings; use strict; my $version = `cat /etc/*-release | head -n 1`; chomp $version; my ($os) = $version =~ /(Ubuntu|CentOS)/; my %rhel = ( "DBI" => "perl-Class-DBI-mysql.noarch", "DateTime" => "perl-DateTime.x86_64", ); if ($os eq 'CentOS'){ foreach my $mod (keys %rhel){ eval { require $mod; $mod->import(); 1; } or do { system("yum install $rhel{$mod} -y"); } } }

2 issues with this, 1 it does not actually seem to load the module and 2 I don't want the text in STDOUT from the eval.

If any of this makes any sense and you can help I'd love the advice. Thanks in advance


------ UPDATE ------

Seems like I have solved my issue. Here is what I ended up doing

#!/usr/bin/perl use warnings; use strict; my $version = `cat /etc/*-release | head -n 1`; chomp $version; my ($os) = $version =~ /(Ubuntu|CentOS)/; my %rhel = ( "DBI" => "perl-Class-DBI-mysql.noarch", "DateTime" => "perl-DateTime.x86_64", ); if ($os eq 'CentOS'){ foreach my $mod (keys %rhel){ eval "use $mod"; if($@){ system("yum install $rhel{$mod} -y"); eval "use $mod"; } } }

Thanks for the help


In reply to Install and then load missing modules with in a script by edimusrex

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.