| 1 | package Number::Continuation; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use boolean qw(true); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Params::Validate ':all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | our ($VERSION, @EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | @EXPORT_OK = qw(continuation); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | validation_options( | ||||||||||||||||||||||||||||||||||||||||||
| 17 | on_fail => sub | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | my ($error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | chomp $error; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | croak $error; | |||||||||||||||||||||||||||||||||||||||||
| 22 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | stack_skip => 2, | |||||||||||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | sub continuation | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
continuation: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | my (@list, %opts, $set); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 15 | _init(\$set, \%opts, \@_); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 15 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 7 | _construct($set, \@list); | |||||||||||||||||||||||||||||||||||||||||
| 34 | 7 | return @list; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 8 | return _format($set, \%opts); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | sub _init | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 15 | my ($set, $opts, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | ▶ | if (ref $args->[-1] eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | %$opts = %{$args->[-1]}; | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | pop @$args; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 15 | my $re_digits = qr!^\-?\d+$!; | |||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | my $spec = sub | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | my ($args, $spec) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | my @spec; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | push @spec, $spec while $args--; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | return @spec; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 15 | }; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 15 | ▶ | if (@$args == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | validate_pos(@$args, { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | type => SCALAR | ARRAYREF, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | callbacks => { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 'valid set' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 11 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 11 | ▶ | foreach my $num (ref $_[0] ? @{$_[0]} : (split /\s+/, $_[0])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 133 | ▶ | die "invalid number\n" unless $num =~ $re_digits; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 11 | ▶ | $$set = ref $_[0] ? $_[0] : [ split /\s+/, $_[0] ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 11 | return true; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 11 | }); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | elsif (@$args > 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | my %spec = ( | |||||||||||||||||||||||||||||||||||||||||
| 77 | type => SCALAR, | ||||||||||||||||||||||||||||||||||||||||||
| 78 | regex => $re_digits, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2 | validate_pos(@$args, $spec->(scalar @$args, \%spec)); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $$set = $args; | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | $$set = []; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 15 | my @args = %$opts; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 15 | validate(@args, { | |||||||||||||||||||||||||||||||||||||||||
| 89 | delimiter => { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | type => SCALAR, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | optional => true, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | regex => qr!^\S{2}$!, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | range => { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | type => SCALAR, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | optional => true, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | regex => qr!^\S{1,2}$!, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | separator => { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | type => SCALAR, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | optional => true, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | regex => qr!^\S$!, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 15 | ▶ | $opts->{delimiter} ||= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"delimiter"} ||= ""
MC/DC100%1/1
$opts->{"delimiter"} ||= ""
$opts->{"delimiter"}
Truth table100%2/2
A
A= $opts->{"delimiter"}
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 15 | ▶ | $opts->{range} ||= '-'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"range"} ||= "-"
MC/DC100%1/1
$opts->{"range"} ||= "-"
$opts->{"range"}
Truth table100%2/2
A
A= $opts->{"range"}
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 15 | ▶ | $opts->{separator} ||= ','; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"separator"} ||= ","
MC/DC100%1/1
$opts->{"separator"} ||= ","
$opts->{"separator"}
Truth table100%2/2
A
A= $opts->{"separator"}
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 15 | @{$opts->{delimiters}} = split //, $opts->{delimiter}; | |||||||||||||||||||||||||||||||||||||||||
| 111 | 15 | ▶ | $opts->{delimiters}[0] ||= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"delimiters"}[0] ||= ""
MC/DC100%1/1
$opts->{"delimiters"}[0] ||= ""
$opts->{"delimiters"}[0]
Truth table100%2/2
A
A= $opts->{"delimiters"}[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 15 | ▶ | $opts->{delimiters}[1] ||= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{"delimiters"}[1] ||= ""
MC/DC100%1/1
$opts->{"delimiters"}[1] ||= ""
$opts->{"delimiters"}[1]
Truth table100%2/2
A
A= $opts->{"delimiters"}[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | sub _construct | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_construct: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 7 | my ($set, $list) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 7 | my $prev_number = undef; | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | my $entry = []; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | foreach my $num (@$set) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 71 | ▶ | if (defined $prev_number | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $prev_number and not $num - $prev_number == 1 || $prev_number - $num == 1
$num - $prev_number == 1 || $prev_number - $num == 1
MC/DC100%3/3
defined $prev_number and not $num - $prev_number == 1 || $prev_number - $num == 1
defined $prev_number
$num - $prev_number == 1
$prev_number - $num == 1
Truth table100%4/4
A and not(B || C)
A= defined $prev_numberB= $num - $prev_number == 1C= $prev_number - $num == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | && !(($num - $prev_number == 1) # positive continuation | ||||||||||||||||||||||||||||||||||||||||||
| 125 | || ($prev_number - $num == 1) # negative continuation | ||||||||||||||||||||||||||||||||||||||||||
| 126 | )) { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 28 | push @$list, $entry; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 28 | $entry = []; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 71 | push @$entry, $num; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 71 | $prev_number = $num; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 7 | ▶ | push @$list, $entry if @$entry; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | sub _format | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | my ($set, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | my $string = ''; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | my $begin = sub | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 37 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 37 | my ($string, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 37 | $$string .= $opts->{delimiters}[0]; | |||||||||||||||||||||||||||||||||||||||||
| 146 | 37 | $$string .= $num; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 148 | my $range = sub | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 24 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 24 | my ($string, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 24 | $$string .= $opts->{range}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 24 | $$string .= $num; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 154 | my $end = sub | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 37 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 37 | my ($string) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 37 | $$string .= $opts->{delimiters}[1]; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 37 | $$string .= "$opts->{separator} "; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | my $consecutive = 0; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | my $prev_number = undef; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 8 | foreach my $num (@$set) { | |||||||||||||||||||||||||||||||||||||||||
| 165 | 80 | ▶ | if (!defined $prev_number) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | $begin->(\$string, $num); | |||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||
| 168 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 73 | ▶ | if (($num - $prev_number == 1) # positive continuation | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$num - $prev_number == 1 or $prev_number - $num == 1
MC/DC100%2/2
$num - $prev_number == 1 or $prev_number - $num == 1
$num - $prev_number == 1
$prev_number - $num == 1
Truth table100%3/3
A or B
A= $num - $prev_number == 1B= $prev_number - $num == 1
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | || ($prev_number - $num == 1) # negative continuation | ||||||||||||||||||||||||||||||||||||||||||
| 171 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 43 | $consecutive++; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | elsif ($consecutive) { | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 21 | $range->(\$string, $prev_number); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 21 | $end->(\$string); | |||||||||||||||||||||||||||||||||||||||||
| 177 | 21 | $begin->(\$string, $num); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 21 | $consecutive = 0; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 9 | $end->(\$string); | |||||||||||||||||||||||||||||||||||||||||
| 182 | 9 | $begin->(\$string, $num); | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 80 | $prev_number = $num; | |||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 8 | ▶ | if ($consecutive) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 3 | $range->(\$string, $prev_number); | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 8 | ▶ | if (@$set) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 7 | $end->(\$string); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | 8 | $string =~ s/\Q$opts->{separator}\E $//; | |||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 8 | return $string; | |||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||||||||||||||||||||||||