Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

"undef" Value and Undefined Variables

This section describes what is the 'undef' value and what is an undefined variable. A scalar, array or hash variable without any value assigned is an undefined variable, which is interpreted as 0, '', or FALSE depending on the context.

Perl also supports a special value called "undef", which is used to indicate the status of an undefined variable.

The "undef" value and undefined variables can be used in a Perl script under these rules:

  • A scalar variable without a scalar value assigned is an undefined variable.
  • A array variable without a list value assigned, or assigned with an empty list, is an undefined variable.
  • A hash variable without a list value assigned, or assigned with an empty list, is an undefined variable.
  • The undef() function removes the value from a scalar variable, an array variable or a hash variable, and makes it an undefined variable.
  • An undefined variable is equal to the undef value in a comparison operation.
  • An undefined variable will be interpreted as 0 in an operation, if a numerical value is expected.
  • An undefined variable will be interpreted as "" in an operation, if a string value is expected.
  • An undefined variable will be interpreted as FALSE in an operation, if a Boolean value is expected.
  • An undefined variable will be interpreted as an empty list, if a list value is expected.

Here is a Perl tutorial script on how to use undef value and undefined variables:

#- UndefinedVariable.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#

#- Undefined scalar variable   
   $price;
   $string = '('.$price.')';           # Interpreted as ""
   print($string, "\n");
   
   $price = $price + 3.99;             # Interpreted as 0
   print($price, "\n");              

   undef($price);                      # Undefined again

   print("undef", "\n") if ($price == undef);
                                       # Interpreted as undef

   print("priceless!", "\n") if (! $price);
                                       # Interpreted as FALSE

#- Undefined array variable   
   @links;                             # Undefined
   @links = (@links, "herongyang.com", "perl.org", "cpan.org");
   print(@links, "\n");                # Interpreted as empty list

   undef(@links);                      # Undefined again

   print("undef", "\n") if (@links == undef);
                                       # Interpreted as undef

   print("empty array!", "\n") if (! @links);
                                       # Interpreted as FALSE

#- Undefined hash variable   
   %siteRanks;                         # Undefined
   %siteRanks = (%siteRanks, "herongyang.com", 4, 
      "perl.org", 6, "google.com", 9);
   print(%siteRanks, "\n");            # Interpreted as empty list

   undef(%siteRanks);                  # Undefined again

   print("undef", "\n") if (%siteRanks == undef);
                                       # Interpreted as undef

   print("empty hash!", "\n") if (! %siteRanks);
                                       # Interpreted as FALSE

Here is the output of the tutorial script:

()
3.99
undef
priceless!
herongyang.comperl.orgcpan.org
undef
empty array!
herongyang.com4google.com9perl.org6
undef
empty hash!

Sections in This Chapter

Scalar Values and List Values

Scalar Value Constructors

Scalar Value Interpretation

List Value Constructors

Variables - Scalar, Array and Hash

Using Scalar Variables

Using Array Variables

Using Hash Variables

"undef" Value and Undefined Variables

Dr. Herong Yang, updated in 2008
"undef" Value and Undefined Variables