Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Monday, October 3, 2011

do while is not a loop

I've found that if you have a loop like this

$i = 0;
do {
print "$i ";
last if $i == 3;
++$i;
} while ($i != 10);

you will get the run-time error 'Can't "last" outside a loop block' when $i reaches 3.

Putting the 'last' in a proper if() {} block doesn't make any difference.

Apparently this occurs because do { } is just a grouping block, not a looping one, and the while here is the statement modifier while, not the control structure while. do { } is what you use if you want to put a statement modifier on a block of statements - { } alone just won't 'do'.

While that makes a sort of sense, a big red warning in Programming Perl would have helped. 'Do not use! Useful construct merely simulated!'

Additionally: what makes this really dangerous is that, if it occurs in a sub called from within a genuine while, the last will immediately jump out of the genuine loop (even if that loop's in a different scope) without any error or warning message. DO NOT USE.

Followers

Blog Archive