Perl Variables
Beginning |
Previous |
Next
Built-in Variable Types
The built-in variables types that Perl 5 supports include:
- Scalars
- Arrays, or Lists
- Associative Arrays, or Hashes
- Hard and Symbolic References and Typeglobs
- File Handles
- Special Variables
Scalar and Array Contexts
If a function returns an array and the left-hand side of the equals
sign is an array, it is an array context. If the left-hand side is a
scalar, it is a scalar context. For example, if we use the built-in
function "grep()" to scan an array for a
regular expression, and use it in an array context, as in:
# @source_code is an array that hold the contents of a file,
# each line per array element
@matches = grep (/;$/, @source_code);
we would end up with an array "@matches"
that holds what grep found. Now if we use it in a scalar context, as in:
# @source_code is an array that hold the contents of a file,
# each line per array element
$num_matches = grep (/;$/, @source_code);
instead, we get the number of times grep found a
match.
Beginning |
Previous |
Next
Last Modified: $Date: 1997/05/02 07:17:52 $