File Coverage

blib/lib/Time/AutoRes.pm
Criterion Covered Total %
statement 20 26 76.9
branch 8 10 80.0
condition n/a
subroutine 5 8 62.5
pod 5 5 100.0
total 38 49 77.5


line stmt bran cond sub pod time code
1             package Time::AutoRes;
2              
3 3     3   99915 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $use_hires);
  3         9  
  3         652  
4              
5             $VERSION = 0.02;
6              
7             @ISA = qw(Exporter);
8             require Exporter;
9              
10             BEGIN {
11 3     3   198 eval 'require Time::HiRes';
12 3         4694 $use_hires = not $@;
13 3 100       13 if ($use_hires) {
14 2         5 @EXPORT = @Time::HiRes::EXPORT;
15 2         276 @EXPORT_OK = @Time::HiRes::EXPORT_OK;
16             } else {
17 1         7 @EXPORT = qw();
18 1         123 @EXPORT_OK = qw(sleep alarm usleep ualarm time);
19             }
20             }
21              
22             sub import {
23 3 100   3   25 if ($use_hires) {
24 2         820 Time::HiRes->export_to_level(0, @Time::HiRes::EXPORT_OK);
25 2         4017 Time::HiRes->export_to_level(1, @_);
26             } else {
27 1 0   0 1 148 eval <<'EOS';
  0 100   20 1 0  
  0 100   2 1 0  
  0     0 1 0  
  0     0 1 0  
  20         19871  
  20         68  
  20         135  
  20         7006881  
  2         538  
  0            
  0            
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         1869 __PACKAGE__->export_to_level(1, @_);
55             }
56             }
57              
58              
59             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 over repeated calls.
81              
82             For example, if you call C, B
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
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, 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.
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 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 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