Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I break out of a loop in Perl?

I'm trying to use a break statement in a for loop, but since I'm also using strict subs in my Perl code, I'm getting an error saying:

Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154.

Is there a workaround for this (besides disabling strict subs)?

My code is formatted as follows:

for my $entry (@array){     if ($string eq "text"){          break;     } } 
like image 484
Zain Rizvi Avatar asked Nov 19 '08 20:11

Zain Rizvi


People also ask

How do you break out of a loop?

To break out of a for loop, you can use the endloop, continue, resume, or return statement.

How do you exit a nested for loop in Perl?

If you want to exit a nested loop, put a label in the outer loop and pass label to the last statement. If LABEL is specified with last statement, execution drops out of the loop encountering LABEL instead of currently enclosing loop.

Can we break for of loop?

The loop does end that iteration, and will continue from the next one. A for..in loop can't use break. It's not possible to end it in this way. Download my free JavaScript Handbook!


1 Answers

Oh, I found it. You use last instead of break

for my $entry (@array){     if ($string eq "text"){          last;     } } 
like image 87
Zain Rizvi Avatar answered Sep 28 '22 12:09

Zain Rizvi