SPUG: breaking out with "next"

El JoPe Magnifico jope-spug at n2h2.com
Fri Aug 6 15:56:43 CDT 1999


I'm guessing you eventually expect to have a while loop around 
your if block, in which case the error will go away.  If not 
(or if you're leaving out the while loop for the moment to deal
with just a single iteration) the alternative is to wrap a 
labelled bare block around the if block, e.g...

BLOCKNAME:
{
	if (1)
	{
		unless (0)
		{
			warn("inside the unless block\n");
			next BLOCKNAME if 1;
			warn("oops, the next didn't take!\n");
		}
	}
}
warn("and that's all there is.\n");

This will allow you to use "next" and "last" without a loop.  
Note that "next" acts like last in such a loopless block.
If you want to later change the bare block into a loop block, 
just stick the loop condition (while, for, foreach) between 
the block label and the  opening curly bracket, e.g...

BLOCKNAME:
while ($keep_on_truckin)
{
	if (1)
	...yadda yadda yadda...

See pages 101-106 of the camel book (2nd ed, anyway) for a
discussion of labels.
-jp

On Fri, 6 Aug 1999, Kevin Fink wrote:
> "next" only works within a block (usually a loop). 
> "unless" isn't a loop construct - try "while" or "until" instead.
> 
> On Fri, 6 Aug 1999, Ryan Forsythe wrote:
>> problem with the 'next if <statement>' command.  i've pored over the
>> camel book and all the FAQs i can find...i still can't figure out what
>> i'm doing wrong. [...] here's the chunk of my code that wigs out:
>> 
>> if ($queryLine =~ m/^$/) {
>> 	unless ( !($dbaseLine = <ACCOUNTS>)
>>		&& ($dbaseLine =~ m/^$/)
>>		&& ($count >= $maxRecords)
>>		) {
>> 		$count++;
>> 		
>> 		# i've used the pound sign as comment in the dbase, so if
>>		# it's the first char in the record i skip the record line.
>> 		next if $dbaseLine =~ m/^#/;
>> 		
>> 		....more code....
>> 	}	
>> }
>> 
>> and i get this error from the compiler:
>> Can't "next" outside a block at findacct.pl line 34, <ACCOUNTS> chunk 1.
>> 
>> (line 34 is the line beginning with 'next if...')




 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list