Perl Cheat Sheet Page 7

ADVERTISEMENT

6. Statements
Every statement is an expression, optionally followed by a modifier, and
terminated with 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
, e.g.:
EXPR1 if EXPR2
;
EXPR1 until EXPR2
;
The logical operators || , && , or ?: also allow conditional execution, e.g.:
EXPR1
||
EXPR2
;
EXPR1
?
EXPR2
:
EXPR3
;
Statements can be combined to form a
BLOCK
when enclosed in {} .
BLOCK
s 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
y (
[
]
[
]
LABEL:
foreach VAR
LIST
)
BLOCK
continue BLOCK
[
]
[
]
LABEL:
BLOCK
continue BLOCK
Program flow can be controlled with:
goto LABEL
Finds the statement labeled with
and resumes execution there.
LABEL
LABEL
may be an expression that evaluates to the name of a 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.
7. Subroutines, packages and modules
[
]
SUBROUTINE
LIST
Executes a
SUBROUTINE
declared by a preceding
sub
declaration, and
returns the value of the last expression evaluated in
.
SUBROUTINE
SUBROUTINE
can be an expression yielding a reference to code. In this
case you can use &${
EXPR
}( [
LIST
] ) or ${
EXPR
}->( [
LIST
] ) .
[
]
&
SUBROUTINE (
LIST
)
Executes a
not neccesarily declared before being used.
SUBROUTINE
bless REF
[
, CLASSNAME
]
Turns the object
into an object in
. Returns the reference.
REF
CLASSNAME
7

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education