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

I have a problem - the code within the function works ok. I know this because I can call the function with
$input="1.1.1.1";
&dnslookup;

What doesn't work though is if I try to step through the array and call the function.

I end up getting the following error message Can't call method "answer" on an undefined value at ./dns.pl line 10.

Is anybody out there able to help me pick my silly mistake? (it's late in the afternoon here)


#!/usr/bin/perl use Net::DNS::Resolver; use Thread::Pool; sub dnslookup { my $res=Net::DNS::Resolver->new; my $search = $res->search($input); foreach $rr ($search->answer) { my $type=$rr->type; if ($type eq "A") { $host=$rr->address; } if ($type eq "PTR") { $host=$rr->ptrdname; } if ($type eq "CNAME") { $host=$rr->cname; } } print "$host\t$input\n"; } open (FILE, "./foo") || die "Could not open file\n"; @ip=<FILE>; close (FILE); foreach (@ip) { $input=$_; print $input; &dnslookup; }

Replies are listed 'Best First'.
Re: Net::DNS looping help
by davido (Cardinal) on Jan 13, 2005 at 05:40 UTC

    The answer is found in the POD for Net::DNS::Resolver:

    Search...Returns a "Net::DNS::Packet" object, or "undef" if no answers were found. If you need to examine the response packet whether it contains any answers or not, use the send() method instead.

    In other words, you need to be checking the return value of search() to know whether you got a valid Net::DNS::Packet object back, or undef, which would indicate no answers were found.

    Hope this helps.


    Dave

      Hey Dave, Thanks for the pointer.... I had read that about an hour ago and didn't give it much thought. however, a quick
      if ($search) { ....}
      ...seemed to fix it....

      once again, thanks heaps.