A variable in Perl is a storage location for a value (values). You can work with values directly, but all but the most trivial code works with variables. A variable is a level of indirection that allows you to describe an operation rather than its effects; it's easier to explain the Pythagorean theorem in terms of the variables a
, b
, and c
than with the side lengths of every right triangle you can imagine. This may seem basic and obvious, but to write robust, well-designed, testable, and composable programs, you must identify and exploit points of genericity wherever possible.
Not all variables require names, but most of the variables you will encounter in your programs will have names. Variables in Perl 5 must conform to the standard rules of identifier naming (names). Variables also have leading sigils.
Variables also have visibility, depending on their scope (scope). Most of the variables you will encounter have lexical scope lexical_scope), as the expected visibility of these variables is local.
Package variables have global visibility throughout the program, but they are only available when prefixed with the name of the containing package. For example:
Within the Store::IceCream
package, $num_flavors
is available without a namespace prefix. Outside of that package, it is available as $Store::IceCream::num_flavors
. If $num_flavors
were a lexical variable (declared with my
), it would not be available outside of the package. Inside the package, it would only be available within the appropriate scope.
In Perl 5, the sigil of the variable in a declaration determines the type of the variable, whether scalar, array, or hash. The sigil of the variable used to access the variable determines the type of access to its value. Sigils on variables vary depending on what you do to the variable. For example, you declare an array as @values
. You access the first element--a single value--of the array with $values[0]
. You access a list of values from the array with @values[ @indices ]
. See the arrays (arrays) and hashes (hashes) sections for more.
Perl 5 variables do not need names; Perl can allocate storage space for variables without storing them in lexical pads or symbol tables. These are anonymous variables. The only way to access them is by reference (references).
Perl 5 variables do not enforce types on their values. You may store a string in a variable in one line, append to that variable a number on the next, and reassign a reference to a function (function_references) on the third. The types of the values is flexible (or dynamic), but the type of the variable is static. A scalar variable can only hold scalars. An array variable only contains lists. A hash variable must contain an even-sized list of key/value pairs.
Assigning to a variable may cause coercion (coercion). The documented way to determine the number of entries in an array is to evaluate that array in scalar context (context_philosophy). Because a scalar variable can only ever contain a scalar, assigning an array to a scalar imposes scalar context on the operation and produces the number of elements in the array:
The relationship between variable types, sigils, and context is vital to a proper understanding of Perl.
Hey! The above document had some coding errors, which are explained below:
- Around line 3:
-
A non-empty Z<>
- Around line 25:
-
A non-empty Z<>
- Around line 75:
-
A non-empty Z<>