Perl goto statement

Perl goto statement Perl loop

Perl has three goto forms: got LABLE, goto EXPR, and goto and NAME:

Serial number Goto type
1 goto LABEL

Find the statement marked LABEL and re-execute from there.

2 goto EXPR

The goto EXPR form is just a general form of goto LABEL. It expects the expression to generate a tag name and jump to that tag to execute.

3 goto &NAME

It replaces the child process that is running with a call from a named child process.

Grammar

The syntax format looks like this:

goto LABEL

或

goto EXPR

或

goto &NAME

Flow chart

Perl goto statement

Instance

The following two instances jump out $a variable is 15.

Here's a common example of goto:

#/usr/bin/perl
   
$a = 10;
LOOP:do
{
    if( $a == 15){
       # 跳过迭代
       $a = $a + 1;
       # 使用 goto LABEL 形式
       goto LOOP;
    }
    print "a = $a\n";
    $a = $a + 1;
}while( $a < 20 );

The above procedure is performed and the output is:

a = 10
a = 11
a = 12
a = 13
a = 14
a = 16
a = 17
a = 18
a = 19

The following examples use goto EXPR. W e used two strings and used a dot (.) to link.

$a = 10;
$str1 = "LO";
$str2 = "OP";

LOOP:do
{
    if( $a == 15){
       # 跳过迭代
       $a = $a + 1;
       # 使用 goto EXPR 形式
       goto $str1.$str2;    # 类似 goto LOOP
    }
    print "a = $a\n";
    $a = $a + 1;
}while( $a < 20 );

The above procedure is performed and the output is:

a = 10
a = 11
a = 12
a = 13
a = 14
a = 16
a = 17
a = 18
a = 19

Perl goto statement Perl loop