There are several modules relating to perl and RPM, available via CPAN
The RPM2 module allows you to query RPM files and the RPM database
Open the RPM database
Create an iterator
Iterate and print nvre
use RPM2;
my $db = RPM2->open_rpm_db();
my $i = $db->find_all_iter();
while (my $pkg = $i->next) {
print $pkg->as_nvre, "\n";
}