saranperl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: difference b/w undef and empty string
by ELISHEVA (Prior) on Aug 07, 2009 at 10:19 UTC

    To understand the difference, you must understand the distinction between an empty string and being undefined. An empty string, is a string defined to be empty. It is defined. Creating a scalar variable without assigning it anything or assigning it the special symbol undef are two ways of saying "I haven't chosen a value yet. Act accordingly." Thus:

    • my $a; defined($a); returns false.
    • my $a=undef; defined($a); returns false.
    • my $a=''; defined($a); returns true.
    • my $a=""; defined($a); returns true.

    The difference between $a and $a=undef depends on whether or not $a has already been declared. my $a; and my $a=undef; two ways of writing the same thing. However, if you've already declared $a somewhere earlier in the script, $a; is an expression that evaluates to the value of $a. So my $a=1; $a; returns true. On the other hand, $a=undef; clears away whatever definition you gave to $a and makes it undefined again, so my $a=1; $a=undef; returns false.

    There is no difference between $a=''; and $a="". Both assign $a an empty string. However, there is quite a difference if you put something between '...' and "...". For non-empty strings, single quotes are "non-interpolating" and double quotes are "interpolating". That means that between single quotes what-you-see-is-what-you-get: '$a' is just the two characters $ and a. But with double ("interpolating") quotes, Perl evaluates any variables found in between "..." so my $a=1; my $x="$a"; sets $x to "1" since that is the value of $a. For more information, see perlop.

    Best, beth

Re: difference b/w undef and empty string
by dHarry (Abbot) on Aug 07, 2009 at 09:30 UTC
Re: difference b/w undef and empty string
by dsheroh (Monsignor) on Aug 07, 2009 at 11:05 UTC
    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".

      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.

      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.

Re: difference b/w undef and empty string
by ikegami (Patriarch) on Aug 12, 2009 at 14:40 UTC

    The first doesn't change the value of $a.

    There is no difference between the string created by the literals '' and "".

    undef is nothing like the empty string. It's a completely different value. Your confusion probably arises from Perl automatic conversion between data types. When you try to use undef as a string, it evaluates to the empty string and issues a warning. When you try to use undef as a number, it evaluates to zero and issues a warning.

    The number zero being automatically converted to a string: >perl -wle"my $x = 0; print $x" 0 Undef being automatically converted to a string: >perl -wle"my $x = undef; print $x" Use of uninitialized value in print at -e line 1.