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

Hello Monks

I'm working on linux and writing a perl script which checks for some packages on it. If packages are not found, i need to install them. here is what i've tried so far...

#!/usr/bin/perl use strict; open( RPM, "rpm -qa |"); my @rpms = <RPM>; chomp(@rpms); close RPM; # Remove the duplicate values @rpms = keys %{{ map { $_ => 1 } @rpms }}; my @glibc_rpm = grep(/glibc/, @rpms); if ( @glibc_rpm ) { print "package found"; } else { # Install the package my $retCode = qx'yum install glibc-common-2.5-49'; }

Replies are listed 'Best First'.
Re: yum in perl
by moritz (Cardinal) on Mar 02, 2012 at 08:55 UTC

    So, what's your problem? Where are you stuck?

    FWIW a cleaner approach would be to build an .rpm package that declares dependencies on all those modules it needs, so that installing that RPM automatically installs all the missing dependencies.

      1. how to find the latest version of any package
      2. do i need to parse the output yum install whether the package is installed or not
Re: yum in perl
by JavaFan (Canon) on Mar 02, 2012 at 09:57 UTC
    Why don't you just run yum install glibc-common-2.5-49? If the package is installed, yum will tell you.
Re: yum in perl
by CountZero (Bishop) on Mar 02, 2012 at 14:45 UTC
    I am mainly a windows user, so pardon my ignorance in this respect, but why cannot you simply install the modules through CPAN?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      why cannot you simply install the modules through CPAN?
      I don't think there's a Perl implementation of the GNU C Compiler available on CPAN.
Re: yum in perl
by Khen1950fx (Canon) on Mar 03, 2012 at 11:58 UTC
    Your script works for me. Here's another way to do it.
    #!/usr/bin/perl -l BEGIN { require 5.005 } use strict; use warnings; use autodie; use IPC::Open3 qw(open3); $| = 1; open RPM, 'rpm -qa |'; my (@rpms) = <RPM>; chomp @rpms; @rpms = keys %{+{ map { $_ => 1; } @rpms } }; close RPM; my(@glibc_rpm) = grep { /glibc/ } @rpms; if (@glibc_rpm) { print "package found"; } else { my ($writer, $reader, $err, $retCode); open3($writer, $reader, $err, `yum install glibc-common-2.5-49`); $retCode = <$reader>; $err = <$err>; } exit;