In Perl, is it better to use goto
or local function, and why with an example?
For example, I am using a code
sub data {
data;
}
data();
or
goto L:
L: if ( $i == 0 )
print "Hello!!!!";
Conventional wisdom is that other constructs are better than goto.
If you want to do something repeatedly, use a loop.
If you want to do something conditionally, use a block with if.
If you want to go somewhere and come back, use a function.
If you want to bail out early and "jump straight to the end", this can often be better written using an exception. (Perl handily manages
That said, do not use
goto
in Perl.goto
There is one place where a
goto
makes sense in Perl. Matt Trout is talking about that in his blog post No, not thatgoto
, the othergoto
.