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

Variables - Scalar, Array and Hash

This section describes Perl variable types: Scalar, Array, and Hash. Variable names must be prefixed with special symbols to indicate their types, $, @ and %.

Perl supports 3 types of variables: Scalar, Array, and Hash.

1. Scalar Variable - A variable to hold a scalar value. The name of a scalar variable must be prefixed with the dollar sign, $, and followed by an identifier, $identifier. For example: $price and $url.

2. Array Variable - A variable to hold a list value. The name of an array variable must be prefixed with the at sign, @, and followed by an identifier, @identifier. For example: @orders and @links.

3. Hash Variable - A variable to hold an associative array, which is a special list value - members are grouped as pairs of keys and values. The name of an array variable must be prefixed with the percent sign, %, and followed by an identifier, %identifier. For example: %priceList and %siteRanks.

Like in many other programming languages, a variable name identifier in Perl is a string beginning with a letter or underscore, and containing letters, underscores, and digits.

Variables for different data types are in different name spaces. You could use the same variable identifier for a scalar, an array, and a 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
Variables - Scalar, Array and Hash