Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

script dies when Net::DNS resolve fails

2 views
Skip to first unread message

Noah

unread,
May 13, 2013, 11:53:13 AM5/13/13
to Perl Beginners
Hi list,


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 = "<<<hostname_removed>>>";
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 <<<hostname_removed>>> failed:
<<script stops here>>>

Cheers

Lawrence Statton

unread,
May 13, 2013, 12:00:04 PM5/13/13
to begi...@perl.org
On 05/13/2013 10:53 AM, Noah wrote:
> 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?

Impossible to say -- you do not show enough to know what is happening
AFTER the block shown.

David Precious

unread,
May 13, 2013, 12:08:22 PM5/13/13
to begi...@perl.org
On Mon, 13 May 2013 08:53:13 -0700
Noah <noah...@enabled.com> wrote:

> Hi list,
>
>
> 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?

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.


--
David Precious ("bigpresh") <dav...@preshweb.co.uk>
http://www.preshweb.co.uk/ www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan www.preshweb.co.uk/github


Dr.Ruud

unread,
May 13, 2013, 3:02:25 PM5/13/13
to begi...@perl.org
On 13/05/2013 18:08, David Precious wrote:

> 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
> }

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';
...;
};

--
Ruud

0 new messages