in reply to cgi problem

gjb is absolutely right, but let's go a little bit deeper.

undef == undef, I don't know whether you feel bothered by this fact of Perl.

  1. From a pure "correctness" point of view, I actually feel bothered. There is something in your left hand, and you don't know what it is. There is also something in your right hand, again you don't know what it is. Can you say that you have the same things in both of your hands? You cannot.
  2. Try these two pieces of code:
    if ($a == $b) { print "equals\n"; }
    if ($a eq $b) { print "equals\n"; }
    Both pieces of code print out "equals", but at the same time, both complains that $a and $b are uninitialized. So we DOES observed some inconsistency.

When you use this undef value in a number context, perl evaulates undef to 0, and in a string context, evaluates undef to "" (empty string).

When you say undef == undef, both undef's are 0's, so 0 == 0. When you say undef eq undef, both undef's are "", so "" eq "".

Replies are listed 'Best First'.
Re: Re: cgi problem
by Anonymous Monk on Jan 04, 2003 at 04:50 UTC

    I think your explanation is slightly misleading, because of this fact:

    undef ne "" and undef != 0

    undef is... undef. undef means that the data does not exist at all. It cannot be quantified (it's not even "0 for none", it's "undef for non-existant"), nor can it be given any sort of string value, even if that value is an empty string.

    There is something in your left hand, and you don't know what it is. There is also something in your right hand, again you don't know what it is. Can you say that you have the same things in both of your hands?

    This is not really an accurate description, as undef in this case would not mean you don't know what the object is, but rather that nothing exists in your hands in the first place. I understood what you meant by your post as I understand the reasoning and the behaviour behind the use of undef, though I'm not sure somebody who knows nothing about undef would have understood the difference between undef, 0, and "".

      (Just to make the facts straight,) did you know that your facts are actually on my side. Let's try those two examples you gave:
      if (undef != 0) {#in number context, undef evaluates to 0, and we know + "0 != 0" does not stand. print "pg is wrong!"; } else { print "pg is right!"; }
      and
      if (undef ne "") {#string context print "pg is wrong!"; } else { print "pg is right!"; }
      Both print "pg is right!".

      Also when you say undef is "does not exist". That's also wrong.

      Look at this piece of code:
      my $a;
      The moment I said this, a memory structure, called SV structure is allocated, so $a exists, although it holds undef value.