EX/Numbers.pm

filter: Statementstmt 93.8% 76 / 81 Branchbran 80.0% 32 / 40 Conditioncond 70.0% 14 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 83.9% 141 / 168 CC 28 SCAR 27.5
CC
28
Cov
87%
range
25.0
new
4.0
parse
2.0
CRAP 15.6
↑ Summary EX/Module.pm »
1 package Getopt::EX::Numbers;
2  
3 1 our $VERSION = "3.05";
4  
5 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Hash::Util qw(lock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $Data::Dumper::Sortkeys = 1;
13  
14 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 our @EXPORT_OK = qw();
16  
17 6 ▶sub _default {
Subroutine _default: called CC=1 SCAR=0.0
18 return {
19 6 min => 0,
20 max => undef,
21 start => '',
22 end => '',
23 step => '',
24 length => '',
25 _spec => undef,
26 };
27 }
28  
29 6 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
30 6 my $class = shift;
31 6 my $obj = bless _default(), $class;
32 6 lock_keys %{$obj};
33 6 ▶ @_ % 2 and croak "invalid number of parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 and croak("invalid number of parameters") 0 6 6
34 6 ▶ while (my($key, $value) = splice(@_, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 15 6 21
35 15 ▶ croak "$key: invalid parameter" if not exists $obj->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{$key} 0 15 15
36 15 $obj->{$key} = $value;
37 }
38 6 $obj;
39 }
40  
41 31 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.2
Pod documented
42 31 my $obj = shift;
43 31 local $_ = shift;
44 31 ▶ if (m{
Branch: 1 branch
BranchTrueFalseTotal
if (/ ^ (?<start> -\d+ | \d* ) (?: (?: \.\. | : ) (?<end> [-+]\d+ | \d* ) (?: : (?<step> \d* ) (?: : (?<length> \d* ) )? )? )? $ /ux) { } 31 0 31
45 ^
46 (?<start> -\d+ | \d* )
47 (?:
48 (?: \.\. | : ) (?<end> [-+]\d+ | \d* )
49 (?:
50 : (?<step> \d* )
51 (?:
52 : (?<length> \d* )
53 )?
54 )?
55 )?
56 $
57 }x) {
58 31 $obj->{start} = $+{start};
59 31 $obj->{end} = $+{end};
60 31 $obj->{step} = $+{step};
61 31 $obj->{length} = $+{length};
62 }
63 else {
64 0 carp "$_: format error";
65 0 return undef;
66 }
67 31 $obj->{_spec} = $_;
68 31 $obj;
69 }
70  
71 32 ▶sub range {
Subroutine range: called CC=23 SCAR=32.2
Pod documented
72 32 my $obj = shift;
73 32 my $max = $obj->{max};
74 32 my $min = $obj->{min};
75  
76 32 my $start = $obj->{start};
77 32 my $end = $obj->{end};
78 32 my $step = $obj->{step};
79 32 my $length = $obj->{length};
80  
81 32 ▶ if (not defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $max) 2 30 32
82 2 ▶ if ($start =~ /^-\d+$/ or
Branch: 1 branch
BranchTrueFalseTotal
if ($start =~ /^-\d+$/u or defined $end and $end =~ /^-\d+$/u) 0 2 2
Condition33%2/6
$start =~ /^-\d+$/u or defined $end and $end =~ /^-\d+$/u
l!l&&r!l&&!r
002
defined $end and $end =~ /^-\d+$/u
!ll&&!rl&&r
020
MC/DC0%0/3
$start =~ /^-\d+$/u or defined $end and $end =~ /^-\d+$/u
$start =~ /^-\d+$/u defined $end $end =~ /^-\d+$/u
Truth table25%1/4
A or B and C
A= $start =~ /^-\d+$/uB= defined $endC= $end =~ /^-\d+$/u
ABCresult
1XX1
0111
00X0
0100
83 (defined $end and $end =~ /^-\d+$/)) {
84 0 carp "$_: max required";
85 0 return ();
86 }
87 }
88  
89 32 ▶ if ($start =~ /\d/ and defined $max and $start > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start =~ /\d/u and defined $max and $start > $max) 0 32 32
Condition83%5/6
$start =~ /\d/u and defined $max and $start > $max
!ll&&!rl&&r
19130
$start =~ /\d/u and defined $max
!ll&&!rl&&r
17213
MC/DC0%0/3
$start =~ /\d/u and defined $max and $start > $max
$start =~ /\d/u defined $max $start > $max
Truth table75%3/4
A and B and C
A= $start =~ /\d/uB= defined $maxC= $start > $max
ABCresult
0XX0
10X0
1100
1111
90 0 return ();
91 }
92 32 ▶ if ($start eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($start eq '') { } 17 15 32
elsif ($start =~ /^-\d+$/u) { } 2 13 15
93 17 $start = $min;
94 }
95 elsif ($start =~ /^-\d+$/) {
96 2 $start = List::Util::max($min, $start + $max);
97 }
98  
99 32 ▶ if (not defined $end) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $end) { } 3 29 32
elsif ($end eq '') { } 7 22 29
elsif ($end =~ /^-/u) { } 2 20 22
elsif ($end =~ s/^\+//u) { } 1 19 20
100 3 $end = $start;
101 }
102 elsif ($end eq '') {
103 7 ▶ $end = defined $max ? $max : $start;
Branch: 1 branch
BranchTrueFalseTotal
defined $max ? : 7 0 7
104 }
105 elsif ($end =~ /^-/) {
106 2 $end = List::Util::max(0, $end + $max);
107 }
108 elsif ($end =~ s/^\+//) {
109 1 $end += $start;
110 }
111 32 ▶ $end = $max if defined $max and $end > $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max and $end > $max 2 30 32
Condition100%3/3
defined $max and $end > $max
!ll&&!rl&&r
2282
MC/DC100%2/2
defined $max and $end > $max
defined $max $end > $max
Truth table100%3/3
A and B
A= defined $maxB= $end > $max
ABresult
0X0
100
111
112  
113 32 ▶ $length ||= 1;
Condition100%2/2
$length ||= 1
l!l
1022
MC/DC100%1/1
$length ||= 1
$length
Truth table100%2/2
A
A= $length
Aresult
11
00
114 32 ▶ $step ||= $length;
Condition66%2/3
$step ||= $length
l!l&&r!l&&!r
16160
MC/DC0%0/2
$step ||= $length
$step $length
Truth table66%2/3
A ||= B
A= $stepB= $length
ABresult
1X1
011
000
115  
116 # Ensure step is positive to avoid infinite loop
117 32 ▶ croak "step must be positive" if $step <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step <= 0 0 32 32
118  
119 32 my @l;
120 32 ▶ if ($step == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step == 1) { } 15 17 32
121 15 @l = ( [$start, $end] );
122 } else {
123 17 ▶ for (my $from = $start; $from <= $end; $from += $step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from <= $end) 75 17 92
124 75 my $to = $from + $length - 1;
125 75 ▶ $to = List::Util::min($max, $to) if defined $max;
Branch: 1 branch
BranchTrueFalseTotal
if defined $max 67 8 75
126 75 push @l, [$from, $to];
127 }
128 }
129  
130 32 return @l;
131 }
132  
133 17 ▶sub sequence {
Subroutine sequence: called CC=2 SCAR=7.1
Pod documented
134 17 my $obj = shift;
135 17 ▶ map { ref $_ eq 'ARRAY' ? ($_->[0] .. $_->[1]) : $_ } $obj->range;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 57 0 57
136 }
137  
138 1 1;
139  
140 __END__
↑ Summary EX/Module.pm »