views:

22082

answers:

2

Hi,

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.

Does anyone know of a workaround for this (besides disabling strict subs)?

My code is formatted as follows:

for my $entry (@array){
    if ($string eq "text"){
         break;
    }
}

Thanks

+19  A: 

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

Zain
Perl is not C/C++.
Brad Gilbert
@Brad - No, it's not - and thank Cthulhu for that!
Sherm Pendley
Though this is merely a semantic difference and doesn't amount to much else other than textual consistency with the "next" command.
Razor Storm
+12  A: 

Additional Data ( In case you have more questions ):

FOO: { 
       for my $i ( @listone ){ 
          for my $j ( @listtwo ){ 
                 if ( cond( $i,$j ) ){ 

                    last FOO;  # --->
                                   # | 
                 }                 # |
          }                        # |
       }                           # |
 } # <-------------------------------
Kent Fredric
You don't actually need the braces after FOO:
cjm
You do if you have other code just after the for my $i loop that you also want to skip. The outer {} is a bare block, which is itself a funny kind of loop that can be last/redo/next'd.
ysth