D/oh/Year.pm

filter: Statementstmt 86.5% 77 / 89 Branchbran 69.2% 18 / 26 Conditioncond 38.4% 5 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 79.1% 19 / 24 Podpod n/a totaltotal 75.6% 121 / 160 CC 19 SCAR 12.7
CC
19
Cov
78%
new
12.0
__ANON__
5.0
__ANON__
5.0
CRAP 3.6
↑ Summary y2k.pm »
1 package D'oh::Year;
2  
3 1 require 5.005; # Need a solid overload
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =pod
8  
9 =head1 NAME
10  
11 D'oh::Year - Catch stupid mistakes when mucking with years, like Y2K bugs
12  
13  
14 =head1 SYNOPSIS
15  
16 use D'oh::Year;
17
18 ($year) = (localtime)[5];
19 print "We're going to party like its 19$year"; # No you're not.
20
21 print "Welcome to the year 20$year!"; # Sorry, Buck.
22
23  
24 =head1 DESCRIPTION
25  
26 NO, PERL DOES NOT HAVE A Y2K BUG! but alot of people seem determined
27 to add it. Perl, and most other languges through various historical
28 reasons, like to return years in the form of the number of years since
29 1900. This has led to the false assumption that its actually
30 returning the last two digits of the current year (1999 => 99) and the
31 mistaken assumption that you can set the current year as "19$year".
32  
33 This is a Y2K bug, the honor is not just given to COBOL progrmamers.
34  
35 Bugs of this nature can easily be detected (most of the time) by an
36 automated process. This is it.
37  
38 When D'oh::Year is used, it provides special versions of localtime() and
39 gmtime() which return a rigged value for the year. When used properly
40 (usually 1900 + $year) you'll notice no difference. But when used for
41 B<EVIL> it will die with a message about misuse of the year.
42  
43 The following things are naughty (where $year is from gmtime() or
44 localtime()):
45  
46 "19$year", 19.$year
47 "20$year", 20.$year
48 "200$year", 200.$year
49 $year -= 100, $year = $year - 100;
50  
51 B<THE FOLLOWING ARE THE CORRECT WAYS TO MANIPULATE THE DATE>
52 Take note, please.
53  
54 $year += 1900; # Get the complete year.
55 $year %= 100; # Get the last two digits of the year.
56 # ie "01" in 2001 and "99" in 1999
57
58  
59 =head1 USAGE
60  
61 Its simple. Just use (do not require!) the module. If it detects a
62 problem, it will cause your program to abort with an error. If you
63 don't like this, you can use the module with the C<:WARN> tag like so:
64  
65 use D'oh::Year qw(:WARN);
66  
67 and it will warn upon seeing a year mishandling instead of dying.
68  
69 Because there is a I<slight> performance loss when using D'oh::Year, you
70 might want to only use it during development and testing. A few
71 suggestions for use...
72  
73 =over 4
74  
75 =item B<Shove it down their throats>
76  
77 Set up /usr/bin/perl on your development machine as a shell wrapper around
78 perl which always uses D'oh::Year:
79  
80 #!/bin/sh
81  
82 perl -MD::oh::Year $@
83  
84 This might be a little draconian for normal usage.
85  
86 =item B<Add it to your test harness>
87  
88 =item B<Make a quick check>
89  
90 C<perl -MD::oh::Year myprogram>
91  
92 =back
93  
94  
95 =head1 CAVEATS
96  
97 This program does its checking at B<run time> not compile time. Thus
98 it is not simply enough to slap D'oh::Year on a program, run it once
99 and expect it to find everything. For a thourough scrubbing you must
100 make sure every line of code is excersied... but you already have test
101 harnesses set up to do that, RIGHT?!
102  
103  
104 =head1 TODO
105  
106 =over 4
107  
108 =item B<sort @times>
109  
110 Sorting time()'s as strings is a common mistake. I can't detect it
111 without some XS code to look at the op stack.
112  
113 =item B<printf "19%02d", $year>
114  
115 I can't handle this without being able to override printf(), but can't
116 do that because it has a complex prototype. This could be handled,
117 but it would require a patch to pp_printf. I can do sprintf(), but I
118 don't think its wise to be non-orthoganal and lead non-doc readers on
119 that if sprintf() is handled, printf() should be, too.
120  
121 =back
122  
123  
124 =head1 AUTHOR
125  
126 Original idea by Andrew Langmead
127  
128 Original code by Mark "The Ominous" Dominous
129  
130 Cleaned up and maintained by Michael G Schwern <schwern@pobox.com>.
131  
132 =cut
133  
134 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
135 1 $VERSION = '0.06';
136  
137 3 ▶sub _mk_localtime {
Subroutine _mk_localtime: called CC=1 SCAR=0.4
138 3 my($reaction) = shift;
139
140 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=16.1
141 5 ▶ return @_ ? localtime(@_) : localtime() unless wantarray;
Branch: 2 branches
BranchTrueFalseTotal
unless wantarray 0 5 5
@_ ? : 0 0 0
142 5 ▶ my @t = @_ ? localtime(@_) : localtime();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
143 5 $t[5] = D'oh::Year::year->new($t[5], $reaction);
144 5 @t;
145 }
146 3 }
147  
148 3 ▶sub _mk_gmtime {
Subroutine _mk_gmtime: called CC=1 SCAR=0.4
149 3 my($reaction) = shift;
150
151 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=16.1
152 5 ▶ return @_ ? gmtime(@_) : gmtime() unless wantarray;
Branch: 2 branches
BranchTrueFalseTotal
unless wantarray 0 5 5
@_ ? : 0 0 0
153 5 ▶ my @t = @_ ? gmtime(@_) : gmtime();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 5 5
154 5 $t[5] = D'oh::Year::year->new($t[5], $reaction);
155 5 @t;
156 }
157 3 }
158  
159 0 ▶sub _mk_time {
Subroutine _mk_time: not called CC=1 SCAR=6.9
160 0 my($reaction) = shift;
161
162 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 return D'oh::Year::time->new(time, $reaction);
164 }
165 0 }
166  
167  
168 3 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
169 3 () = shift; # Dump the package.
170 3 my $reaction = shift;
171 3 my $caller = caller;
172
173 3 ▶ $reaction = ':DIE' unless defined $reaction;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reaction 1 2 3
174  
175 3 ▶ $reaction = $reaction =~ /^:WARN/i ? 'warn' : 'die';
Branch: 1 branch
BranchTrueFalseTotal
$reaction =~ /^:WARN/i ? : 1 2 3
176
177 {
178 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 3 *{$caller . '::localtime'} = &_mk_localtime($reaction);
180 3 *{$caller . '::gmtime'} = &_mk_gmtime($reaction);
181 # Didn't pan out.
182 # *{$caller . '::time'} = &_mk_time($reaction);
183 }
184 }
185  
186  
187 package D'oh::Year::year;
188  
189 1 ▶use fields qw(_Year _Reaction);
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192  
193 1 ▶use overload '.' => \&concat,
Subroutine BEGIN: called CC=1 SCAR=0.0
194 '""' => \&stringize,
195 '0+' => \&numize,
196 '-' => \&subtract,
197 'fallback' => 'TRUE',
198 1 ;
199  
200  
201 10 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
202 10 my $proto = shift;
203 10 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
204 10 my($year, $reaction) = @_;
205  
206 10 my $self = fields::new($class);
207 10 $self->{_Year} = $year;
208 10 ▶ $self->{_Reaction} = $reaction || 'die';
Condition50%1/2
$reaction || 'die'
l!l
100
MC/DC0%0/1
$reaction || 'die'
$reaction
Truth table50%1/2
A
A= $reaction
Aresult
11
00
209  
210 10 return $self;
211 }
212  
213  
214 78 ▶sub concat {
Subroutine concat: called CC=5 SCAR=16.1
215 78 my ($self, $a2, $rev) = @_;
216  
217 78 ▶ if ($a2 =~ /(19|200?)$/ && $rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a2 =~ /(19|200?)$/ and $rev) 54 24 78
Condition100%3/3
$a2 =~ /(19|200?)$/ and $rev
!ll&&!rl&&r
121254
MC/DC100%2/2
$a2 =~ /(19|200?)$/ and $rev
$a2 =~ /(19|200?)$/ $rev
Truth table100%3/3
A and B
A= $a2 =~ /(19|200?)$/B= $rev
ABresult
0X0
100
111
218 54 require Carp;
219 54 ▶ if ( $self->{_Reaction} eq 'warn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_Reaction'} eq 'warn') { } 18 36 54
220 18 Carp::carp("Possible year misuse.");
221 } else {
222 36 Carp::croak("Possible year misuse.");
223 }
224 }
225
226 24 ▶ if ($rev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev) { } 12 12 24
227 12 return $a2 . $self->{_Year};
228 } else {
229 12 return $self->{_Year} . $a2;
230 }
231 }
232  
233 2 ▶sub stringize {
Subroutine stringize: called CC=1 SCAR=0.0
234 2 return shift->{_Year};
235 }
236  
237  
238 12 ▶sub numize {
Subroutine numize: called CC=1 SCAR=0.0
239 12 return shift->{_Year};
240 }
241  
242 18 ▶sub subtract {
Subroutine subtract: called CC=3 SCAR=11.0
243 18 my($self, $num) = @_;
244 18 ▶ if( $num == 100 ) { # Catch $year -= 100
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 100) 12 6 18
245 12 require Carp;
246 12 ▶ if ( $self->{_Reaction} eq 'warn' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_Reaction'} eq 'warn') { } 4 8 12
247 4 Carp::carp("Possible year misuse.");
248 } else {
249 8 Carp::croak("Possible year misuse.");
250 }
251 }
252  
253 6 return $self->{_Year} - $num;
254 }
255  
256 # I had an idea about catching C<sort time>, but it didn't pan out.
257 package D'oh::Year::time;
258  
259 1 ▶use fields qw(_Time _Reaction);
Subroutine BEGIN: called CC=1 SCAR=0.0
260  
261 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 1 ▶use overload '""' => \&stringize,
Subroutine BEGIN: called CC=1 SCAR=0.0
264 '0+' => \&numize,
265 'fallback' => 'TRUE',
266 1 ;
267  
268  
269 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
270 0 my $proto = shift;
271 0 ▶ my $class = ref $proto || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
272 0 my($time, $reaction) = @_;
273  
274 0 my $self = fields::new($class);
275 0 $self->{_Time} = $time;
276 0 ▶ $self->{_Reaction} = $reaction || 'die';
Condition0%0/2
$reaction || 'die'
l!l
00
MC/DC0%0/1
$reaction || 'die'
$reaction
Truth table0%0/2
A
A= $reaction
Aresult
11
00
277  
278 0 return $self;
279 }
280  
281  
282 0 ▶sub stringize {
Subroutine stringize: not called CC=1 SCAR=6.9
283 # XXX Need code to figure out if we're being called directly from
284 # XXX a sort.
285 0 return shift->{_Time};
286 }
287  
288  
289 0 ▶sub numize {
Subroutine numize: not called CC=1 SCAR=6.9
290 0 return shift->{_Time};
291 }
292  
293  
294  
295 1 return 'sc_current_century';
↑ Summary y2k.pm »