SPUG: Using last in non-loop blocks

Andrew Sweger andrew at sweger.net
Thu Apr 22 18:37:53 CDT 2004

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).

Andrew B. Sweger -- The great thing about multitasking is that several
                                things can go wrong at once.

More information about the spug-list mailing list