Skip to content

Latest commit

 

History

History
117 lines (81 loc) · 3.63 KB

variables.pod

File metadata and controls

117 lines (81 loc) · 3.63 KB

Variables

A variable in Perl is a storage location for a value (values). While a trivial program can manipulate values directly, most programs work with variables to simplify the logic of the code. A variable represents values; it's easier to explain the Pythagorean theorem in terms of the variables a, b, and c than by intuiting its principle by producing a long list of valid values. This concept may seem basic, but effective programming requires you to manage the art of balancing the generic and reusable with the specific.

Variable Scopes

Variables are visible to portions of your program depending on their scope (scope). Most of the variables you will encounter have lexical scope (lexical_scope). Files themselves provide their own lexical scopes, such that the package declaration on its own does not create a new scope:

As of Perl 5.14, you may provide a block to the package declaration. This syntax does provide a lexical scope:

Variable Sigils

The sigil of the variable in a declaration determines the type of the variable: scalar, array, or hash. The sigil used when accessing a variable varies depending on what you do to the variable. For example, you declare an array as @values. Access the first element--a single value--of the array with $values[0]. Access a list of values from the array with @values[ @indices ].

Anonymous Variables

Perl variables do not require names. Names exist to help you, the programmer, keep track of an $apple, @barrels, or %cheap_meals. Variables created without literal names in your source code are anonymous variables. The only way to access anonymous variables is by reference (references).

Variables, Types, and Coercion

A variable in Perl 5 represents both a value (a dollar cost, available pizza toppings, guitar shops with phone numbers) and the container which stores that value. Perl's type system deals with value types and container types. While a variable's container type--scalar, array, or hash--cannot change, Perl is flexible about a variable's value type. 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.

Performing an operation on a variable which imposes a specific value type may cause coercion (coercion) from the variable's existing value type.

For example, 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 an array evaluated in scalar context returns the number of elements in the array:

This relationship between variable types, sigils, and context is essential.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 16:

A non-empty Z<>

Around line 65:

A non-empty Z<>