edimusrex has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Install and then load missing modules with in a script
by dmitri (Priest) on Jul 20, 2016 at 19:33 UTC |