| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | ▶ | while (my($key, $value) = splice(@_, 0, 2)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 15 | ▶ | croak "$key: invalid parameter" if not exists $obj->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | if ($start =~ /^-\d+$/ or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$start =~ /^-\d+$/u or defined $end and $end =~ /^-\d+$/u
defined $end and $end =~ /^-\d+$/u
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$start =~ /\d/u and defined $max and $start > $max
$start =~ /\d/u and defined $max
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
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | return (); | |||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 32 | ▶ | if ($start eq '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | $end = $start; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | elsif ($end eq '') { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | $end = defined $max ? $max : $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $max and $end > $max
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
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 32 | ▶ | $length ||= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$length ||= 1
MC/DC100%1/1
$length ||= 1
$length
Truth table100%2/2
A
A= $length
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 32 | ▶ | $step ||= $length; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$step ||= $length
MC/DC0%0/2
$step ||= $length
$step
$length
Truth table66%2/3
A ||= B
A= $stepB= $length
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # Ensure step is positive to avoid infinite loop | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 32 | ▶ | croak "step must be positive" if $step <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 32 | my @l; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 32 | ▶ | if ($step == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 15 | @l = ( [$start, $end] ); | |||||||||||||||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 17 | ▶ | for (my $from = $start; $from <= $end; $from += $step) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 75 | my $to = $from + $length - 1; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 75 | ▶ | $to = List::Util::min($max, $to) if defined $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||||||||||||||||