Since I left these out of the code I posted, and since some people did not follow the
Help Name This Module thread, here is some documentation for the
Loop::Watch module.
Abstract
Loop::Watch is like
alarm() for variables -- it watches a given set of variables (scalars, arrays, and hashes are the only ones currently supported), and exits a block of code (which may or may not be a loop) as soon as the condition it is given becomes false. It's like a
while-loop that keeps tabs on the condition constantly.
Usage
Let's say you wanted to do a series of statements, but stop as soon as one of them caused
$temperature to go below 0, and if none caused that, to continue normally with the rest of the program.
use Loop::Watch;
my $temp = 20; # celsius, that is
# doing { ... } does *not* loop
ensure { $temp >= 0 } watching($temp), doing {
move_to_florida();
wait_until_winter();
move_to_new_jersey();
wait_until_spring();
move_to_montana();
wait_until_fall();
move_to_alaska();
wait_until_winter();
};
That code would probably stop somewhere around the move to New Jersey. That function could do any number of things to
$temperature, and this module keeps an eye on things.
Let's say, though, you wanted to circle the globe until your money ran out:
use Loop::Watch;
my $money = 1_000_000; # in USD
# looping { ... } *does* loop
ensure { $money > 0 } watching($money), looping {
circle_globe();
};
sub circle_globe {
move_to_next_location();
purchase_lodging();
eat();
# ...
}
This allows the functions called by
circle_globe()
to assume you have money left -- and as soon as you don't have any money left, you stop your trip.
You can watch multiple variables in the same block:
use Loop::Watch;
my ($age, @companies);
$age = 20;
ensure { $age < 35 and @companies < 10 }
watching($age, \@companies),
looping {
$age++ if new_year();
if (my $c = acquisition_available()) {
if (try_to_purchase($c)) {
push @companies, $c;
check_related($c);
}
}
};
That code will stop once you've reached 35 (good age to retire at) or you've acquired 10 companies. Maybe during the course of the
new_year(), you lose a company or something -- that's ok. You needn't do any checking there, all the checking of the age and company count is done by the module.
japhy --
Perl and Regex Hacker
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.