Well, if (undef ==0) will cause "Use of uninitialized value in numeric eq (==)" if warnings are turned on. So undef and zero are not exactly the same.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.