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; } }
To break out of a for loop, you can use the endloop, continue, resume, or return statement.
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.
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!
Oh, I found it. You use last instead of break
for my $entry (@array){ if ($string eq "text"){ last; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With