Perl goto Function



This function has three forms, the first form causes the current execution point to jump to the point referred to as LABEL. A goto in this form cannot be used to jump into a loop or external can only jump to a point within the same scope.

The second form expects EXPR to evaluate to a recognizable LABEL. In general, you should be able to use a normal conditional statement or function to control the execution of a program, so its use is deprecated.

The third form substitutes a call to the named subroutine for the currently running subroutine. The new subroutine inherits the argument stack and other features of the original subroutine; it becomes impossible for the new subroutine even to know that it was called by another name.


Following is the simple syntax for this function −

goto LABEL

goto EXPR

goto &NAME

Return Value

This function does not return any value.


Following is the example code showing its basic usage −


$count = 0;

$count = $count + 1;

if( $count > 4 ) {
   print "Exiting program\n";
} else {
   print "Count = $count, Jumping to START:\n";
   goto START;

When above code is executed, it produces the following result −

Count = 1, Jumping to START:
Count = 2, Jumping to START:
Count = 3, Jumping to START:
Count = 4, Jumping to START:
Exiting program