top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

script dies when Net::DNS resolve fails

0 votes
296 views

When Net::DNS resolved name is not found my script dies. How can I allow for my script to continue on even if there is a failed DNS query?
Just cutting and pasting the relevant lines

use Net::DNS;
my $res = Net::DNS::Resolver->new;

 my $v6_source_hostname = "";
 my $source_ipaddress;
 $source_ipaddress = $res->query("$v6_source_hostname", "AAAA");
 if ($source_ipaddress) {
 foreach my $rr (grep { $_->type eq 'AAAA' } 
$source_ipaddress->answer) {
 print $rr->address, "n";
 }
 } else {
 warn "query failed: ", $res->errorstring, "n";
 }

------ output -----

query failed: NXDOMAIN
lookup for  failed:
posted May 14, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Impossible to say -- you do not show enough to know what is happening  AFTER the block shown.

1 Answer

0 votes

The usual way to catch exceptions is with an eval block or Try::Tiny etc.

Basic example:

my $source_address = eval { $res->query(....); };

if ($@) {
 # an error occurred - $@ will contain the message
 # do something appropriate here
}

However, $resolver->query() should, according to the docs, just return undef if no answers are found, and your example output suggests that execution gets to your "query failed:" warning in the else block, so
something else is happening after that - something which we have no idea, since we're not seeing the whole code.

answer May 14, 2013 by anonymous
Testing $@ is basically always wrong, because it is a global variable that can have changed value.

A rewrite:

 my $source_address;
 eval {
 $source_address = $res->query(....);
 1; # success
 }
 or do {
 my $eval_error = $@ || 'Zombie Error';
 ...;
 };
Similar Questions
...