[grand-rapids-pm-list] Perl Puzzle!

grand-rapids-pm-list at pm.org grand-rapids-pm-list at pm.org
Fri Oct 10 13:04:13 PDT 2008


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! ;-).

    if ( $a == undef ) {...} # undef converted to zero in numeric comparison
    if ( $a eq undef ) {...} # undef converted to empty string in string
comparison


On Fri, Oct 10, 2008 at 3:53 PM, Ed Eddington <ed.eddington at gmail.com>wrote:

> Thanks for the responses!
>
> 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.
>
> 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:
>
> $b = $a + 10;
> and
> $c = 'Hello ' . $a;
>
> Remember: For arithmetic operations undef behaves like 0, for sting
> operations like a zero length string.
>
> Related Tips:
>
> 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!
>
>     if ( $a == undef ) {...} # $a will always be converted to zero first in
> numeric comparison
>     if ( $a eq undef ) {...} # $a will always be converted to empty string
> first in string comparison
>
> 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).
>
> 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).
>
> Some further reading:
> http://www.softpanorama.org/Scripting/Perlbook/Ch02/variables.shtml
>
> Ed Eddington
> Priority Health
> Grand Rapids, MI
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/grand-rapids-pm-list/attachments/20081010/dee9647e/attachment.html>


More information about the grand-rapids-pm-list mailing list