Updated April 4, 2023
Introduction to Goto in Perl
Goto in Perl is a statement that was also called jump statement, goto statement sometimes referred to as unconditional jump statement. Goto statement is basically used as a jump from one statement to another statement or jump within the same function which was used in a program. This statement is very important and useful to jump within in function or one statement to another statement. There are three forms of goto statement in Perl is label, expression, and goto & name, these three basic forms is used in Perl goto statement.
Syntax:
Below is the syntax which is as follows.
1.
LABEL: -- Jumps statement using label.
Statement 1;
Statement 2;
… -- Statement is nothing but expression which we have used in code.
…
Statement N;
Goto label; -- Jumps statement using label.
2.
Goto label -- Jumps statement using label.
OR
Goto EXPR -- Jumps statement using expressions.
OR
Goto &NAME -- It is used to substitute call of subroutines.
Jumps statement using subroutines.
Parameters
Below is the parameter description syntax:
1. Label: The label is used for jump from one statement to another statement or jump within the same function using a goto statement in Perl. Label is very important and useful.
2. Statement 1 to Statement N: We can use N number of the statement in goto label statement.
3. Goto: Goto statement which was also called as jump statement, goto statement sometimes referred to as unconditional jump statement in Perl.
4. Expression: The expression in goto statement state that it will expect return expression as a label name and then jumps to a labeled statement. It is a generalization of the global statement in Perl goto statement.
5. &Name: This is used to substitute the call of subroutines in Perl goto statement.
Flowchart
Below is the pictorial representation of the flowchart
- In the above diagram flow of goto statement will start from starting of the goto statement. We need to start a statement using the start keyword.
- Then the compiler will go to label 1 in label 1 we have used statement 1, the compiler will execute the label1 statement as statement 1.
- We have used a total of three labels and three statements to represent a flowchart of Perl goto statement.
- After the execution of the first statement compiler will jump from statement 1 to statement 3. The compiler will execute the third statement.
- The compiler will skip the second statement and directly jumps to the third statement. We have used label in the flowchart.
- The label is defined as it is a user-defined identifier that is used to indicates or define the target statement.
- Label using goto statement is used to jump from one statement to another statement. Or it is used to jump within a function.
- After the execution of the third statement in the above flowchart compiler will directly goto the end statement of the goto statement body. It will not execute the second statement.
How goto Works in Perl?
Below is the working:
1. There are three main forms of statement available.
- Goto Label
- Goto Expression
- Goto &Name
2. These three basic forms are used in perl goto statement.
- Goto Label: It is used to find the name as label after finding name control will stop the execution and executes the next statement of a block. The label is going to anywhere but the scope of this statement is dynamic.
- Goto Expression: The expression in goto statement state that it will expect return expression as a label name and then jumps to a labeled statement.
- Goto &Name: This is used to substitute call of subroutines. This is a very important form.
3. Goto statement is also called as jump statement, goto statement sometimes referred as unconditional jump statement in perl.
4. This statement is very important and useful in perl language.
5. It is basically used as jump from one statement to another statement or jump within the same function which function is used in a program.
Examples
Below are the examples:
Example #1 – using label
Code:
use strict;
use warnings;
# Define function name as goto Label
sub gotoLabel()
{
my $number = 1; ## Define variable name as number.
label: ## Label define in a code with goto statement.
print "$number ";
$number++;
if ($number <= 100)
{
goto label; ##goto statement with label
}
}
gotoLabel ();
Output:
Example #2 – using expression
Code:
use strict;
use warnings;
$a = "lab";
$b = "el";
# Define function name as gotoExpression
sub gotoExpression()
{
my $number = 1; ## Define variable name as number.
label: # Label define in a code with goto statement.
print "$number ";
$number++;
if ($number <= 50)
{
goto $a.$b; # goto statement used with expressions.
}
}
gotoExpression ();
Output :
Example #3 – using subroutine
Code:
## Define function name as gotoLabel
sub gotoSubroutine
{
print "$number "; # Print number
$number++;
if($number <= 200) # Loop used to print the number from 1 to 200
{
goto &gotoSubroutine; ##goto statement with subroutines
}
}
my $number = 1; ## Define variable name as number.
gotoSubroutine ();
Output:
Conclusion
Goto statement in Perl is basically used as jump from one statement to another statement or jump within the same function which was used in a program. It is very important and useful in perl language to jump within in function or one statement to other statements.
Recommended Articles
We hope that this EDUCBA information on “Goto in Perl” was beneficial to you. You can view EDUCBA’s recommended articles for more information.