Control structure learning notes in Perl


1、 Types of statements

A computer is a tool that has the advantage of performing repetitive tasks Loop execution, loop statement.
A lot of judgment needs to be made, and conditional statements are executed according to conditions.
Sequential statements.
Summary of true and false values of expressions
The expression is not necessarily a logical expression, but the true and false values must be obtained.
False value:
The logical value is false
The value is 0
The string is empty
The list is empty
Other cases are true

2、 Conditional statement if



Copy codeThe code is as follows:

If (expression) {spaces are ignored and line breaks can be different
If (expression)
Elsif (expression) {statement



(1) Elsif and else can be omitted
(2) Notice the elsif keyword
(3) Statement block {} must have, but can be empty
(4) Can be nested
(5) Meaning: execute when true, and the expression is the execution condition

3、 Conditional statement unless


Copy codeThe code is as follows:

Unless (expression) {statement}


Contrary to the meaning of if condition, execute the statement unless the expression is true. If it is true, the expression is an exit condition

4、 Loop statement while


Copy codeThe code is as follows:

While (expression) {statement}


(1) When the expression is true, it is a condition for execution
(2) The condition must be set to be false, otherwise it cannot exit and unlimited cycle
Do {statement} while (expression)
Execute at least once, execute once before judgment.

5、 Loop statement until


Copy codeThe code is as follows:

Until (expression) {statement}

(1) In contrast to while, execute the statement until the expression is true
(2) Stop when true, and the expression is an exit condition

Copy codeThe code is as follows:

Do {statement} until expression)

Execute the statement at least once before conditional judgment


6、 For loop

For (expression 1; expression 2; expression 3) {statement}
(1) Expression 1: initializing control variables or other variables is only executed at the first loop, which can be executed by
Assign values to multiple variables separated by commas
(2) Expression 2: compare at each loop, loop when true
(3) Expression 3: execute each time after the loop is executed, and change the value of the control variable, which is generally self increasing
(4) All three expressions can be omitted, but two; Must be retained for(;;)
(5) First, all three expressions can be used to execute multiple statements.

For example: for ($line = < >, $count = 1; $count < 10; $line = < >, $count + +) {print $line;}

7、 Foreach statement

Foreach $W (list or array) {statement}
(1) () is an array @a, or a list (1,2,3)
(2) $W can be a new variable or a used variable. The variable value will be restored after the loop ends
(3) Each loop assigns the values in the list or array to $W in turn until the list is empty. You don’t have to care about the length of the array
(4) If the value of $W is modified in the loop body, the corresponding data element value also changes
(5) When the list is a constant, the value of $W cannot be modified
(6) $W and @ a can be the same, such as $a (@ a)
(7) For and foreach are interchangeable and universal

Foreach form
(1) Foreach $a (@ a) {} general form
(2) Foreach $a (1,2,3,4) {} list
(3) Foreach $K (keys% h) {} hash form
(4) Foreach $a (@ a [1,2,3]) {} only for some elements of the array
(5) Foreach (@ a) {} omits the loop variable and defaults to$_
    $_ The default variable is print$_ Variable
(6) Grep and map functions are equivalent to foreach operating on each element

8、 Cycle control

Last: exit loop
Next: execute the next loop. The statement under next in the loop will not be executed this time. For for for, the variable to be executed increases automatically
Redo: re execute this cycle. For for not executing variable auto increment, it is invalid in do statement and is not recommended
Goto: jump. You can’t jump into a loop or subroutine. It’s not recommended for structured programming now

9、 Mark

Marks can only be composed of letters and underscores, which are generally capitalized and separated by:.
Tag definition: defined before the loop statement or before the statement (goto)
Generally, when the inner layer of a nested loop jumps to the outermost layer, where last is to exit the outermost loop and the program continues to go down,
Not jump to the outermost layer


Copy codeThe code is as follows:

Print “use label:”;
             print “$j”;
             last LABEL if $i==3;
print “\$i=$i”;


10、 Continue statement block

Continue {statement}

(1) Immediately after the loop statement, it is executed after the loop body is executed and before the next conditional judgment
(2) It can also be used after the statement block identified by {}
(3) Do not execute after jumping out from last, but execute next

11、 Single line conditional statement and circular statement

Conditional statement: statement before condition print $a if $a==0; But test the condition before executing the statement

Copy codeThe code is as follows:

unless: print $a unless($a==0);
while: print $a while($a–==0);

Note: loop statements should not lose control variables, resulting in an endless loop
Conditional statements with |, & &: $a = = 0 & & print $a;
                  open(F,’file’)||die “can’t open”;
Die function: exit the program after the console outputs a standard error message
Warn: after outputting information, it does not exit the program, but only serves as an alarm
$!: Internal variable contains error code
[email protected]: internal variable contains error information


12、 Summary

1. Conditional statement:

Copy codeThe code is as follows:

If (expression) {statement}
Elsif (expression) {statement}
Else {statement}


2. Single line statement if (expression);

3. | or & & and form

4.?: form

5. Unless (expression) {statement}
Single line statement unless (expression);

6. While until loop

While (expression) {statement}
Do {statement} while (expression)
While (expression) {statement} continue {statement}
Single line statement while (expression)
Until (expression) {statement}
Do {statement} until (expression)
Until (expression) {statement} continue {statement}
Single line statement until (expression)

7. For foreach loop

For (expression 1; expression 2; expression 3) {statement}
Foreach (@ a) {statement}
Foreach $K (keys% hash) {statement}

8. Cycle control: last, next, redo

9. Mark: goto label

Recommended Today

Ios15 adaptation

Need immediate modification 1. Uinavigationbar, uitoolbar and uitabbar styles Uinavigationbar, uitoolbar and uitabbar need to use uibarappearance APIs to set colors. // UINavigationBar let navigationBarAppearance = UINavigationBarAppearance() navigationBarAppearance.backgroundColor = .red navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance navigationController?.navigationBar.standardAppearance = navigationBarAppearance // UIToolbar let toolBarAppearance = UIToolbarAppearance() toolBarAppearance.backgroundColor = .blue navigationController?.toolbar.scrollEdgeAppearance = toolBarAppearance navigationController?.toolbar.standardAppearance = toolBarAppearance // UITabBar let tabBarAppearance […]