laziness, impatience, and hubris | |
PerlMonks |
Re: Checking Variable for Not Nullby sundialsvc4 (Abbot) |
on May 27, 2015 at 15:39 UTC ( [id://1128024]=note: print w/replies, xml ) | Need Help?? |
Another way to say it, is to say that the first example tests for “truthiness.” As others have said, there are many possible values for $ex_date which are “false-y” but which are not what you are (intending to be ...) testing for. And this leads to the very worst kind of bug: the “it seems to work ...” bug. Therefore, even though you are happily dealing with a “DWIM = Do What I Mean” language, you must sometimes be careful to “SWIM = Say What I (Really ...) Mean.” This is one of those occasions. And this, of course, is also in-keeping with a distinction that the SQL language is very careful to draw: “NULL is not False.” NULL is: “the absence of any value at all, therefore neither True nor False.” So, in your application logic, you should take careful pains to draw the same distinction. Test for undef (NULL) “first, and explicitly.” Test for “truthiness” (or better yet, for an explicit value ...) only after you have established that the value is not NULL. Then, throw an exception (die()) if you encounter “none of the above.” When debugging-time comes, you (or your successors) will be glad that you “went to all that ‘extra’ trouble.”
In Section
Seekers of Perl Wisdom
|
|