<div dir="ltr">Ok, I wasn't exactly correct on Tip #1. In these examples, I believe BOTH sides of the relation are first converted based on the operator. So, they do "work", but only because both sides are converted to either zero or the empty string. But, logically, the comparison is incorrect (and should thus be avoided! ;-).<br>
<br> if ( $a == undef ) {...} # undef converted to zero in numeric comparison<br> if ( $a eq undef ) {...} # undef converted to empty string in string comparison<br><br><br><div class="gmail_quote">On Fri, Oct 10, 2008 at 3:53 PM, Ed Eddington <span dir="ltr"><<a href="mailto:ed.eddington@gmail.com">ed.eddington@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr">Thanks for the responses!<br><br>The exact answer I was looking for was Perl's value (non-value?) for undefined variables: "undef". When used in comparisons, an undefined variable is converted to either zero or the empty string depending on the comparison operator!! So, in the first example, $a (undef) evaluates to the numeric value 0, resulting in the comparison 0>=0 - which is true. In the second example, $a evaluates to the string value '' in the string comparison '' ge 0 - which is false. <br>
<br>This chameleon-like behavior holds for other numeric and string operators, too, like '+' and '.' (concatenate), which is what's happening under-the-hood that allows Perl to handle the following with an undefined $a:<br>
<br>$b = $a + 10;<br>and <br>$c = 'Hello ' . $a;<br>
<br>Remember: For arithmetic operations undef behaves like 0, for sting operations like a zero length string.<br><br>Related Tips:<br><br>1. Use if (defined $a) to test for undef. Never use the term 'undef' itself in comparisons! The following are NEVER true – contrary to what you might expect!<br>
<br> if ( $a == undef ) {...} # $a will always be converted to zero first in numeric comparison<br> if ( $a eq undef ) {...} # $a will always be converted to empty string first in string comparison<br><br>2. use warnings! With warnings turned on, you will get a message on stderr like: Use of uninitialized value in string ge at ./test.pl line 18. Sometimes this message can be annoying because you may not really care to check if defined. Also note that the "uninitialized" warnings are only given at runtime. Doing perl –cW test.pl will not give you any warning (not even for if ($a == undef), which I think would be nice because it is not possible).<br>
<br>Like a rumple in the carpet, undef is something easily tripped over again and again! (I know from my own stumblings.) I did a grep through our code base and found a few occurrences of if ($var eq undef).<br><br>Some further reading: <a href="http://www.softpanorama.org/Scripting/Perlbook/Ch02/variables.shtml" target="_blank">http://www.softpanorama.org/Scripting/Perlbook/Ch02/variables.shtml</a><br>
<br>Ed Eddington<br>Priority Health<br>Grand Rapids, MI<br></div>
</blockquote></div><br></div>