Archive for the 'Perl' Category

I just learned something…

It’s very frustrating to have two very similar blocks of code where one works and the other doesn’t. The gist of my problem was this - two blocks almost exactly like the block below.


do
{
  if ($some_external_condition) {
    $result = subroutine();
    last if !defined $result;
  }
} until $some_other_external_condition

One of them ran just fine. The other one was throwing a

Can't "last" outside a loop block

runtime error.

Huh? How come one works and one doesn’t? I dug out the trusty Camel Book and looked up a do loop. Huh? What do you mean, it’s not a loop? How come my other block of code works just fine?

Takes a closer look - doh! It’s a runtime error! My first chunk of code never reaches the last call because of the external condition. My second one does reach it.

Oh, well. I hope that this helps someone else with a similar problem…