Funnily, simple braces { } qualify as a 'loop' which is processed once. It can be aborted using last.
Therefore you could use:
which works without an error message. But it isn't very good style. Try to use a normal while loop instead.$a = 3; { last unless $a == 4; redo; # if ( ... ); }
Supplement:
The perldoc entry for do also gives you the answer and a link to alternative strategies:
do BLOCK Not really a function. Returns the value of the last c +ommand in the sequence of commands indicated by BLOCK. When modified + by the "while" or "until" loop modifier, executes the BLOCK once before testing the loop condition. (On other state +ments the loop modifiers test the conditional first.) "do BLOCK" does not count as a loop, so the loop contro +l statements "next", "last", or "redo" cannot be used to leave or res +tart the block. See [doc://perlsyn] for alternative strategies.
In reply to Re: "last" not really outside loop block
by mscharrer
in thread "last" not really outside loop block
by spx2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |