in reply to How to no goto or gosub
Long story short: old languages didn't have good Loop Control or early return, so they needed goto. Perl has them, so you are probably doing something bad if you need to use it. So consider how you can refactor to avoid it. This is coming from a Fortran hacker, so this warning should not be taken lightly.
To do what you have asked, the code is:
which appears to be your case 1, so I think you have transcribed your problem badly. I do not know what code you are trying to emulate, but I would probably write your code as:#!/usr/bin/perl use strict; use warnings; use 5.012; my $gas_cost = 1.5; if ($gas_cost < 1) {goto ENDING_BRACE }; say "Intermediate"; ENDING_BRACE: say "After";
or{ last if $gas_cost < 1; stuff here } continue here
or evenfunc(); continue here; sub func { return if $gas_cost < 1; stuff here }
Or a myriad of variations. The difference between your proposal and any of the above is only scoping, and that can be addressed as well if you share your real code with us.if ($gas_cost >= 1) { stuff here } continue here
You may also find How does 'goto LABEL' search for its label? informative.
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
---|