[Israel.pm] undef vs. ""

sawyer x xsawyerx at gmail.com
Wed Jan 7 00:49:57 PST 2009

You do not understand the "undef" part.

undef is a function, it's not a value type, so you can't test if
something is undefined using "undef", because that will just run the
function "undef" on the variable.

perldoc -f undef
       undef EXPR
       undef   Undefines the value of EXPR, ...

Basically when you have a variable and you want it cleared off, use
"undef" such as:

my $temp = "blah blah blah";
<do some stuff here with $temp>
# i don't need you anymore
undef $temp;

Usually that's not required in Perl, when using the "my" (which you
always should) and writing code in reasonable blocks and functions.
That way the garbage collector gets it and cleans it off for you when
the code block is done.

In order to check if something is not defined, you should use the
"defined" function.
print "it's defined" if defined $";
print "it's undefined" if ! defined $"; # or: not defined $"

The comparison "==" notion is for numbers. you should use "eq" to
check if characters or strings are equal and "ne" to check if
characters or strings are not equal.

if ( defined $" ) {
    print "omg it's defined!\n";
} else {
    print "omg the else is cuddled!\n";

On Wed, Jan 7, 2009 at 10:32 AM, Chanan Berler <chananb at centerity.com> wrote:
> Hi All,
> I wonder if I should consider "" equal to undef.
> If it is so..then is there an empty string? Or there is not
> Please consider this as an example:
> =========================================
> print "undef" if ($" == undef);    ----> will print: undef
> print "found \"\"" if ($" == "");  ----> will print: found ""
> so I can understand that $" == "" == undef
> So why do I get a warning message, when doing this:
> $var = undef . "string";
> But never get this warning message when doing:
> $var = "string" . undef
> Also I found that when trying to do this, no warning is out
> $var = "" . "string";
> $var = "string" . "";
> PS: another thing I found, different warnings when using "use warnings;" -
> Is this a bug in perl, or just a fact? Or lets say a "known issue"
> Thanks
> Chanan
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

More information about the Perl mailing list