lib/Time/StasisField.pm

filter: Statementstmt 100% 108 / 108 Branchbran 97.6% 41 / 42 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 27 / 27 Podpod 57.1% 8 / 14 totaltotal 96.4% 192 / 199 CC 25 SCAR 9.9
CC
25
Cov
99%
alarm
6.0
sleep
4.0
_trigger_alarm
3.0
CRAP 2.7
↑ Summary
1 package Time::StasisField;
2  
3 =head1 NAME
4  
5 Time::StasisField - control the flow of time
6  
7 =cut
8  
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use POSIX (qw{SIGALRM});
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Scalar::Util (qw{set_prototype});
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 VERSION
16  
17 Version 0.01
18  
19 =cut
20  
21 4 our $VERSION = '0.01';
22  
23 =head1 SYNOPSIS
24  
25 I<Time::StasisField> provides a simple interface for controlling the flow of
26 time. When the stasis field is disengaged, Perl's core time functions --
27 alarm, gmtime, localtime, sleep, and time -- behave normally, assuming that
28 time flows with the system clock. When the stasis field is engaged, time
29 is guaranteed to advance at a predictable rate on every call. For consistency,
30 all other time-related functions will use the modified time.
31  
32 Example usage:
33  
34 use Time::StasisField;
35  
36 my @foos;
37  
38 @foos = map { Foo->new(create_time => time) } (1 .. 20);
39  
40 # All times will likely all look the same
41 print $foos[-1]->create_time - $foos[0]->create_time;
42  
43 # The program will pause for 10 seconds
44 sleep(10);
45  
46 # Time will be 10 seconds later
47 print time;
48  
49 #Let's control time
50 Time::StasisField->engage;
51  
52 @foos = map { Foo->new(create_time => time) } (1 .. 20);
53  
54 # All times will be distinct
55 print $foos[-1]->create_time - $foos[0]->create_time;
56  
57 # Time will advance by 10 seconds
58 sleep(10);
59  
60 # Fetch the current time without advancing it
61 print Time::StasisField->now;
62  
63  
64 Time::StasisField->seconds_per_tick(60);
65  
66 # Time is now 1 minute later
67 print time;
68  
69 # Everything is back to normal
70 Time::StasisField->disengage;
71  
72 # Hooray for system time
73 print Time::StasisField->now;
74  
75 =cut
76  
77 ############################
78 # Private Class Variables
79 ############################
80  
81 4 my $alarm_time;
82 4 my $current_time = 0;
83 4 my $is_alarm_set = 0;
84 4 my $is_engaged = 0;
85 4 my $is_frozen = 0;
86 4 my $seconds_per_tick = 1;
87  
88 ############################
89 # Helper Functions
90 ############################
91  
92 125 ▶sub _validate_number {
Subroutine _validate_number: called CC=1 SCAR=0.0
93 125 my $class = shift;
94  
95 #Make sure the value is numeric
96 4 ▶ use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶ no warnings ("void");
Subroutine BEGIN: called CC=1 SCAR=0.0
98 125 int($_[0]);
99 }
100  
101 228 ▶sub _trigger_alarm {
Subroutine _trigger_alarm: called CC=3 SCAR=11.0
102 228 my $class = shift;
103  
104 return
105 228 ▶ if ! $is_alarm_set
Branch: 1 branch
BranchTrueFalseTotal
if not $is_alarm_set or $class->now < $alarm_time 221 7 228
Condition100%3/3
not $is_alarm_set or $class->now < $alarm_time
l!l&&r!l&&!r
208137
MC/DC100%2/2
not $is_alarm_set or $class->now < $alarm_time
not $is_alarm_set $class->now < $alarm_time
Truth table100%3/3
not(A) or B
A= not $is_alarm_setB= $class->now < $alarm_time
ABresult
1X1
011
000
106 || $class->now < $alarm_time;
107  
108 7 CORE::alarm(0);
109 7 $is_alarm_set = 0;
110 7 kill SIGALRM, $$;
111 }
112  
113 =head1 STASIS FIELD METHODS
114  
115 =cut
116  
117 =head2 engage
118  
119 Enable the stasis field, seizing control of the system time and setting now to
120 the time the field was enabled. If engage is called while the field is already
121 enabled, now is updated to the current system time.
122  
123 =cut
124  
125 17 ▶sub engage {
Subroutine engage: called CC=3 SCAR=11.0
Pod documented
126 17 my $class = shift;
127  
128 17 ▶ if ($class->is_engaged) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->is_engaged) { } 2 15 17
129 #Update now to real time
130 2 $current_time = CORE::time;
131 #Trigger the alarm that may have occurred during the transition
132 2 $class->_trigger_alarm;
133  
134 } else {
135 #Turn off the alarm so that we don't accidentally throw while switching state
136 15 my $old_alarm = $class->alarm(0);
137  
138 15 $is_engaged = 1;
139 15 $current_time = CORE::time;
140  
141 #Turn the alarm back on
142 15 ▶ $class->alarm($old_alarm || 0);
Condition100%2/2
$old_alarm || 0
l!l
213
MC/DC100%1/1
$old_alarm || 0
$old_alarm
Truth table100%2/2
A
A= $old_alarm
Aresult
11
00
143 }
144  
145 17 return;
146 }
147  
148 =head2 disenage
149  
150 Disable the stasis field, returning control to the system time.
151  
152 =cut
153  
154 20 ▶sub disengage {
Subroutine disengage: called CC=3 SCAR=11.0
Pod undocumented
155 20 my $class = shift;
156  
157 20 ▶ return unless $class->is_engaged;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_engaged 5 15 20
158  
159 15 $current_time = CORE::time;
160 15 $is_engaged = 0;
161  
162 #Start the system alarm from now
163 15 ▶ $class->alarm($alarm_time - $current_time) if $is_alarm_set;
Branch: 1 branch
BranchTrueFalseTotal
if $is_alarm_set 3 12 15
164 #Trigger the alarm that may have occurred during the transition
165 15 $class->_trigger_alarm;
166  
167 14 return;
168 }
169  
170 =head2 is_engaged
171  
172 Return whether or not the stasis field is enabled.
173  
174 =cut
175  
176 788 ▶sub is_engaged { $is_engaged }
Subroutine is_engaged: called CC=1 SCAR=0.0
Pod documented
177  
178 =head2 freeze
179  
180 Time should stop advancing now.
181  
182 =cut
183  
184 1 ▶sub freeze { $is_frozen = 1 }
Subroutine freeze: called CC=1 SCAR=0.0
Pod documented
185  
186 =head2 unfreeze
187  
188 Time should continue advancing now.
189  
190 =cut
191  
192 1 ▶sub unfreeze { $is_frozen = 0 }
Subroutine unfreeze: called CC=1 SCAR=0.0
Pod documented
193  
194 =head2 is_frozen
195  
196 Return whether or not time advances now.
197  
198 =cut
199  
200 327 ▶sub is_frozen { $is_frozen }
Subroutine is_frozen: called CC=1 SCAR=0.0
Pod documented
201  
202 =head1 TIME METHODS
203  
204 =cut
205  
206 =head2 now
207  
208 Accessor for the current time. The supplied time may be any valid number,
209 though now will always return an integer. Falls back to the system time when
210 the stasis field is disengaged.
211  
212 =cut
213  
214 327 ▶sub now {
Subroutine now: called CC=3 SCAR=11.0
Pod documented
215 327 my $class = shift;
216  
217 327 ▶ return CORE::time unless $class->is_engaged;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_engaged 14 313 327
218  
219 313 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 296 313
220 17 $class->_validate_number($_[0]);
221 16 $current_time = $_[0];
222 16 $class->_trigger_alarm;
223 }
224  
225 312 return int($current_time);
226 }
227  
228 =head2 seconds_per_tick
229  
230 Accessor for the number of seconds time changes with each tick. Supports
231 negative and subsecond deltas. Only works on time in an engaged stasis field.
232  
233 =cut
234  
235 204 ▶sub seconds_per_tick {
Subroutine seconds_per_tick: called CC=2 SCAR=6.9
Pod documented
236 204 my $class = shift;
237  
238 204 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 196 204
239 8 $class->_validate_number($_[0]);
240 8 $seconds_per_tick = $_[0];
241 }
242  
243 204 return $seconds_per_tick;
244 }
245  
246 =head2 tick
247  
248 Advance time by the value of seconds_per_tick, regardless of the freeze state.
249 Returns now.
250  
251 =cut
252  
253 327 ▶sub tick {
Subroutine tick: called CC=2 SCAR=6.9
Pod documented
254 327 my $class = shift;
255  
256 327 ▶ return CORE::time unless $class->is_engaged;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_engaged 132 195 327
257  
258 195 $current_time += $class->seconds_per_tick;
259 195 $class->_trigger_alarm;
260  
261 194 return $class->now;
262 }
263  
264 ############################
265 # Core Overrides
266 ############################
267  
268 4 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
269 4 for my $function (qw{
270 alarm
271 gmtime
272 localtime
273 sleep
274 time
275 }) {
276 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
277 20 *{"CORE::GLOBAL::$function"} = set_prototype(
278 400 ▶ sub { unshift @_, 'Time::StasisField'; goto &{"Time::StasisField::$function"} },
Subroutine __ANON__: called CC=1 SCAR=0.0
279 20 prototype("CORE::$function")
280 );
281 }
282 }
283  
284 76 ▶sub alarm {
Subroutine alarm: called CC=6 SCAR=17.9
Pod undocumented
285 76 my $class = shift;
286 76 ▶ my $offset = @_ ? $_[0] : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 76 0 76
287  
288 76 $class->_validate_number($offset);
289  
290 76 ▶ return CORE::alarm($offset) unless $class->is_engaged;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_engaged 41 35 76
291  
292 35 ▶ my $previous_alarm_time_remaining =
Branch: 2 branches
BranchTrueFalseTotal
!defined($alarm_time) ? : 4 31 35
$is_alarm_set ? : 6 25 31
293 ! defined $alarm_time ? $alarm_time :
294 $is_alarm_set ? $alarm_time - $class->now : 0;
295 35 ▶ $alarm_time = $offset > -1 ? $class->now + int($offset) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$offset > -1 ? : 34 1 35
296 35 $is_alarm_set = $offset >= 1;
297  
298 35 return $previous_alarm_time_remaining;
299 }
300  
301 5 ▶sub gmtime {
Subroutine gmtime: called CC=3 SCAR=11.0
Pod undocumented
302 5 my $class = shift;
303  
304 5 ▶ $class->_validate_number($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 2 5
305 4 ▶ use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
306 5 ▶ CORE::gmtime(@_ ? $_[0] : time);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 2 5
307 }
308  
309 5 ▶sub localtime {
Subroutine localtime: called CC=3 SCAR=11.0
Pod undocumented
310 5 my $class = shift;
311  
312 5 ▶ $class->_validate_number($_[0]) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 2 5
313 4 ▶ use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
314 5 ▶ CORE::localtime(@_ ? $_[0] : time);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 3 2 5
315 }
316  
317 20 ▶sub sleep {
Subroutine sleep: called CC=4 SCAR=13.9
Pod undocumented
318 20 my $class = shift;
319  
320 20 ▶ return CORE::sleep unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 18 20
321 18 $class->_validate_number($_[0]);
322 18 ▶ return CORE::sleep if $_[0] <= -1;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] <= -1 2 16 18
323 16 ▶ return $class->is_engaged ? do { $class->now($class->now + $_[0]); int($_[0]) } : CORE::sleep($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
$class->is_engaged ? : 5 11 16
324 }
325  
326 327 ▶sub time {
Subroutine time: called CC=2 SCAR=6.9
Pod undocumented
327 327 my $class = shift;
328  
329 327 ▶ return $class->is_frozen ? $class->now : $class->tick;
Branch: 1 branch
BranchTrueFalseTotal
$class->is_frozen ? : 9 318 327
330 }
331  
332 =head1 ACKNOWLEDGEMENTS
333  
334 This module was made possible by L<Shutterstock|http://www.shutterstock.com/>
335 (L<@ShutterTech|https://twitter.com/ShutterTech>). Additional open source
336 projects from Shutterstock can be found at
337 L<code.shutterstock.com|http://code.shutterstock.com/>.
338  
339 =head1 AUTHOR
340  
341 Aaron Cohen, C<< <aarondcohen at gmail.com> >>
342  
343 =head1 BUGS
344  
345 Please report any bugs or feature requests to C<bug-time-stasisfield at rt.cpan.org>, or through
346 the web interface at L<https://github.com/aarondcohen/perl-time-stasisfield/issues>. I will
347 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
348  
349 =head1 SUPPORT
350  
351 You can find documentation for this module with the perldoc command.
352  
353 perldoc Time::StasisField
354  
355 You can also look for information at:
356  
357 =over 4
358  
359 =item * Official GitHub Repo
360  
361 L<https://github.com/aarondcohen/perl-time-stasisfield>
362  
363 =item * GitHub's Issue Tracker (report bugs here)
364  
365 L<https://github.com/aarondcohen/perl-time-stasisfield/issues>
366  
367 =item * CPAN Ratings
368  
369 L<http://cpanratings.perl.org/d/Time-StasisField>
370  
371 =item * Official CPAN Page
372  
373 L<http://search.cpan.org/dist/Time-StasisField/>
374  
375 =back
376  
377 =head1 LICENSE AND COPYRIGHT
378  
379 Copyright 2013 Aaron Cohen.
380  
381 This program is free software; you can redistribute it and/or modify it
382 under the terms of either: the GNU General Public License as published
383 by the Free Software Foundation; or the Artistic License.
384  
385 See http://dev.perl.org/licenses/ for more information.
386  
387 =cut
388  
389 4 1; # End of Time::StasisField
↑ Summary