Schedule::ByClock - Give back the control to the caller at given times.
use Schedule::ByClock;
# Constructor $th = Schedule::ByClock->new([time [,time [,...]]]);
# Methods @times = $th->set_control_list([time [,time [,...]]]); @times = $th->get_control_list();
$second = $th->get_control_on_second([second [,second [,...]]]); $minute = $th->get_control_on_minute([minute [,minute [,...]]]);
$version = $th->get_version();
Schedule::ByClock uses the Time::localtime module.
This module implements an 'intelligent' (?) layer over sleep(). Call the module when you want to sleep to a given second in the minute, or to a given minute in the hour, without having to calculate how long to wait.
Use with multiple 'time' values to sleep until the chronologically first 'time' in the list.
Note that all times used in Schedule::ByClock are calculated from the local time in the computer where Schedule::ByClock is executed.
Assume that you want to do something repeatedly every minute, when the seconds show for instance 34.
Assume that you want to do something (maybe just once) the next time the seconds in the computer shows 23. Assume that 'now' is 18. You would need to use sleep and to calculate how many seconds there are from 'now' till 23. Easy, 23 - 18 = 5. sleep(5); Then, assume that 'now' is 28. I.e. 23 - 28 = 55. (Huh?)
Assume that you want to do something repeatedly, when the seconds show either 12 or 45 or 55, whichever comes first compared to 'now'. Assume that 'now' is 56. You would have to find out if it's 12, 45 or 55 that comes 'after' 56. Then you would have to calculate 12 - 56 = 16. (Right?)
Assume that you don't want to wait for seconds, but for minutes instead. Sleep until the minutes in the hour are either 23, 55 or 59.
You should have got the picture by now. (Or I have failed. :-)
$th = Schedule::ByClock->new(12,8,55); # Constructor with three 'time' values.
$rc = $th->get_control_on_second(); # This will return at 09:09:55.
At 09:09:24, you call:
$rc = $th->get_control_on_second(); # This will return at 09:10:23.
At 09:09:24, you call:
$rc = $th->get_control_on_second(); # This will return immediately (with return value undef).
At 09:09:25, you call:
$rc = $th->get_control_on_second(12); # This will return at 09:10:12.
At 09:09:25, you call:
$rc = $th->get_control_on_minute(12); # This will return at 09:12:00. <= Note the minutes.
$rc = $th->get_control_on_second(); # This will return at 09:10:55, one minute later.
The 'time' values can be in arbitrary order.
Any 'time' that is not within the range 0 - 59 will be ignored and a warning (carp) will be written to the terminal.
get_control_on_second()
and/or get_control_on_minute(),
overriding the old list (if any).
The 'time' values can be in arbitrary order.
Any 'time' that is not within the range 0 - 59 will be ignored and a warning (carp) will be written to the terminal. If no 'times' are given (no parameters), then the internally stored list of 'times' will be cleared. Returns the newly stored list of 'times'.
Whenever a call to $th->get_control_on_second()
or $th->get_control_on_minute()
is done with a parameter list, all values in the list will have to be validated.
Gustav Schaffter <schaffter_cpan@hotmail.com>
Copyright (c) 1999, 2000, 2001 and 2002, Gustav Schaffter. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.