Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Number of times I've used goto in Perl

by ikegami (Pope)
on May 06, 2018 at 04:13 UTC ( #1214108=note: print w/replies, xml ) Need Help??


in reply to Number of times I've used goto in Perl

I found goto useful in C.

char *p1 = alloc(); if (p1 == NULL) goto ERROR; char *p2 = alloc(); if (p2 == NULL) goto ERROR2; char *p3 = alloc(); if (p3 == NULL) goto ERROR3; ... return 1; ERROR3: free(p2); ERROR2: free(p1); ERROR: return 0

But with Perl's reference counting and destructors, this model isn't necessary.

Replies are listed 'Best First'.
Re^2: Number of times I've used goto in Perl
by Anonymous Monk on May 06, 2018 at 09:55 UTC

    In C, any time you use the switch statement, you're really using (forward) goto in disguise.

      for, while, if, and more can also be implemented using a jump. That doesn't matter. The problem with goto isn't that it's a jump instruction; the problem is the resulting lack of code structure. For code to readable, it you should composed of "blocks". Prematures exits (next, last, return, die, and goto as used in my C example) are an exception as they can actually improve readability. This is why they don't necessarily have the same problem as goto.

        Just one example what you can do with switch: Duff's device.

        Switch cases have the fallthrough property, so those blocks can have multiple entry edges. Besides that, switch and goto have the same problem that they can enter a scope at the middle. (For instance, C prohibits bypassing the declaration of variably modified type, e.g. int foo[n]; where n is a variable too — this is more or less the same as alloca().)

        The main benefit of goto is that it can be used to set up a simple FSM without using a state variable. Which is also the main problem with goto, in case the simple turns out to be "simple".

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214108]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2019-03-19 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (89 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 18:53 UTC) Welcome new users!