Programming Perl, Second Edition

Previous Chapter 2 Next
 

2.2 Built-in Data Types

Computer languages vary in how many and what kinds of data types they provide at compile time. Unlike some commonly used languages that provide many types for similar kinds of values, Perl provides just a few built-in data types. (You can, however, define fancy dynamic types via the object-oriented features of Perl--see Chapter 5, Packages, Modules, and Object Classes.) Perl has three basic data types: scalars, arrays of scalars, and hashes of scalars, also known as associative arrays.

Scalars are the fundamental type from which more complicated structures are built. A scalar stores a single, simple value, typically a string or a number. Elements of this simple type can be combined into either of the two composite types. An array is an ordered list of scalars that you access with a numeric subscript (subscripts start at 0).[1] A hash is an unordered set of key/value pairs that you access using strings (keys) as subscripts, to look up the scalar value corresponding to a given key. Variables are always one of these three types. (Other than variables, Perl also has some partially hidden thingies called filehandles, directory handles, subroutines, typeglobs, and formats, which you can think of as data types.)

[1] As in C, all of Perl's indexing starts with zero. (A negative subscript counts from the end, though.) This applies to various substring and sublist operations as well as to regular subscripting.


Previous Home Next
Lexical Texture Book Index Terms