lib/Date/Manip/Obj.pm

filter: Statementstmt 93.6% 159 / 172 (2 uncoverable) Branchbran 95.7% 66 / 70 (1 uncoverable) Conditioncond 83.3% 15 / 18 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 95.6% 22 / 23 Podpod 100% 14 / 14 totaltotal 93.2% 285 / 309 (3 uncoverable) CC 45 SCAR 23.4
CC
45
Cov
93%
new
23.0
version
9.2
get_config
7.0
CRAP 10.3
« lib/Date/Manip/TZ_Base.pm ↑ Summary blib/lib/Date/Manip/Lang/finnish.pm »
1 package Date::Manip::Obj;
2 # Copyright (c) 2008-2026 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5  
6 ########################################################################
7 ########################################################################
8  
9 170 require 5.010000;
10 170 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 170 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 170 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 170 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 170 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 170 our ($VERSION);
17 170 $VERSION='7.00';
18 170 ▶END { undef $VERSION; }
Subroutine END: called CC=1 SCAR=0.0
19  
20 ########################################################################
21 # METHODS
22 ########################################################################
23  
24 170 my %classes = ( 'Date::Manip::Base' => 1,
25 'Date::Manip::TZ' => 1,
26 'Date::Manip::Date' => 1,
27 'Date::Manip::Delta' => 1,
28 'Date::Manip::Recur' => 1,
29 );
30  
31 21251 ▶sub new {
Subroutine new: called CC=23 SCAR=31.4
Pod documented
32 21251 my(@args) = @_;
33 21251 my(@allargs) = @args;
34  
35 # $old is the object (if any) being used to create a new object
36 # $new is the new object
37 # $class is the class of the new object
38 # $tz is a Date::Manip::TZ object to base the new object on
39 # (only for Date, Delta, Recur objects)
40 # $base is the Date::Manip::Base object to base the new object on
41 # @opts options to pass to config method
42  
43 21251 my($old,$new,$class,$tz,$base,@opts);
44  
45 # Get the class of the new object
46  
47 21251 ▶ if (exists $classes{ $args[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $classes{$args[0]}) { } 21238 13 21251
48  
49 # $obj = new CLASS
50 21238 $class = shift(@args);
51  
52 } else {
53  
54 # $obj->new
55 13 $class = ref($args[0]);
56 }
57  
58 # Find out if there are any config options (which will be the
59 # final argument).
60  
61 21251 ▶ if (@args && ref($args[$#args]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args and ref $args[$#args] eq "ARRAY") 16 21235 21251
Condition100%3/3
@args and ref $args[$#args] eq "ARRAY"
!ll&&!rl&&r
14581977716
MC/DC100%2/2
@args and ref $args[$#args] eq "ARRAY"
@args ref $args[$#args] eq "ARRAY"
Truth table100%3/3
A and B
A= @argsB= ref $args[$#args] eq "ARRAY"
ABresult
0X0
100
111
62 16 @opts = @{ pop(@args) };
63 }
64  
65 # Get an old object
66  
67 21251 ▶ if (ref($args[0]) =~ /^Date::Manip/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($args[0]) =~ /^Date::Manip/) 19783 1468 21251
68 # $old->new
69 # new CLASS $old
70 19783 $old = shift(@args);
71 }
72  
73 # Additional arguments will be passed to parse.
74  
75 ########################
76  
77 # Get Base/TZ objects from an existing object
78  
79 21251 ▶ if ($old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old) 19783 1468 21251
80 19783 ▶ if (ref($old) eq 'Date::Manip::Base') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $old eq 'Date::Manip::Base') { } 17 19766 19783
elsif (ref $old eq 'Date::Manip::TZ') { } 3 19763 19766
81 17 $base = $old;
82 } elsif (ref($old) eq 'Date::Manip::TZ') {
83 3 $tz = $old;
84 3 $base = $$tz{'base'};
85  
86 # *** I think this is useless code, deprecate
87 # } elsif (ref($old) eq 'ARRAY') {
88 # my %old = @$old;
89 # $tz = $old{'tz'};
90 # $base = $$tz{'base'};
91  
92 } else {
93 19763 $tz = $$old{'tz'};
94 19763 $base = $$tz{'base'};
95 }
96 }
97  
98 # Create a new empty object.
99  
100 $new = {
101 21251 'data' => {},
102 'err' => '',
103 };
104  
105 # Create Base/TZ objects if necessary
106  
107 21251 ▶ if ($base && @opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base and @opts) 13 21238 21251
Condition100%3/3
$base and @opts
!ll&&!rl&&r
14681977013
MC/DC100%2/2
$base and @opts
$base @opts
Truth table100%3/3
A and B
A= $baseB= @opts
ABresult
0X0
100
111
108 13 $base = _clone($base);
109 13 ▶ $tz = new Date::Manip::TZ $base if ($tz);
Branch: 1 branch
BranchTrueFalseTotal
if $tz 12 1 13
110 }
111  
112 21251 my $init = 1;
113 21251 ▶ if ($class eq 'Date::Manip::Base') {
Branch: 2 branches
BranchTrueFalseTotal
if ($class eq 'Date::Manip::Base') { } 509 20742 21251
elsif ($class eq 'Date::Manip::TZ') { } 518 20224 20742
114 509 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 4 505 509
115 # new Date::Manip::Base $base
116 #
117 # We have to clone it (which we already did if @opts was given)
118 #
119 4 ▶ if (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) { } 2 2 4
120 2 $new = $base;
121 } else {
122 2 $new = _clone($base);
123 }
124 4 $init = 0;
125 }
126  
127 } elsif ($class eq 'Date::Manip::TZ') {
128 518 ▶ if ($tz) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tz) { } 3 515 518
elsif (not $base) { } 502 13 515
129 # new Date::Manip::TZ $tz
130 3 ▶ if (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) { } 2 1 3
131 2 $new = $tz;
132 } else {
133 1 $new = _clone($tz);
134 }
135 3 $init = 0;
136 } elsif (! $base) {
137 502 $base = new Date::Manip::Base;
138 }
139 518 $$new{'base'} = $base;
140  
141 } else {
142 20224 ▶ if (! $tz) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tz) 462 19762 20224
143 462 ▶ if ($base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 1 461 462
144 1 $tz = new Date::Manip::TZ $base;
145 } else {
146 461 $tz = new Date::Manip::TZ;
147 }
148 }
149 20224 $$new{'tz'} = $tz;
150 }
151  
152 21251 $$new{'args'} = [ @args ];
153 21251 bless $new,$class;
154  
155 21251 ▶ $new->_init() if ($init);
Branch: 1 branch
BranchTrueFalseTotal
if $init 21244 7 21251
156 21251 ▶ $new->config(@opts) if (@opts);
Branch: 1 branch
BranchTrueFalseTotal
if @opts 16 21235 21251
157 21251 ▶ $new->_init_args() if (@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 15 21236 21251
158 21251 $new->_init_final();
159 21251 return $new;
160 }
161  
162 # This clones an object. Currently, it only clones a Base or TZ
163 # object, but dclone can't handle stored regexps so we have to copy
164 # them manually.
165 #
166 16 ▶sub _clone {
Subroutine _clone: called CC=4 SCAR=13.9
167 16 my($obj) = @_;
168  
169 16 ▶ if (ref($obj) eq 'Date::Manip::Base') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq 'Date::Manip::Base') { } 15 1 16
170  
171 15 my $tmp = $$obj{'data'}{'rx'};
172 15 delete $$obj{'data'}{'rx'};
173 15 my $new = dclone($obj);
174 15 $$obj{'data'}{'rx'} = $tmp;
175 15 $$new{'data'}{'rx'} = $tmp;
176 15 return $new;
177  
178 } else {
179  
180 1 my $base = $$obj{'base'};
181 1 delete $$obj{'base'};
182  
183 1 my @rx = qw(namerx zonerx abbrx offrx zrx offabbrx orrparrx);
184 1 my @tmp;
185 1 foreach my $rx (@rx) {
186 7 push(@tmp,$$obj{'data'}{$rx});
187 7 delete $$obj{'data'}{$rx};
188 }
189  
190 1 my $new = dclone($obj);
191  
192 1 foreach my $rx (@rx) {
193 7 my $r = shift(@tmp);
194 7 $$obj{'data'}{$rx} = $r;
195 7 $$new{'data'}{$rx} = $r;
196 }
197  
198 1 $$obj{'base'} = $base;
199 1 $$new{'base'} = $base;
200 1 return $new;
201 }
202 }
203  
204 # Only called if extra @args exist
205 0 ▶sub _init_args {
Subroutine _init_args: not called CC=1 SCAR=6.9
206 0 my($self) = @_;
207  
208 0 my @args = @{ $$self{'args'} };
209 0 carp "WARNING: [new] invalid arguments: @args";
210  
211 0 return;
212 }
213  
214 20733 ▶sub _init_final {
Subroutine _init_final: called CC=1 SCAR=0.0
215 20733 my($self) = @_;
216 20733 return;
217 }
218  
219 8 ▶sub new_config {
Subroutine new_config: called CC=3 SCAR=11.0
Pod documented
220 8 my(@args) = @_;
221  
222 # Make sure that @opts is passed in as the final argument.
223  
224 8 ▶ if (! @args ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@args and ref $args[$#args] eq "ARRAY") 6 2 8
Condition66%2/3
@args and ref $args[$#args] eq "ARRAY"
!ll&&!rl&&r
062
MC/DC50%1/2
@args and ref $args[$#args] eq "ARRAY"
@args ref $args[$#args] eq "ARRAY"
Truth table66%2/3
A and B
A= @argsB= ref $args[$#args] eq "ARRAY"
ABresult
0X0
100
111
225 ! (ref($args[$#args]) eq 'ARRAY')) {
226 6 push(@args,['ignore','ignore']);
227 }
228  
229 8 return new(@args);
230 }
231  
232 13959 ▶sub new_date {
Subroutine new_date: called CC=1 SCAR=0.0
Pod documented
233 13959 my(@args) = @_;
234 13959 require Date::Manip::Date;
235 13959 return new Date::Manip::Date @args;
236 }
237 5510 ▶sub new_delta {
Subroutine new_delta: called CC=1 SCAR=0.0
Pod documented
238 5510 my(@args) = @_;
239 5510 require Date::Manip::Delta;
240 5510 return new Date::Manip::Delta @args;
241 }
242 279 ▶sub new_recur {
Subroutine new_recur: called CC=1 SCAR=0.0
Pod documented
243 279 my(@args) = @_;
244 279 require Date::Manip::Recur;
245 279 return new Date::Manip::Recur @args;
246 }
247  
248 19626 ▶sub base {
Subroutine base: called CC=3 SCAR=11.0
Pod documented
249 19626 my($self) = @_;
250 19626 my $t = ref($self);
251 19626 ▶ if ($t eq 'Date::Manip::Base') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'Date::Manip::Base') { } 1 19625 19626
elsif ($t eq 'Date::Manip::TZ') { } 17912 1713 19625
252 1 return undef;
253 } elsif ($t eq 'Date::Manip::TZ') {
254 17912 return $$self{'base'};
255 } else {
256 1713 my $dmt = $$self{'tz'};
257 1713 return $$dmt{'base'};
258 }
259 }
260  
261 195 ▶sub tz {
Subroutine tz: called CC=3 SCAR=11.0
Pod documented
262 195 my($self) = @_;
263 195 my $t = ref($self);
264  
265 195 ▶ if ($t eq 'Date::Manip::Base' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "Date::Manip::Base" or $t eq "Date::Manip::TZ") 2 193 195
Condition100%3/3
$t eq "Date::Manip::Base" or $t eq "Date::Manip::TZ"
l!l&&r!l&&!r
11193
MC/DC100%2/2
$t eq "Date::Manip::Base" or $t eq "Date::Manip::TZ"
$t eq "Date::Manip::Base" $t eq "Date::Manip::TZ"
Truth table100%3/3
A or B
A= $t eq "Date::Manip::Base"B= $t eq "Date::Manip::TZ"
ABresult
1X1
011
000
266 $t eq 'Date::Manip::TZ') {
267 2 return undef;
268 }
269  
270 193 return $$self{'tz'};
271 }
272  
273 730 ▶sub config {
Subroutine config: called CC=4 SCAR=13.9
Pod documented
274 730 my($self,@opts) = @_;
275 730 my $obj;
276 730 ▶ if (ref($self) eq 'Date::Manip::Base' ||
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Date::Manip::Base' or ref $self eq 'Date::Manip::TZ') { } 339 391 730
Condition100%3/3
ref $self eq "Date::Manip::Base" or ref $self eq "Date::Manip::TZ"
l!l&&r!l&&!r
57282391
MC/DC100%2/2
ref $self eq "Date::Manip::Base" or ref $self eq "Date::Manip::TZ"
ref $self eq "Date::Manip::Base" ref $self eq "Date::Manip::TZ"
Truth table100%3/3
A or B
A= ref $self eq "Date::Manip::Base"B= ref $self eq "Date::Manip::TZ"
ABresult
1X1
011
000
277 ref($self) eq 'Date::Manip::TZ') {
278 339 $obj = $self;
279 } else {
280 391 $obj = $$self{'tz'};
281 }
282  
283 730 ▶ while (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) 768 730 1498
284 768 my $var = shift(@opts);
285 768 my $val = shift(@opts);
286 768 $obj->_config_var($var,$val);
287 }
288  
289 730 return;
290 }
291  
292 8 ▶sub get_config {
Subroutine get_config: called CC=7 SCAR=19.5
Pod documented
293 8 my($self,@args) = @_;
294  
295 8 my $base;
296 8 my $t = ref($self);
297 8 ▶ if ($t eq 'Date::Manip::Base') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'Date::Manip::Base') { } 2 6 8
elsif ($t eq 'Date::Manip::TZ') { } 2 4 6
298 2 $base = $self;
299 } elsif ($t eq 'Date::Manip::TZ') {
300 2 $base = $$self{'base'};
301 } else {
302 4 my $dmt = $$self{'tz'};
303 4 $base = $$dmt{'base'};
304 }
305  
306 8 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 7 1 8
307 7 my @ret;
308 7 foreach my $var (@args) {
309 # uncoverable branch false
310 8 ▶ if (exists $$base{'data'}{'sections'}{'conf'}{lc($var)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $base->{'data'}{'sections'}{'conf'}{lc $var}) { } 8 0 8
311 8 push @ret,$$base{'data'}{'sections'}{'conf'}{lc($var)};
312 } else {
313 # uncoverable statement
314 0 carp "ERROR: [config] invalid config variable: $var";
315 # uncoverable statement
316 0 return '';
317 }
318 }
319  
320 7 ▶ if (@ret == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ret == 1) { } 6 1 7
321 6 return $ret[0];
322 } else {
323 1 return @ret;
324 }
325 }
326  
327 1 my @ret = sort keys %{ $$base{'data'}{'sections'}{'conf'} };
328 1 return @ret;
329 }
330  
331 11013 ▶sub err {
Subroutine err: called CC=2 SCAR=6.9
Pod documented
332 11013 my($self,$arg) = @_;
333 11013 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 1419 9594 11013
334 1419 $$self{'err'} = '';
335 1419 return;
336 } else {
337 9594 return $$self{'err'};
338 }
339 }
340  
341 2 ▶sub is_date {
Subroutine is_date: called CC=1 SCAR=0.0
Pod documented
342 2 return 0;
343 }
344 2 ▶sub is_delta {
Subroutine is_delta: called CC=1 SCAR=0.0
Pod documented
345 2 return 0;
346 }
347 2 ▶sub is_recur {
Subroutine is_recur: called CC=1 SCAR=0.0
Pod documented
348 2 return 0;
349 }
350  
351 170 ▶sub version {
Subroutine version: called CC=4 SCAR=22.2
Pod documented
352 170 my($self,$flag) = @_;
353 170 ▶ if ($flag && ref($self) ne 'Date::Manip::Base') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag and ref $self ne 'Date::Manip::Base') { } 0 170 170
Condition33%1/3
$flag and ref $self ne "Date::Manip::Base"
!ll&&!rl&&r
17000
MC/DC0%0/2
$flag and ref $self ne "Date::Manip::Base"
$flag ref $self ne "Date::Manip::Base"
Truth table33%1/3
A and B
A= $flagB= ref $self ne "Date::Manip::Base"
ABresult
0X0
100
111
354 0 my $dmt;
355 0 ▶ if (ref($self) eq 'Date::Manip::TZ') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq 'Date::Manip::TZ') { } 0 0 0
356 0 $dmt = $self;
357 } else {
358 0 $dmt = $$self{'tz'};
359 }
360 0 my $tz = $dmt->_now('systz');
361 0 return "$VERSION [$tz]";
362 } else {
363 170 return $VERSION;
364 }
365 }
366  
367 170 1;
368 # Local Variables:
369 # mode: cperl
370 # indent-tabs-mode: nil
371 # cperl-indent-level: 3
372 # cperl-continued-statement-offset: 2
373 # cperl-continued-brace-offset: 0
374 # cperl-brace-offset: 0
375 # cperl-brace-imaginary-offset: 0
376 # cperl-label-offset: 0
377 # End:
« lib/Date/Manip/TZ_Base.pm ↑ Summary blib/lib/Date/Manip/Lang/finnish.pm »