Reputation: 21
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!!!!";
Upvotes: 1
Views: 819
Reputation: 2847
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
Upvotes: 0
Reputation: 54373
Programmers don't die. They just
GOSUB
withoutRETURN
.
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 that goto
, the other goto
.
Upvotes: 6