File Coverage

blib/lib/Time/Duration/Object/Infinite.pm
Criterion Covered Total %
statement 35 42 83.3
branch 6 8 75.0
condition n/a
subroutine 16 22 72.7
pod 12 14 85.7
total 69 86 80.2


line stmt bran cond sub pod time code
1 1     1   67922 use strict;
  1         9  
  1         37  
2 1     1   4 use warnings;
  1         2  
  1         346  
3             package Time::Duration::Object::Infinite 0.302;
4             # ABSTRACT: Time::Duration::Object, but infinite
5              
6             sub isa {
7 2 100   2 0 495 return 1 if $_[1] eq 'Time::Duration::Object';
8 1         5 return $_[0]->UNIVERSAL::isa($_[1]);
9             }
10              
11             #pod =head1 SYNOPSIS
12             #pod
13             #pod use Time::Duration::Object::Infinite;
14             #pod
15             #pod my $duration = Time::Duration::Object::Infinite->new_future;
16             #pod
17             #pod # It will happen forever from now.
18             #pod print "It will happen ", $duration->from_now;
19             #pod
20             #pod =head1 DESCRIPTION
21             #pod
22             #pod This is a class for Time::Duration::Object-like objects representing infinite
23             #pod durations.
24             #pod
25             #pod =method new
26             #pod
27             #pod =method new_positive
28             #pod
29             #pod These methods return a new Time::Duration::Object::Infinite for a positive
30             #pod duration.
31             #pod
32             #pod =cut
33              
34             sub new_positive {
35 1     1 1 2 my ($class) = @_;
36 1         2 my $duration = 1;
37 1         4 bless \$duration => $class;
38             }
39              
40 1     1 1 73 sub new { shift->new_positive }
41              
42             #pod =method new_negative
43             #pod
44             #pod This returns a new Time::Duration::Object::Infinite for a negative duration.
45             #pod
46             #pod =cut
47              
48             sub new_negative {
49 1     1 1 252 my ($class) = @_;
50 1         2 my $duration = -1;
51 1         3 bless \$duration => $class;
52             }
53              
54 11     11   14 sub _is_pos { ${$_[0]} == -1 }
  11         26  
55              
56             #pod =method new_seconds
57             #pod
58             #pod This method returns either C<+inf> or C<-inf> using Math::BigInt. (I don't
59             #pod recommend calling it.)
60             #pod
61             #pod =cut
62              
63             sub seconds {
64 0     0 0 0 require Math::BigInt;
65 0 0       0 return Math::BigInt->binf(shift->_is_pos ? '-' : ());
66             }
67              
68             #pod =method duration
69             #pod
70             #pod =method duration_exact
71             #pod
72             #pod These methods both return "forever."
73             #pod
74             #pod =cut
75              
76 0     0 1 0 sub duration { 'forever' }
77              
78             # This is to make it easy to implement the matched pair methods.
79             sub _flop {
80 11     11   21 my ($self, $flop, $pair) = @_;
81 11 100       27 my $is_pos = $flop ? ! $self->_is_pos : $self->_is_pos;
82 11 100       22 my $index = $is_pos ? 0 : 1;
83 11         19 my $str = $pair->[$index];
84 11         44 bless \$str => 'Time::Duration::_Result::_Infinite';
85             }
86              
87             my $ago_from_now;
88             my $earlier_later;
89             BEGIN {
90 1     1   5 $ago_from_now = [ 'forever ago', 'forever from now' ];
91 1         243 $earlier_later = [ 'infinitely earlier', 'infinitely later' ];
92             }
93              
94             #pod =method ago
95             #pod
96             #pod =method ago_exact
97             #pod
98             #pod These methods return "forever ago" for positive durations and "forever from
99             #pod now" for negative durations.
100             #pod
101             #pod =cut
102              
103 6     6 1 17 sub ago { $_[0]->_flop(1, $ago_from_now); }
104 1     1 1 235 sub ago_exact { $_[0]->_flop(1, $ago_from_now); }
105              
106             #pod =method from_now
107             #pod
108             #pod =method from_now_exact
109             #pod
110             #pod These methods do the opposite of the C methods.
111             #pod
112             #pod =cut
113              
114 0     0 1 0 sub from_now { $_[0]->_flop(0, $ago_from_now); }
115 0     0 1 0 sub from_now_exact { $_[0]->_flop(0, $ago_from_now); }
116              
117             #pod =method later
118             #pod
119             #pod =method later_exact
120             #pod
121             #pod These methods return "infinitely later" for positive durations and "infinitely
122             #pod earlier" for negative durations.
123             #pod
124             #pod =cut
125              
126 2     2 1 14 sub later { $_[0]->_flop(0, $earlier_later); }
127 0     0 1 0 sub later_exact { $_[0]->_flop(0, $earlier_later); }
128              
129             #pod =method earlier
130             #pod
131             #pod =method earlier_exact
132             #pod
133             #pod These methods do the opposite of the C methods.
134             #pod
135             #pod =cut
136              
137 2     2 1 5 sub earlier { $_[0]->_flop(1, $earlier_later); }
138 0     0 1 0 sub earlier_exact { $_[0]->_flop(1, $earlier_later); }
139              
140             package Time::Duration::_Result::_Infinite 0.302;
141              
142             #pod =method concise
143             #pod
144             #pod This method can be called on the result of the above methods, trimming down the
145             #pod ouput. For example:
146             #pod
147             #pod my $duration = Time::Duration::Object::Infinite->new_positive;
148             #pod print $duration->ago; # forever ago
149             #pod print $duration->ago->concise # forever ago
150             #pod
151             #pod Doesn't look any shorter, does it? No, it won't be. These methods are here
152             #pod for compatibility with Time::Duration::Object's returns.
153             #pod
154             #pod =cut
155              
156             sub concise {
157 1     1   1 ${ $_[0] }
  1         4  
158             }
159              
160 9     9   1378 sub as_string { ${ $_[0] } }
  9         72  
161              
162             use overload
163 1         4 '""' => 'as_string',
164 1     1   1021 fallback => 1;
  1         842  
165              
166             1;
167              
168             __END__