Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to Docs and Examples for Loop::Watch by japhy
in thread Loop::Watch by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-04-18 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found