There are seven separate kinds of variables or variable-like things in Perl: scalar variables, array variables, hash variables , subroutine names, format names , filehandles, and directory handles. Each of these different kinds of variables has its own namespace . Changing the value of one kind of variable does not in any way affect the value of another kind of variable with the same name . For example, the scalar variable $a is independent of the array variable @a :
Also, each package (see Item 42) in a Perl program defines its own set of namespaces. For example, $a in package main is independent of $a in package foo :
You have to look to the right as well as the left of an identifier, as Perl does, to determine what kind of variable the identifier refers to. For example, the syntax for accessing elements of arrays and hashes begins with $ , not @ or % . The $ means that the result is a scalar value, not that you are referring to a scalar variable:
Not all variable-like things in Perl are prefixed with punctuation characters . Subroutine names can be prefixed with an ampersand, but the ampersand is generally optional. In some cases, parentheses around the subroutine arguments can also be omitted:
Filehandles, format names, and directory handles are not prefixed with punctuation characters, but are recognized in context: The filehandle, format name, and dirhandle below are independent of one another, even though they are all named TEST.
It's not necessarily bad programming style to take advantage of Perl's independent namespaces by giving two different kinds of variables the same name. Sometimes it even seems like the sensible thing to do:
|