|
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] |
$jth element of the $ith
element of array @var. |
| $var{'Feb'} |
one 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}.
|
Operators
| ** |
|
Exponentiation |
| + - |
* / |
Addition, subtraction, multiplication, division |
| % |
|
Modulo division |
| & | |
^ |
Bitwise AND, bitwise OR, bitwise exclusive
OR |
| >> |
<< |
Bitwise shift right, bitwise shift left |
| || |
&& |
Logical OR, logical AND |
| . |
|
Concatenation of two strings |
| x |
|
Returns a string or array consisting
of the left operand (an array or a string) repeated the
number of times specified by the right operand |
| All of the above operators also
have an assignment operator, e.g., .= |
| -> |
|
Dereference operator |
| \ |
|
Reference (unary) |
| ! |
~ |
Negation (unary), bitwise complement (unary) |
| ++ |
-- |
Auto-increment (magical on strings), auto-decrement |
| == |
!= |
Numeric equality, inequality |
| eq |
ne |
String equality, inequality |
| < |
> |
Numeric less than, greater than |
| lt |
gt |
String less than, greater than |
| <= |
>= |
Numeric less (greater) than or equal to |
| le |
ge |
String less (greater) than or equal to |
| <=> |
cmp |
Numeric (string) compare. Returns -1, 0,
1. |
| =~ |
!~ |
Search pattern, substitution, or translation
(negated) |
| .. |
|
Range (scalar context) or enumeration (array
context) |
| ?: |
|
Alternation (if-then-else) operator |
| , |
|
Comma operator, also list element separator.
You can also use =>. |
| not |
|
Low-precedence negation |
| and |
|
Low-precedence AND |
| or |
xor |
Low-precedence OR, exclusive OR |
All Perl functions can be used as list operators,
in which case they have very high or very low precedence,
depending on whether you look at the left or the right side
of the operator. Only the operators not, and,
or and xor have lower precedence.
A "list" is a list of expressions, variables,
or lists. An array variable or an array slice may always be
used instead of a list.
Parentheses can be added around the parameter
lists to avoid precedence problems.
|
Statements
Every statement is an expression, optionally
followed by a modifier, and terminated by a semicolon. The
semicolon may be omitted if the statement is the final one
in a BLOCK.
Execution of expressions can depend
on other expressions using one of the modifiers if,
unless, while or until, for example:
EXPR1
if EXPR2 ;
EXPR1
until EXPR2 ;
The logical operators ||,
&& or ?: also allow
conditional execution:
EXPR1
|| EXPR2 ;
EXPR1
? EXPR2 :
EXPR3 ;
Statements can be combined
to form a BLOCK when enclosed in {}. Blocks
may be used to control flow:
if (EXPR)
BLOCK [ [ elsif (EXPR)
BLOCK ... ] else BLOCK ]
unless (EXPR) BLOCK
[ else BLOCK ]
[ LABEL: ] while (EXPR) BLOCK [ continue
BLOCK ]
[ LABEL: ] until (EXPR) BLOCK [ continue
BLOCK ]
[ LABEL: ] for (EXPR;
EXPR; EXPR) BLOCK
[ LABEL: ] foreach VAR† (LIST) BLOCK
[ LABEL: ] BLOCK [ continue BLOCK ]
Program flow can be controlled with:
| goto LABEL |
Continue execution at the specified
label. |
| last [ LABEL
] |
Immediately exits the loop in question.
Skips continue block. |
| next [ LABEL
] |
Starts the next iteration of the loop. |
| redo [ LABEL
] |
Restarts the loop block without evaluating
the conditional again. |
Special forms are:
do BLOCK while
EXPR ;
do
BLOCK until EXPR ;
which are guaranteed to perform
BLOCK once before testing EXPR, and
do BLOCK
which effectively turns BLOCK into
an expression.
|