Search code examples
perlfunctiongoto

Goto vs. Local function in Perl


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!!!!";

Solution

  • Programmers don't die. They just GOSUB without RETURN.

    That said, do not use goto in Perl.

    • If you want a statement to be executed once, just write that statement and don't use any flow control
    • If you want your program to execute the stuff several times, put it in a loop
    • If you want them to be executed from different places, put them in a sub
    • If you want the sub to be available in different programs, put them in a module
    • Don't use 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.