doubledecker
doubledecker

Reputation: 363

how to get list of rpm packages installed in the system

how to get list of all rpm packages installed on Linux using Perl. any help is appreciated.

Upvotes: 4

Views: 17245

Answers (3)

MichielB
MichielB

Reputation: 4285

Depending on how to interpret your question, the correct answer can be:

rpm -qR perl

Upvotes: 1

David W.
David W.

Reputation: 107040

I guess you can always use the rpm command:

$ rpm --query --all  --qf "%-30{NAME} - %{VERSION}\n"

You can then use this in a wide variety of ways:

use autodie;
open my $RPM_FH, "-|", qq(rpm --query --all --qf "%-30{NAME} - %{VERSION}\n");
my @rpmLines = <$RPM_FH>;
close $RPM_FH;

Or:

 my @rpmLines = qx(rpm --query --all --qf "%-30{NAME} - %{VERSION}\n");

I've also found RPM::Database which would be a more Perlish way of doing things. This package ties the RPM database to a hash:

use RPM::Database;

tie %RPM, "RPM::Database" or die "$RPM::err";

for (sort keys %RPM)
{
    ...
}

I have never used it, so I'm not sure exactly how it would work. For example, I assume that the value of each hash entry is some sort of database object. For example, I would assume that it would be important to know the version number and the files in your RPM package, and there must be someway this information can be pulled, but I didn't see anything in RPM::Database or in RPM::HEader. Play around with it. You can use Data::Dumper to help explore the objects returned.

WARNING: Use Data::Dumper to help explore the information in the objects, and the classes. Don't use it to figure out how to pull the information directly from the objects. Use the correct methods and classes.

Upvotes: 7

Dave Cross
Dave Cross

Reputation: 69224

The easiest way is probably to shell out to the rpm program.

chomp(my @rpms = `rpm -qa`);

Upvotes: 4

Related Questions