in reply to difference b/w undef and empty string

The difference between undef and an empty string is exactly the same as the difference between (SQL) NULL and an empty string:

undef means "I don't know what the value is or even if there is a value".

empty string means "I know what the value is, and it is nothing".

  • Comment on Re: difference b/w undef and empty string

Replies are listed 'Best First'.
Re^2: difference b/w undef and empty string
by Marshall (Canon) on Aug 07, 2009 at 12:47 UTC
    That may be true in a scalar context, but a point about list context:
    In a list context: undef means I know that there is something that is supposed to be here and I don't know what it is (undef IS a value!), '' or "" means null (empty)string which is also a value, meaning "empty".
    So in list or array context, undef means there is a value, but it is unknown.
Re^2: difference b/w undef and empty string
by Jenda (Abbot) on Aug 09, 2009 at 15:34 UTC

    Undef means whatever you design it to mean. For that particular variable. It might mean the exact same thing as an empty string or a zero or something entirely different.

    I mean assume you have a variable that's supposed to contain an object (a blessed reference if you like), does an undef in that variable mean "I don't know what the value is or even if there is a value"? I don't think so. It rather means "there is no such object" or "the constructor of the object failed, do find out why". How's that "I don't know what the value is"?

    "I don't know what the value is or even if there is a value" would be a good description of free variables in Prolog, but not of ALL undefs in Perl.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.