The reason I tested all three situations:     if ( $query != 0 or $query !=
undef or $query != ' ' )  {

was for knowledge and repitition that everytime I looked at the code I
would know what all 3 meant and that they had the same meaning, 0.  In
addition I wanted to be anal !



> Maybe a dumb question (since I have no routine with Net::DNS::Resolver):
> Are there alway two elements in array @a  if everything went fine?

            No.  There are actually 0-9 elements in this array without the
"svr" string search


> btw: If the 1st FOR LOOP loop failed, @a is empty, so maybe you should
move
> the last 3 lines in the 1st FOR LOOP  if branch.


      tkx for the pointer!  So here is what you mean?


                foreach ($query->answer) {
                     if ( $_->type eq 'NS' ) {
                     #next unless $_->type eq "A";
                         foreach ($_->nsdname) {
                                 push (@a,$_) if $_ =~ "svr";
                                 $e++;
                         } # END 2nd FOR LOOP
         } else {
         print F "DNS query failed: ", $res->errorstring, "\n";
                     } # END 2nd IF

              @a = sort { $a cmp $b } @a;
              print F $a[0],"\n";
              print F $a[1],"\n";
                 } # END 1st FOR LOOP

         } # END 1st IF




Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams




                                                                           
             "Wiggins                                                      
             d'Anconia"                                                    
             <[EMAIL PROTECTED]                                          To 
             .org>                     [EMAIL PROTECTED]        
                                                                        cc 
             03/02/2005 09:15          beginners@perl.org                  
             AM                                                    Subject 
                                       Re: Net::DNS                        
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




John Doe wrote:
> Hi Derek,
> hi all (see below, please help)
>
>
>>[...]
>>
>>>        my $res = Net::DNS::Resolver->new;
>>>        my $query = $res->query(".ohnet", "NS");
>>>
>>>
>>>        if ( $query != 0 or $query != undef or $query != ' ' )  {
>
>
> Please replace this condition by
>
> if ($query)
>
> I'm to confused at the moment to explain why in detail, sorry...
>

Your question below answers why.....

>
> ==================
>
> hi all
>
> while trying to prove by code that the above if condition is incorrect...
>
>    My logic says that a condition in the form
>    if ($a != $val1 or $a != $val2)
>    is always true.

Unless both are false.... which is a basic premise of 'or'.

>
>    sub test {print "condition is true\n" if $_[0] != 1 or $_[0] != 2}
>    test (1);   test (2);   test (3);
>    # this prints 3 times as expected:
>    condition is true
>
> ... I struggled over something when I included "undef" in some code
tests:
>
>    print "Oups!\n" if 0==undef;
>    # prints:
>    Oups! # <<<<<<<< ?????
>
> Can anybody please explain this result to me.
>
>    print "Oups!\n" if !defined 0;
>    # prints nothing as expected.
>

The key here is that the == and != operators put their operands into
numerical context. 'undef' in numerical context resolves to 0. So the
first case is true.  In the second case you are *specifically* checking
for definedness (not matching a value), and 0 *is* defined.

>
> I thought that a had a clue of perl after using it intensively for years.
> I'm not shure anymore...
>

Understanding undefined vs. true/false vs. 0 vs. some other value is
definitely a difficult thing to grasp and unusual to HLLs where context
can automagically switch values.

So back to your confusion above, you want to check,

if ($query)

Because 'undef' resolves to false. However, if $query could contain a
false value (namely 0) then you would have to check specifically for
undefinedness,

if (defined $query)

and this becomes a nasty bug when not being paid special attention, but
is also one reason why turning of initialization warnings is often a bad
idea.

Just to blow your mind, look around for "zero but true" ;-)....

>
>
> greetings joe
>

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