Perl goto Statement
goto is a control flow statement in the Perl programming language that allows you to jump the execution of the program to a specified label.
Perl has three forms of goto:goto LABLE, goto EXPR, and goto &NAME:
| No. | goto types |
|---|---|
| 1 | goto LABEL
Find the statement marked with LABEL and resume execution from there. |
| 2 | goto EXPR
The goto EXPR form is just a general form of goto LABEL. It expects the expression to produce a label name and jumps to that label for execution. |
| 3 | goto &NAME
It replaces the currently running subprocess with a call to a named subprocess. |
Syntax
The syntax format is as follows:
goto LABEL
或
goto EXPR
或
goto &NAME
Flowchart

Example
The following two examples jump out of output when the variable $a is 15.
The following is a commonly used goto example:
Example
#/usr/bin/perl
$a
=
10
;
LOOP
:
do
{
if
(
$a
==
15
)
{
Skip iteration
$a
=
$a
1
;
Using the goto LABEL form
"
Jump out of output
\n
"
;
goto
LOOP
;
"
This statement will not be executed
\n
"
;
}
"
a =
$a
\n
"
;
$a
=
$a
1
;
}
while
(
$a
<
20
);
Running 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 the goto EXPR form. We use two strings and concatenate them with the dot (.) operator.
Example
$a
=
10
;
$str1
=
"
LO
"
;
$str2
=
"
OP
"
;
LOOP
:
do
{
if
(
$a
==
15
)
{
Skip iteration
$a
=
$a
1
;
Using the goto EXPR form
goto
$str1
.
$str2
;
Similar to goto LOOP
}
"
a =
$a
\n
"
;
$a
=
$a
1
;
}
while
(
$a
<
20
);
Running the above program, the output is:
a = 10
a = 11
a = 12
a = 13
a = 14
a = 16
a = 17
a = 18
a = 19
Note:Using goto often makes code difficult to understand and maintain, so in most cases it should be avoided; try to use structured control flow statements (such as if, else, while, for, etc.) to write code.
Perl Loops