1. Perl
  2. Syntax
  3. here

goto statement - jump to any label

You can use the goto statement to jump to any label.

goto label

It is good not to use goto

As explained in Perl's modern writing style, goto is a good programming style not to use.

Use exception handling if you want to handle errors, and last statement if you want to break out of the loop. Use. Perl has alternatives, so it's unlikely that you'll use goto.

The only use of goto is to recurse a function

Besides jumping to the label, goto can execute functions without increasing the call stack. Perl doesn't optimize tail recursion and warns you of deep stacks, which you can work around with the "goto & function name" syntax.

goto & function name

This is an example using goto with tail recursion.

use strict;
use warnings;

# Tail-recursive function
my $fact = fact (3, 2);

print "$fact\n";

sub fact {
  my ($i, $n) = @_;
  if ($n == 0) {
    return $i;
  }
  else {
    @_ = ($i * $n, $n-1);
    goto & fact;
  }
}

The only use of goto is tail recursion, but you can write the same thing with tail recursion using for statement. increase. Therefore, it is recommended to write using for statement instead of tail recursion so that goto is not used in the source code at all.

Related Informatrion