Open In App

Perl | goto statement

Improve
Improve
Like Article
Like
Save
Share
Report

The goto statement in Perl is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.
Syntax:

LABEL:
Statement 1;
Statement 2;
.
.
.
.
.
Statement n;
goto LABEL;

In the above syntax, the goto statement will instruct the compiler to immediately go/jump to the statement marked as LABEL. Here label is a user-defined identifier which indicates the target statement. The statement immediately followed after ‘label:’ is the destination statement.
goto
goto statement in Perl is of three forms- Label, Expression, and Subroutine.

  1. Label: It will simply jump to the statement marked with the LABEL, and will continue the execution from that statement.
  2. Expression: In this form, there will be an expression that will return a Label name after evaluation and goto will make it jump to the labeled statement.
  3. Subroutine: goto will transfer the compiler to the subroutine of the given name from the currently running subroutine.

Syntax:

goto LABEL

goto EXPRESSION

goto Subroutine_Name

goto using LABEL name: LABEL name is used to jump to a specific statement in code and will start execution from that statement. Its reach is limited though. It can work only within the scope from where it is being called.
Example:




# Perl program to print numbers 
# from 1 to 10 using goto statement 
  
# function to print numbers from 1 to 10 
sub printNumbers() 
    my $n = 1; 
label: 
    print "$n "
    $n++; 
    if ($n <= 10) 
    {
        goto label;
    }
    
# Driver Code
printNumbers(); 


Output:

1 2 3 4 5 6 7 8 9 10

goto using Expression: An expression can also be used to give a call to a specific label and pass the execution control to that label. This expression when passed to the goto statement, evaluates to generate a label name, and further execution is continued from that statement defined by that label name.
Example:




# Perl program to print numbers 
# from 1 to 10 using the goto statement 
  
# Defining two strings 
# which contain 
# label name in parts
$a = "lab";
$b = "el";
  
# function to print numbers from 1 to 10 
sub printNumbers() 
    my $n = 1; 
label: 
    print "$n "
    $n++; 
    if ($n <= 10) 
    {
        # Passing Expression to label name
        goto $a.$b;
    }
    
# Driver Code
printNumbers(); 


Output:

1 2 3 4 5 6 7 8 9 10

goto using Subroutine: A subroutine can also be called with the use of the goto statement. This subroutine is called from within another subroutine or individually based on its use. It holds the work that is to be performed next to the calling statement. This method can be used to call a function recursively to print a series or a range of characters.
Example:




# Perl program to print numbers 
# from 1 to 10 using goto statement 
  
# function to print numbers from 1 to 10 
sub label
{
    print "$n ";
    $n++;
      
    if($n <= 10)
    {
        goto &label;
    }
}
  
# Driver Code
my $n = 1;
label(); 


Output:

1 2 3 4 5 6 7 8 9 10


Last Updated : 20 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads