Goto
There are two forms of goto in Perl:
goto labeland
goto &subroutineThe first form is generally deprecated, and is only used in rare situations. For example, when attempting to preserve error status in $?
, some modules will use goto like this:
The second form is called a tail call, and is used to enhance the performance of certain kinds of constructs where Perl's default stack management would perform non-optimally. For example:
sub factorial { my $n = shift; my $total = shift(@_) || 1; if ($n > 1) { @_ = ($n-1,$total*$n); goto &factorial; } else { return $total; } }This form is also used to create aliases for subroutines with minimal overhead. This can help reduce "Out of Memory" errors (or high memory usage in general) found often in repeating the same subroutine.
Read more about this topic: Perl Control Structures