Oops, sorry. Really basic stuff. This fails on domains that have listed two nameservers, but only one provides actual answers because the other one is offline or misconfigured.
#!/usr/local/bin/perl
use Net::DNS::Resolver::Recurse;
my $res = Net::DNS::Resolver::Recurse->new;
my $coderef = sub {
my $packet = shift;
printf ";; Received %d bytes from %s\n\n",
$packet->answersize, $packet->answerfrom;
};
$res->callback($coderef);
$res->debug(0);
my $reply = $res->query("test.com", "MX");
if ($reply) {
foreach $rr (grep { $_->type eq "MX" } $reply->answer)
{
print $rr->exchange, $rr->preference, "\n";
}
} else {
warn "query failed: ", $res->errorstring, "\n";
}