SPUG: Using last in non-loop blocks

Jack Foy jack at gehennom.net
Thu Apr 22 19:02:50 CDT 2004


Andrew Sweger wrote:
> This led me to think that I could safely use 'last' in any block
> structure, including an if block. E.g.,

'if' (and 'unless') blocks are the exception to that rule -- unlike all
other blocks, they are not one-time loops.  (See page 123 of PP3.)
That's because you often want to do something like this, and have it do
the notionally right thing:

for (@lines) {
	if ($disaster) {
		clean_up();
		last;
	}
	normal_processing();
}

If you really want to use last inside an if, use a double block:

if ($condition1) {{
	stuff();
	last if $condition2;
	more_stuff();
}}

-- 
Jack Foy <jack at gehennom.net>



More information about the spug-list mailing list