[EMAIL PROTECTED] wrote:
All,

Can someone explain this code line by line?
Obviously I understand most of it but lines 4, 5, 6 and 10 I am fuzzy.


I can try :-).

For line 4: if ($query) ..... if what?  Here I would expect to see if ( -s
$query  ) or something like a return code/exit code value check like $? >>
8.
What is if ($query) saying?  Is there a more beginner way?  After line 3
could I check the exit value then  run a more obvious if statement?


Generally this is a check for definedness. Many modules as a convention return a true value of some sort, in this case a "query object" from the resolver -or- they return undef as an error indication. So to handle the error you check to see if $query is defined (or true/false). This is module specific, but generally the module *should* tell you how it expects you to do error handling.


For line 5: foreach my $rr, we are looking for a record string eq to
NS(nameserver)  from $query answer?  But where does $rr, $_ and answer come
from?

$rr is where the loop temporarily stores the looping variable for the foreach, you can pick whatever you want to name the variable. In this case the list (that gets looped over) is special, and is generated as the return value of 'grep'. grep itself takes a list and loops over that list testing each element to see if it evaluates the conditional to true. $_ is where grep temporarily assigns each of the values in the list passed to it, which in this case is the return value from calling the 'answer' method on the $query object. 'answer' is an object method and should be documented in Net::DNS. So this says:


Loop over every value returned by calling 'answer' on $query, checking to see if calling 'type' on that value is equivalent to the string literal 'NS'. Loop over every value where that test is true temporarily setting the value from 'answer' to $rr. Or give me every answer where the type of answer is an 'NS'.

This entire line is fuzzy to me. Is there a more beginner way ?

Always. :-)

I would
probably write it like:
            foreach ($query->answer) {
                  if ( $_ eq NS) {

This is not quite right since you need to handle the inner loop (grep) test the same.


if ($_->type eq 'NS') {

                        print  $rr->nsdname, "\n";
                  }
            }


For line 10: $res->errorstring. Where does errorstring come from? Are there globals in this module that I am missing? I looked on CPAN and did not find any.


'errorstring' is a method of $res (resolver object) and goes back to the error checking I mention above. So if $query is undefined (aka an error happened) then we know (by virtue of the docs for Net::DNS::Resolver) that we can call errorstring and ask it what the error was.


In general I am having trouble understanding and using this modules
potential.


This is where a detailed reading of the documentation for a module's API is critical. And it is probably the most important task in learning programming. Anyone can string syntax together, but understanding how to use someone else's code is far more difficult. Note that in some cases you may have to look at the documentation for modules that this particular module inherits from.



1> use Net::DNS; 2>my $res = Net::DNS::Resolver->new; 3> my $query = $res->query("example.com", "NS");

  4>if ($query) {
  5>foreach my $rr (grep { $_->type eq 'NS' } $query->answer) {
  6>       print $rr->nsdname, "\n";
  7>    }
  8>}
  9>else {
 10>    warn "query failed: ", $res->errorstring, "\n";
 11> }


Good luck,


thank you, Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams




You should check out Learning Perl Objects, References, and Modules from O'Reilly, and/or the object documentation available with perl,


perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot

perldoc -f grep
perldoc Net::DNS
perldoc Net::DNS::Resolver

http://danconia.org

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to