Perl Structure Control Statements:
If condition statement:
if (condition) {
Expression
} elsif {
Expression
} else {
Expression
}
Unless conditional statements:
The condition is false when executed.
Unless (condition) {
Expression
} elsif {
Expression
} else {
Expression
}
While Loop statement:
while (condition) {
Expression
}
while (1) {
...
} #死循环
Until Loop statement:
Execute only when the condition is false
Until (condition) {
Expression
}
foreach Loop statement:
foreach $var (@arry/list) {
Expression
}
$var loop variable is not a copy of the list element, but the list element itself.
If you modify the loop variable in the foreach body, you modify the elements in the list.
$var loop variable restores the original value after exiting the Foreach loop.
The loop variable can be omitted, and the default is to use $_ instead:
foreach (@arry/list) {
print "$_";
}
For Loop statement:
The For and foreach equivalents in Perl can be converted to each other according to the conditions.
for (initialization; condition; increment) {
Expression
}
for (;;) {
...
} #死循环
Bare BLOCK:
{
Expression
}
Last function : Jumps out of the loop, equivalent to the break of C.
Last label/expr
next function : Jump out of this cycle, continue the next cycle, the equivalent of C continue.
Next label/expr
redo function : Unconditionally jump to the top of this cycle, re-execute this cycle.
Redo Label/expr
Given-when Control Structure:
Given ($var) {
When (condition) {expression}
...
default {expression}
}
When matching for multiple projects:
foreach (@arr) {
When (condition) {expression; continue}
...
default {expression}
}
break: Jump out of given block
continue: continue with the next sentence
Conditional modifiers:
dosomething if condition;
Can also be written as:
DoSomething
if condition;
tags in the loop:
A jump can be achieved by adding a tag to the Loop keyword such as for/while.
The label is capitalized, and can be alphanumeric and glide, but not a number.
Eval Expr/block: Capturing errors
special variables [email protected] Store the error message if no exception is triggered [email protected] is empty.
eval {
....
}
if ([email protected]_) {
Print "An error occurred ([email protected]), continuing\n";
}
Evalbytes
Goto
Return
Exit
Do
Wantarray
Dump
Caller
"__file", "__line__", "__package__", "__sub__"
Perl: Control structure