SPUG: Using last in non-loop blocks

Michael R. Wolf MichaelRWolf at att.net
Wed Apr 28 13:17:54 CDT 2004


Yitzchak Scott-Thoennes <sthoenna at efn.org> writes:

[...]

>> But that means something like this will work:
>> 
>>  if ($reasonably_true) {{
>>      #blah blah
>>      last unless $denominator > 0;
>>      #blah blah
>>  }}
>>  print "I managed to last out...\n";

It took me a while to get around to this thread, so I read it all at
once. Luckily I did. It took seeing double curlies a few times before
I realized it's non-standard enough (at least to my eyes) that I'd
hilight it some other way. I know that most of these other solutions
are obvious to most of us, but I'd like to hilight the goto solution.
Not that I like it. But that's the point. The 'goto' solution appeals
as little to me as the 'last unless' solution. IMHO, of course, but
this discussion seemed to be bordering the gap between 'technically
feasable' and 'stylistic preference'. It's a balance act.

# 1. Label the "extra" block, and name it in the 'last'.

if ($reasonably_true) {
    DIVISION: {
        #blah blah
        last DIVISION unless $denominator > 0;
        #blah blah
    }
}

# 2. Refactor the following code into a block.

if ($reasonably_true) {
    #blah blah
    unless $denominator > 0 {
        #blah blah
    }
}

# 3. Use an exception
if ($reasonably_true) {
    eval {
        #blah blah
        die unless $denominator > 0;
        #blah blah
    };
    handle_divide_by_negative if ($@);
}


# 4. Break down and do a GOTO. 
if ($reasonably_true) {
    #blah blah
    goto SAFE if $denominator > 0;
    #blah blah
    SAFE:
    # handle divide by negative
}



> It's worth noting that there is no way of doubling the braces for a
> do { } while or do { } until block that will work sanely both for last
> and next/redo.

Would you consider labels to be an a sane solution?


-- 
Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net





More information about the spug-list mailing list