http://www.perlmonks.org?node_id=904680

henzen has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm slow today for some reason - maybe too little coffee, and would appreciate some wisdom.

Net::DNS::Resolver->new(nameservers => [qw(1 2 3)]...

That's the usual usage, ie, hard coding the nameserver IPs. I want to use a variable such as @nameservers (or even the scalar $nameservers):

Net::DNS::Resolver->new(nameservers => [@nameservers]...

This works, but it only queries the first nameserver. The remainder are ignored (so it fails if the first NS is dead).

I cannot hard-code as $ns = qw(ip1 ip2 ip3 ip4), since the IPs are sourced from another variable and qw() doesn't interpolate (so I cannot do $ns = qw($ip1 $ip2 $ip3 $ip4))...

I just need to programmatically pass several nameserver IPs to Net::DNS::Resolver.

Any pointers to alleviate my dumbness would be appreciated. Thanks

Replies are listed 'Best First'.
Re: Net::DNS::Resolver and nameservers
by gam3 (Curate) on May 13, 2011 at 14:38 UTC
    The code Net::DNS::Resolver->new(nameservers => [qw(1 2 3)]; and the code
    @nslist = qw(1 2 3); Net::DNS::Resolver->new(nameservers => [@nslist];
    are the same, so I think the problem with only the first DNS server being queried is a problem with the Net::DNS::Resolver package or the underlieing library. You might randomize the list of nameservers and reload them in a different order if a request fails.
    -- gam3
    A picture is worth a thousand words, but takes 200K.
      Thanks for the quick response. It looks like this was bad data ('1.1.1.1', '2.2.2.2 3.3.3.3', '...').

      If 1.1.1.1 was dead, it hit '2.2.2.2 3.3.3.3' and soiled itself...
Re: Net::DNS::Resolver and nameservers
by jellisii2 (Hermit) on May 13, 2011 at 15:01 UTC
    The documentation states that it takes an arrayref. Have you tried to use Net::DNS::Resolver->new(nameservers => [\@nameservers] ...
      Na, that makes it fail every time. Problem solved though, so thanks anyway.

      [\@foo] is an AoA, not an arrayref. Update: Well, it is an arrayref of sorts, but not an arrayref reference to an array (better? ;-b) of scalars containing strings. :-)

      --MidLifeXis

        but not an arrayref of scalars. :-)

        An arrayref is a reference to an array is always a reference to an array, no mater what the array contains, and yes, arrayrefs , like all refs, are scalars -- tricky :)