next up previous contents
Next: Arrays Up: Variables Previous: Variables   Contents

Basics

There are three basic types of variables in perl: scalar variables (whose names all begin with $), array variables (whose names all begin with @), and which are collections of objects indexed by number , and hashes (whose names all begin with %), and which are collections of objects indexed by strings. Like C, arrays in perl are zero-based, that is, the first element is indexed by the value 0, and the $n$th element will have index $n - 1$.

The names you use for variables (after the special symbol) follow the following rules. Variable names must begin with a letter or underscore, and may contain letters, digits or underscores. Perl has an internal limit of 256 characters for variable names. Beyond these rules, there are special names, reserved primarily for internal use, which begin with digits or punctuation marks. Finally, two colons (::) are used in perl variable names to specify the package within which a variable or function my be found, such as IO::File.

Besides the fact that perl requires all its variables to begin with a special symbol, perl treats its variables different than most other languages in other ways as well. For example, by default all variables in perl are said to be global. That means that when you refer to a symbol anywhere in your perl program, you'll always be referring to the same thing, unless you explicitly create a local variable with the same name.

Another unusual fact about variables in perl is that there are separate namespaces for each of the three types of variables (scalars, arrays and hashes). Thus, in the same program, you can have variables named $x, @x and %x, each representing entirely different things. It is highly recommended that you avoid the practice of having identically named variables, since it is very easy to get them confused.


next up previous contents
Next: Arrays Up: Variables Previous: Variables   Contents
Phil Spector 2002-10-18