Webmaster in a Nutshell

Previous Chapter 15
Perl Quick Reference
Next
 

15.3 Variables

$var

A simple scalar variable.

$var[28]

29th element of array @var.

$p = \@var

Now $p is a reference to array @var.

$$p[28]

29th element of array referenced by $p.

Also, $p->[28].

$var[-1]

Last element of array @var.

$var[$i][$j]

$j-th element of $i-th element of array @var.

$var{'Feb'}

A value from hash (associative array) %var.

$p = \%var

Now $p is a reference to hash %var.

$$p{'Feb'}

A value from hash referenced by $p.

Also, $p->{'Feb'}.

$#var

Last index of array @var.

@var

The entire array; in a scalar context, the number of elements in the array.

@var[3,4,5]

A slice of array @var.

@var{'a','b'}

A slice of %var; same as ($var{'a'},$var{'b'}).

%var

The entire hash; in a scalar context, true if the hash has elements.

$var{'a',1,...}

Emulates a multidimensional array.

('a'...'z')[4,7,9]

A slice of an array literal.

pkg::var

A variable from a package, e.g., $pkg::var, @pkg::ary.

\object

Reference to an object, e.g., \$var, \%hash.

*name

Refers to all objects represented by name.

*n1 = *n2 makes n1 an alias for n2.

*n1 = $n2 makes $n1 an alias for $n2.

You can always use a { block } returning the right type of reference instead of the variable identifier, e.g., ${...}, &{...}. $$p is just a shorthand for ${$p}.


Previous Home Next
Literals Book Index Operators

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell