in reply to GOTO or not GOTO

OK, awreddy! The wisdom elsewhere in this thread prompts me to expand what appears to be my minority response to the OP:

First, for clarity, allow me to amend "For the purpose ... ahead and GOTO." to read: "For the NARROW purpose you've outlined, perlitical correctness is not the issue. "Perlitically correct" be damned! If it works, go ahead and GOTO (sic) (but don't get in the habit)!

Yes, even that runs counter to convention wisdom (and I readily acknowledge that the conventional wisdom is wise. (In fact, the bit of nonsense below represents the first time I've even considered using goto in years.)

But I read the OP, the Seeker has a suite of tests of "human reaction," each of whose members stands alone. IOW, I conceive each test to be fundamentally self-contained and (perhaps) presented in a fixed sequence; that his offer to re-run the tests would occur only upon completion of the suite.

In such a case, concerns about spaghetti code, readability, resetting of variables, and all the other usual problems with goto are (or can be, in a(n otherwise, if you must) well-written program can be moot.

The script below, while not reflecting precisely the constraints above, may invite comments... or even second thoughts???

use strict; use warnings; my @array = qw/a b c/; START: for my $item(@array) { my $sub_call = "&" . $item; eval ($sub_call); } sub quit { print "Would you like to do that again? 'y' or 'n'\n"; if (<STDIN> =~ /y/) { goto START; }else{ print "OK, done.\n"; exit; } } sub a { print "Type 'blue' to continue:\n"; if (<STDIN> =~ /blue/i) { return 1; } else { quit(); } } sub b { print "Cross your fingers and type 'foobar' to continue:\n"; if (<STDIN> =~ /foobar/i) { return 1; } else { quit(); } } sub c { print "To quit (prematurely), type 'no'\n"; if (<STDIN> =~ /no/) { exit; } else { goto START; } }

For "nonsense" constructed specifically to use a goto, this seems to me (YMMV) clear and utterly without 'strange actions at a distance.'

FTR, in NO way does it rebut the correctness of deprecating goto; rather, it's intended as a reminder that there are -- occasionally -- times to violate conventional wisdom and ways to sidestep the underlying evil that norm seeks to avoid.

Replies are listed 'Best First'.
Re^2: GOTO or not GOTO
by GrandFather (Saint) on Jan 27, 2009 at 10:35 UTC

    Lets restructure that code just a little:

    use strict; use warnings; my @array = (\&blue, \&foobar, \&no); do { eval {$_->() for @array}; } while $@; sub quit { print "Would you like to do that again? 'y' or 'n'\n"; die if <STDIN> =~ /y/; print "OK, done.\n"; exit; } sub blue { print "Type 'blue' to continue:\n"; return 1 if <STDIN> =~ /blue/i; quit(); } sub foobar { print "Cross your fingers and type 'foobar' to continue:\n"; return 1 if <STDIN> =~ /foobar/i; quit(); } sub no { print "To quit (prematurely), type 'no'\n"; exit if <STDIN> =~ /no/i; die; }

    Shorter, cleaner, no gotos and, IMO, clearer. Now, why did we need those gotos again?


    Perl's payment curve coincides with its learning curve.