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>