I guess this is just a bit off topic, but there are several ways to differentiate between "undef" and numeric zero, but you would want to test for "truthfulness". Here are two ways of returning something that is logically "true", but numeric "0" (undef and a normal "0" are both logically false):
1. return the string "0E0" - this is what the DBI does when the operation succeeded but no rows were altered. 0 * 10**0 = 0*1=0. If the operation fails for some reason, the DBI returns undef. Since "0E0" is a completely valid number, you can do math on it (like add to some counter).
2. return the string "0 but true" - This is a special case coded into Perl. That is also logically true, but weirdly enough this is also a valid numeric string! Even with warnings, you can do math on that string without generating a warning. A string like "0 xyzzy" cannot be used in numeric operations without generating a warning.
The two above techniques are useful to make a single return value do "double duty" for two separate concepts (the "did it work?" flag and the numeric return). In other languages you would need two variables for these.
I actually find being able to test an undefined value to be a great thing and not annoying at all. This is a weird concept to get used to if you are used to working in other languages. Also perhaps worthy of mention is that in Perl 5.10, the // operator was introduced. $var //=12; sets $var to 12 if $var is undefined (no effect if $var is 0).
In reply to Re: 0==undef, that's annoying. Is it?
by Marshall
in thread 0==undef, that's annoying. Is it?
by mascip
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |