Latest web development tutorials

Perl goto statement

Perl cycle Perl cycle

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

No. goto type
1 goto LABEL

Find the labeled LABEL statement and re-executed from there.

2 goto EXPR

goto EXPR goto LABEL form is the general form. It looked forward to an expression generates a tag name, and jump to the mark to perform.

3 goto & NAME

It is a running child process with a call to a named sub-processes.

grammar

The syntax is as follows:

goto LABEL

或

goto EXPR

或

goto &NAME

flow chart

Examples

The following two examples in the variable $ a is 15 out of the output.

The following is a commonly used goto examples:

#/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 program, the output is:

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

The following example uses goto EXPR form. We used two strings and use a dot (.) To the 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 program, the output is:

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

Perl cycle Perl cycle