The naked block is a good starting point, but I wouldn't use
redo. This is the way to evaluate the conditions as few times as possible, and also in the exact same order as your own code:
{
last unless (my $once = $condition1) and $condition2;
while(1) {
# ...
# ...
last if $once or not $condition3;
}
}
Careful about the order of checks. Note that you might skip preserving the state in $once so long as $condition1 wouldn't change during an iteration of the loop and checking it doesn't have any side effects. However, you will have to duplicate the code at the bottom of the loop, so I believe the above form is still clearer in intent. It also has the benefit that it evaluates the expression only once.
Update: (Duh me.) So long as your statement 1..30 don't need next/last, you can and probably should use a do while:
{
last unless (my $once = $condition1) and $condition2;
do {
# ...
# ...
} while not $once and $condition3;
}
Makeshifts last the longest.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.