25.4. Data Types and Variables

Perl supports the usual range of data types—numeric and string. The next two sections detail the data types available in Perl and how variables are handled.

25.4.1. Data Types

Perl supports three types of data:

  • Scalars

  • Arrays

  • Associative arrays

Perl also supports objects—an unordered container of various data types. Objects are covered in the "Objects" section later in this chapter.

Scalar values are individual numeric, string, or Boolean values. Example scalar values include 3, 3.14159, TRUE, and "character data."

Arrays are collections of scalars. Individual elements in an array are referenced by an index relating to their position in the array with a zero index. For example, in the following string array, "Steve" is element number 3:

"Terri","Ian","Branden","Steve","Tammy"

Associative arrays are arrays that use string identifiers instead of numeric identifiers for elements of the array. Associative arrays are generally used to store data associated with a particular identifier. For example, you could define an associative array to store anniversary dates of employees so that accessing the array with an index of "Steve" would return the date associated with Steve.

25.4.2. Variables

Perl variables are largely untyped; you can use the same variable to contain different scalar values at different times in your scripts. Of course, doing so is generally frowned upon—the point is that variable typing in Perl is left up to the programmer.

Perl variable ...

Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.