CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
break out of foreach loop and GOTO somewhere (help)

 



perlFun
Novice

Jun 26, 2013, 12:10 PM

Post #1 of 5 (525 views)
break out of foreach loop and GOTO somewhere (help) Can't Post

I have probably a really simple question. Say I am in a foreach:

foreach my $thing (@stuff){
#lots of code
if( some condition happens){
#want to break out from HERE
}
}

#more code going on after the foreach, I want to skip this if that condition is met

#finally i want to GOTO this point if I meet that condition in that foreach


If I use a goto, as follows:


foreach my $thing (@stuff){
#code
if(condition){
goto NEWPLACE;
}
}

#mode code

NEWPLACE:
#do stuff


Won't this take me to NEWPLACE (when that condition is met) but then continue with the remainder of the foreach once the stuff in NEWPLACE is done?

How do I completely break out of that foreach and then skip down to NEWPLACE?


Thanks so much!


FishMonger
Veteran / Moderator

Jun 26, 2013, 12:33 PM

Post #2 of 5 (521 views)
Re: [perlFun] break out of foreach loop and GOTO somewhere (help) [In reply to] Can't Post

You use the "last" function to break out of a loop.
http://perldoc.perl.org/functions/last.html

DON'T use goto!!!!! It's almost never the proper thing to do. In almost all cases, it's a sign of poor coding logic (i.e., spaghetti code: http://en.wikipedia.org/wiki/Spaghetti_code).


perlFun
Novice

Jun 26, 2013, 1:06 PM

Post #3 of 5 (516 views)
Re: [FishMonger] break out of foreach loop and GOTO somewhere (help) [In reply to] Can't Post

if I don't use goto then how can I jump to that particular place in the code? The situation is after my foreach theres a bunch of code I only want to execute if I never break out of that foreach


shawnhcorey
Enthusiast


Jun 26, 2013, 1:45 PM

Post #4 of 5 (509 views)
Re: [perlFun] break out of foreach loop and GOTO somewhere (help) [In reply to] Can't Post


In Reply To
if I don't use goto then how can I jump to that particular place in the code? The situation is after my foreach theres a bunch of code I only want to execute if I never break out of that foreach


The usual technique is to use a state variable. Inside the loop, set it to a value to indicate that the code should be skipped and put and `if` around the code so it will skip it if the state variable indicates it.

Code
my $skip = 0; 
foreach ...
...
if( some_condition ){
$skip = 1;
}
...
} # end foreach
if( ! $skip ){
# code conditionally skipped
}


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


Laurent_R
Veteran / Moderator

Jun 26, 2013, 3:12 PM

Post #5 of 5 (502 views)
Re: [perlFun] break out of foreach loop and GOTO somewhere (help) [In reply to] Can't Post


In Reply To
Won't this take me to NEWPLACE (when that condition is met) but then continue with the remainder of the foreach once the stuff in NEWPLACE is done?

No, it won't, if you use a goto go break out of a loop, then you're out of the loop for good.


In Reply To
How do I completely break out of that foreach and then skip down to NEWPLACE?

As noted above, a goto would do it, but don't use goto, don't do that. Perl has all the powerful instruments needed to do good and efficient structured programming, and a goto is just bad because it breaks structured programming.

You should know that the gods kill a puppy each time a programmer on the earth uses a goto. In brief, goto is evil (well, only 99.9% of the time, there are extremely rare cases where it could make sense, but only if you really know the usually much better alternatives and have a very good specific reason to do so) ! I have been programming in Perl for more than 10 years and have never found the need to use a goto (and also never used a goto in C programming in the preceding 10 years).

There are several statements in Perl that allow you to break out of a loop or modify the behavior of a loop, such as last, next and continue, depending on what you need to do.

Now, I am not a religious fanatic against goto's. In another programming language that I am using very commonly, I am frequently using goto's. But this is just because that language does not have the equivalent of a next statement when looping on the records of a database, so I use a goto to go straight to the end of the loop when some special condityions are met, in other words I use a goto in that language only to emulate a next statement, a case where it is legitimate because it does not break strutured programming.

(To tell the truth, I am old enough to have used older languages such as older Fortran versions and some versions of Basic, and also or course various assembly languages, where goto was necessary. But this is no longer required, and is certainly not useful in Perl and other modern languages availabe today).

In brief, I warn you against goto: if you feel like needing it, it is probably because you program is poorly structured. Rework the structure, refactor it.

(Just a final point for the sake of completeness. There is in Perl one very special and unusual form of goto which has very very little to do with what most people associate with goto (I believe it should have been given a different name), really a form of black magic. That specific form of goto does make sense when needed, but that is so rare that it is better to just forget about it: if you were anywhere near to understand these extremely specific cases, you would not ask the question you just asked.)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives