| 1 | package Time::AutoRes; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 3 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $use_hires); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | 1 | $VERSION = 0.02; | |||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 1 | @ISA = qw(Exporter); | |||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 3 | ▶ | BEGIN { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 11 | 3 | eval 'require Time::HiRes'; | |||||||||||||||||
| 12 | 3 | $use_hires = not $@; | |||||||||||||||||
| 13 | 3 | ▶ | if ($use_hires) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 14 | 2 | @EXPORT = @Time::HiRes::EXPORT; | |||||||||||||||||
| 15 | 2 | @EXPORT_OK = @Time::HiRes::EXPORT_OK; | |||||||||||||||||
| 16 | } else { | ||||||||||||||||||
| 17 | 1 | @EXPORT = qw(); | |||||||||||||||||
| 18 | 1 | @EXPORT_OK = qw(sleep alarm usleep ualarm time); | |||||||||||||||||
| 19 | } | ||||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 3 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 23 | 3 | ▶ | if ($use_hires) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 24 | 2 | Time::HiRes->export_to_level(0, @Time::HiRes::EXPORT_OK); | |||||||||||||||||
| 25 | 2 | Time::HiRes->export_to_level(1, @_); | |||||||||||||||||
| 26 | } else { | ||||||||||||||||||
| 27 | 0 | ▶ | eval <<'EOS'; | ||||||||||||||||
|
Subroutine
alarm: not called CC=2 SCAR=7.1
Subroutine
sleep: called CC=3 SCAR=11.3
Subroutine
time: called CC=1 SCAR=0.1
Subroutine
ualarm: not called CC=1 SCAR=0.1
Subroutine
usleep: not called CC=1 SCAR=0.1
Pod
documented
Pod
documented
Pod
documented
Pod
documented
Pod
documented
Branch: 3 branches
| |||||||||||||||||||
| 28 | sub Time::AutoRes::sleep { | ||||||||||||||||||
| 29 | my ($delta) = @_; | ||||||||||||||||||
| 30 | my $int_delta = int $delta; | ||||||||||||||||||
| 31 | $int_delta++ if rand() < $delta - $int_delta; | ||||||||||||||||||
| 32 | CORE::sleep $int_delta if $int_delta; | ||||||||||||||||||
| 33 | } | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | sub Time::AutoRes::alarm { | ||||||||||||||||||
| 36 | my ($delta) = @_; | ||||||||||||||||||
| 37 | my $int_delta = int $delta; | ||||||||||||||||||
| 38 | $int_delta++ if rand() < $delta - $int_delta; | ||||||||||||||||||
| 39 | CORE::alarm $int_delta; | ||||||||||||||||||
| 40 | } | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | sub Time::AutoRes::usleep { | ||||||||||||||||||
| 43 | Time::AutoRes::sleep($_[0] / 1_000_000); | ||||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | sub Time::AutoRes::ualarm { | ||||||||||||||||||
| 47 | Time::AutoRes::alarm($_[0] / 1_000_000); | ||||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | sub Time::AutoRes::time { | ||||||||||||||||||
| 51 | CORE::time | ||||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | EOS | ||||||||||||||||||
| 54 | 1 | __PACKAGE__->export_to_level(1, @_); | |||||||||||||||||
| 55 | } | ||||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 1 | 1; | |||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | Time::AutoRes - use Time::HiRes or fall back to core code | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | use Time::AutoRes qw(sleep time alarm); | ||||||||||||||||||
| 69 | sleep(1.5); | ||||||||||||||||||
| 70 | $now = time; | ||||||||||||||||||
| 71 | alarm($now + 2.5); | ||||||||||||||||||
| 72 | |||||||||||||||||||
| 73 | =head1 DESCRIPTION | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | Time::AutoRes provides access to most of the functions that may be | ||||||||||||||||||
| 76 | imported from Time::HiRes (see list below). If Time::HiRes isn't | ||||||||||||||||||
| 77 | available, Time::AutoRes silently falls back to core Perl functions; | ||||||||||||||||||
| 78 | when this happens, it tries to emulate Time::HiRes by rounding | ||||||||||||||||||
| 79 | non-integers up or down in such a way as to approximate the | ||||||||||||||||||
| 80 | non-integer value B<on average> over repeated calls. | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | For example, if you call C<usleep(3.4)>, B<and if Time::HiRes is not | ||||||||||||||||||
| 83 | available>, there's a 40% chance of sleeping for 4 seconds, and a 60% | ||||||||||||||||||
| 84 | chance of sleeping for only 3 seconds. If you call C<usleep(3.4)> | ||||||||||||||||||
| 85 | repeatedly, the average delay will tend toward 3.4 seconds. | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =head1 EXPORTABLE FUNCTIONS | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | =over 4 | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | =item sleep($interval_in_seconds) | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | Sleep the given number of sleeps. If the interval is not an integer, | ||||||||||||||||||
| 95 | B<and Time::HiRes is not available>, Time::AutoRes will randomize the | ||||||||||||||||||
| 96 | delay as described above so that repeated calls with the same interval | ||||||||||||||||||
| 97 | can be expected to sleep the specified interval B<on average>. | ||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | =item usleep($interval_in_microseconds) | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | Seleep the given number of microseconds. There are one million | ||||||||||||||||||
| 102 | microseconds in a second. Randomness is used when Time::HiRes is not | ||||||||||||||||||
| 103 | available and a non-integer argument is given, in exactly the same way | ||||||||||||||||||
| 104 | as for Time::AutoRes::sleep. | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | =item alarm($interval_in_seconds) | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | Arranges to have a SIGALRM delivered to this process after the | ||||||||||||||||||
| 109 | specified number of seconds have elapsed. Randomness is used when | ||||||||||||||||||
| 110 | appropriate, as for Time::AutoRes::sleep. | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | =item ualarm($interval_in_microseconds) | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | Same as C<alarm> but in microseconds rather than seconds. Again, | ||||||||||||||||||
| 115 | randomness is used when appropriate. | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | =item time() | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | Returns the number of non-leap seconds since the epoch. | ||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | This simply calls Time::HiRes::time if it's available, or CORE::Time | ||||||||||||||||||
| 122 | if not. | ||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | B<Note:> This is the only exported function that never uses | ||||||||||||||||||
| 125 | randomness! | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | =back | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | =head1 BUGS | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | gettimeofday(), tv_interval(), getitimer() and setitimer() aren't | ||||||||||||||||||
| 132 | implemented. | ||||||||||||||||||
| 133 | |||||||||||||||||||
| 134 | =head1 AUTHOR | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | Paul Hofman (nkuitse AT cpan DOT org). | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | =head1 COPYRIGHT | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | Copyright 2004 Paul M. Hoffman. | ||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||
| 143 | it under the same terms as Perl itself. | ||||||||||||||||||
| 144 | |||||||||||||||||||