Node:Undefined Variables, Previous:Scalar Interpolation, Up:Scalar Variables



Undefined Variables

You may have begun to wonder: what value does a scalar variable have if you have not given it a value? In other words, after:

     use strict;
     my $sweetNothing;
     

what value does $sweetNothing have?

The value that $sweetNothing has is a special value in Perl called undef. This is frequently expressed in English by saying that $sweetNothing is undefined.

The undef value is a special one in Perl. Internally, Perl keeps track of which variables your program has assigned values to and which remain undefined. Thus, when you use a variable in any expression, Perl can inform you if you are using an undefined value.

For example, consider this program:

     #!/usr/bin/perl
     
     use strict;
     use warnings;
     
     my $hasValue = "Hello";
     my $hasNoValue;
     
     print "$hasValue $hasNoValue\n";
     
When this program is run, it produces the following output:
     Use of uninitialized value at line 8.
     Hello
     

What does this mean? Perl noticed that we used the uninitialized (i.e., undefined) variable, $hasNoValue at line 8 in our program. Because we were using warnings, Perl warned us about that use of the undefined variable.

However, Perl did not crash the program! Perl is nice enough not to make undefined variables a hassle. If you use an undefined variable and Perl expected a string, Perl uses the empty string, "", in its place. If Perl expected a number and gets undef, Perl substitutes 0 in its place.

However, when using warnings, Perl will always warn you when you have used an undefined variable at run-time. The message will print to the standard error (which, by default, is the screen) each time Perl encounters a use of a variable that evaluates to undef. If you do not use warnings, the warnings will not print, but you should probably wait to turn off warnings until you are an experienced Perl programmer.

Besides producing warning messages, the fact that unassigned variables are undefined can be useful to us. The first way is that we can explicitly test to see if a variable is undefined. There is a function that Perl provides called defined. It can be used to test if a variable is defined or not.

In addition, Perl permits the programmer to assign a variable the value undef. The expression undef is a function provided by Perl that we can use in place of any expression. The function undef is always guaranteed to return an undefined value. Thus, we can take a variable that already has a value and make it undefined.

Consider the following program:

     #!/usr/bin/perl
     
     use strict;
     use warnings;
     
     my $startUndefined;
     my $startDefined = "This one is defined";
     
     print "defined \$startUndefined == ",
           defined $startUndefined,
           ", defined \$startDefined == ",
           defined $startDefined, "\n";
     
     $startUndefined = $startDefined;
     $startDefined = undef;
     
     print "defined \$startUndefined == ",
           defined $startUndefined,
           ", defined \$startDefined == ",
           defined $startDefined, "\n";
     
Which produces the output:
     defined $startUndefined == , defined $startDefined == 1
     defined $startUndefined == 1, defined $startDefined ==
     

Notice a few things. First, since we first declared $startUndefined without giving it a value, it was set to undef. However, we gave $startDefined a value when it was declared, thus it started out defined. These facts are exemplified by the output.

To produce that output, we did something that you have not seen yet. First, we created some strings that "looked" like the function calls so our output would reflect what the values of those function calls were. Then, we simply used those functions as arguments to the print function. This is completely legal in Perl. You can use function calls as arguments to other functions.

When you do this, the innermost functions are called first, in their argument order. Thus, in our print statements, first defined $startUndefined is called, followed by defined $startDefined. These two functions each evaluate to some value. That value then becomes the argument to the print function.

So, what values did defined return? We can determine the answer to this question from the printed output. We can see that when we called defined on the variable that we started as undefined, $startUndefined, we got no output for that call (in fact, defined returned an empty string, ""). When we called defined on the value that we had assigned to, $startDefined, we got the output of 1.

Thus, from the experiment, we know that when its argument is not defined, defined returns the value "", otherwise known as the empty string (which, of course, prints nothing to the standard output when given as an argument to print).

In addition, we know that when a variable is defined, defined returns the value 1.

Hopefully, you now have some idea of what an undef value is, and what defined does. It might not yet be clear why defined returns an empty string or 1. If you are particularly curious now, see A Digression--Truth Values.