SPUG: Using last in non-loop blocks

Yitzchak Scott-Thoennes sthoenna at efn.org
Thu Apr 22 18:59:43 CDT 2004


On Thu, Apr 22, 2004 at 04:37:53PM -0700, Andrew Sweger <andrew at sweger.net> wrote:
> According to perldoc -f last (perl 5.8.3):
> 
>     Note that a block by itself is semantically identical to a loop that
>     executes once. Thus "last" can be used to effect an early exit out of
>     such a block.
> 
> This led me to think that I could safely use 'last' in any block
> structure, including an if block. E.g.,
> 
>     if ($reasonably_true) {
>         #blah blah
>         last unless $denominator > 0;
>         #blah blah
>     }
> 
> I got slapped in the terminal with,
> 
>     Can't "last" outside a loop block at /my/dumb/script.pl line 162.
> 
> Just thought that was interesting. I thought it should work (according to
> the docs anyway).

That's not a block by itself, that's part of the if statement.
Try:

if (...) {{
   ...
}}



More information about the spug-list mailing list