Hi Derek

I have two propositions to the code below, which in my opinion will not 
function because you still use $rr within the foreach,  while you replaced it 
by the implicit $_ in the foreach line)

++ Line 4:

if ($query != undef)
can be written as
if (defined $query)

The meaning and the results are the same, but using the "defined" operator 
makes clear that "undefinedness" is not a real, comparable value but 
indicates the _absence_ of any value.

++ Lines 3-12:

a) In the case of $res beeing undefined, line 3 will fail.
b) In the case of  $query beeing undefined, no code of lines 4 to 13 is 
executed and no error message is output in this case.
This may be fine (don't know the rest of the code).

You coul'd eventually shorten the code as follows which makes it a bit more 
readable (untested):

use Net::DNS;

my $res = Net::DNS::Resolver->new 
 or die "can't create resolver object: $!"; # not shure if $! ok here
my $query = $res->query("example.com", "NS")
 or die "query failed: ", $res->errorstring;

# at this point, everything went ok,
# so do something with $query:

foreach ($query->answer) {
 print $_->nsdname, "\n" if ($_->type eq 'NS' );
}

# done


greetings joe

[nothing inline below]

Am Dienstag, 1. März 2005 17.35 schrieben Sie:
> excellent thank you to JuniperHost and you Wiggens.
>
> So here is what I ended up with:
>    > 1> use Net::DNS;
>
>   2>my $res   = Net::DNS::Resolver->new;
>   3> my $query = $res->query("example.com", "NS");
>
>    4>if ($query != undef) {
>    5>    foreach ($query->answer) {
>    6>       if ($_->type eq 'NS' ) {
>                   print $rr->nsdname, "\n";
>    8> }else {
>   10>       warn "query failed: ", $res->errorstring, "\n";
>   11>       }
>   12>    }
>   13> }
>
>
> So I can find the all these object methods in the perl docs listed below?
> I have yet to read object orientation with Perl as I am still on
> Programming Perl Chapter 2.
>
> thanks again,
>
> Derek B. Smith
> OhioHealth IT
> UNIX / TSM / EDM Teams
>
>
>
>
>
>              "Wiggins
>              d'Anconia"
>              <[EMAIL PROTECTED]                                          To
>              .org>                     [EMAIL PROTECTED]
>                                                                         cc
>              03/01/2005 11:08          Perl Beginners <beginners@perl.org>
>              AM                                                    Subject
>                                        Re: Net::DNS
>
> [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>

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