jeepj,
I believe I have only used it once. Here is some code that does not use a
continue block:
my $counter = 0;
while ($counter < $limit) {
my $item = shift @work_queue;
my $thingy = some_function($item);
if ($thingy ne 'PARTY') {
++$counter;
next;
}
# more code if $thingy ne 'PARTY'
++$counter; # thanks ikegami
}
Here is the same code with the continue block
my $counter = 0;
while ($counter < $limit) {
my $item = shift @work_queue;
my $thingy = some_function($item);
next if $thingy ne 'PARTY';
# more code if $thingy ne 'PARTY'
}
continue { ++$counter; }
Here is my general rule of thumb for when to use continue
- You have 1 or more statements that need to be executed every single loop
- You have more than one possible short-circuit condition in the loop
It is a nice way of code re-use. Instead of doing
if ($condition1) {
# statement 1
# statement 2
# statement 3
next;
}
# more code
if ($condition2) {
# statement 1
# statement 2
# statement 3
next;
}
You can just do
next if $condition1;
# more code
next if $condition2;
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.