Pipeline/Sugar.pm

filter: Statementstmt 100% 89 / 89 Branchbran 100% 36 / 36 Conditioncond 96.0% 24 / 25 MC/DCmcdc 93.7% 15 / 16 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 98.9% 182 / 184 CC 30 SCAR 17.6
CC
30
Cov
99%
_define
11.0
import_into
8.0
plus_one
5.0
CRAP 5.8
« Pipeline.pm ↑ Summary Pipeline/Operators.pm »
1 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Ekorn::Pipeline::Sugar;
4  
5 1 ▶use experimental qw(builtin);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use builtin qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Ref::Util qw(is_plain_arrayref is_plain_hashref is_ref);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Ekorn::Pipeline qw(is_stage);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Ekorn::Pipeline::Errors qw(croakf);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Croak at the line that called Ekorn::Pipeline.
14 1 our @CARP_NOT = @Ekorn::Pipeline::Errors::INTERNAL;
15  
16 # Name the pipelines defined after "pipeline", in the package that uses
17 # this module, and give it a function of each name.
18 23 ▶sub import ($class, @args) {
Subroutine import: called CC=1 SCAR=0.0
19 23 $class->import_into(scalar caller, @args);
20 5 return;
21 }
22  
23  
24 # Name the pipelines defined after "pipeline" in $package, and give it a
25 # function of each name, as import does for its caller: for a module
26 # whose own sugar defines pipelines for its caller.
27 25 ▶sub import_into ($class, $package, @args) {
Subroutine import_into: called CC=8 SCAR=20.8
Pod documented
28 25 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 23 7 30
29 23 my $key = shift @args;
30 23 ▶ croakf 'Expected pipeline, not %s', $key // 'undef'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and $key eq "pipeline" 2 21 23
Condition100%5/5
defined $key and $key eq "pipeline"
!ll&&!rl&&r
1121
$key // "undef"
l!l
11
MC/DC100%2/2
defined $key and $key eq "pipeline"
defined $key $key eq "pipeline"
MC/DC100%1/1
$key // "undef"
$key
Truth table100%3/3
A and B
A= defined $keyB= $key eq "pipeline"
ABresult
0X0
100
111
Truth table100%2/2
A
A= $key
Aresult
11
00
31 unless defined $key && $key eq 'pipeline';
32 21 my $definitions = shift @args;
33 my @pairs
34 21 ▶ = is_plain_hashref($definitions) ? map { ($_, $definitions->{$_}) }
Branch: 2 branches
BranchTrueFalseTotal
Ref::Util::XS::is_plain_hashref($definitions) ? : 15 6 21
Ref::Util::XS::is_plain_arrayref($definitions) ? : 4 2 6
35 sort keys %$definitions
36 : is_plain_arrayref($definitions) ? @$definitions
37 : croakf
38 'Expected the pipelines after pipeline in a HASH or an ARRAY of pairs';
39 19 ▶ croakf 'Expected the pipelines after pipeline in pairs of a name and an ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if @pairs % 2 1 18 19
40 if @pairs % 2;
41 18 _define($package, @pairs);
42 }
43 7 return;
44 }
45  
46  
47 # Check the definitions given, as pairs of a name and an array of
48 # stages, then name each pipeline in $package, and give it a function of
49 # its name, each once those it names from the same pairs are.
50 18 ▶sub _define ($package, @pairs) {
Subroutine _define: called CC=11 SCAR=24.0
51 18 my(@names, %stages);
52 18 ▶ while (my($name, $stages) = splice @pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@pairs, 0, 2)) 26 11 37
53 26 ▶ croakf 'Expected a name for a pipeline (%s)', $name // 'undef'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ / \A [^\W\d] \w* \z /ux 3 23 26
Condition100%5/5
defined $name and $name =~ / \A [^\W\d] \w* \z /ux
!ll&&!rl&&r
1223
$name // "undef"
l!l
21
MC/DC100%2/2
defined $name and $name =~ / \A [^\W\d] \w* \z /ux
defined $name $name =~ / \A [^\W\d] \w* \z /ux
MC/DC100%1/1
$name // "undef"
$name
Truth table100%3/3
A and B
A= defined $nameB= $name =~ / \A [^\W\d] \w* \z /ux
ABresult
0X0
100
111
Truth table100%2/2
A
A= $name
Aresult
11
00
54 unless defined $name && $name =~ m( \A [^\W\d] \w* \z )x;
55 croakf 'Expected a pipeline defined once (%s)', $name
56 23 ▶ if $stages{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $stages{$name} 1 22 23
57 22 ▶ croakf 'Expected the pipeline %s in an ARRAY', $name
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_arrayref($stages) 1 21 22
58 unless is_plain_arrayref($stages);
59 21 ▶ croakf 'Expected no function of the name in %s already (%s)', $package, $name
Branch: 1 branch
BranchTrueFalseTotal
if $package->can($name) 2 19 21
60 if $package->can($name);
61 19 push @names, $name;
62 19 $stages{$name} = $stages;
63 }
64 11 for my $name (_in_order($package, \@names, \%stages)) {
65 15 my @stages = @{ $stages{$name} };
66 15 my $pipeline = Ekorn::Pipeline->pipeline_in($package, @stages);
67 # A name alone gives the pipeline it names; a new one is made of
68 # it, as named does, so a name never aliases another.
69 12 ▶ $pipeline = Ekorn::Pipeline->new($pipeline)
Branch: 1 branch
BranchTrueFalseTotal
if @stages == 1 and not is_stage($stages[0]) 1 11 12
Condition100%3/3
@stages == 1 and not is_stage($stages[0])
!ll&&!rl&&r
741
MC/DC100%2/2
@stages == 1 and not is_stage($stages[0])
@stages == 1 not is_stage($stages[0])
Truth table100%3/3
A and not(B)
A= @stages == 1B= not is_stage($stages[0])
ABresult
0X0
100
111
70 if @stages == 1 && !is_stage($stages[0]);
71 12 $pipeline->named("${package}::$name");
72 11 _install("${package}::$name", $pipeline);
73 }
74 5 return;
75 }
76  
77  
78 # Return the names in @$names in an order to define them in: each after
79 # those it names, from the same definitions, as given otherwise.
80 11 ▶sub _in_order ($package, $names, $stages) {
Subroutine _in_order: called CC=4 SCAR=13.9
81 # The names each definition uses from the same definitions, itself
82 # included, which makes a circle of one.
83 11 my %needs;
84 11 for my $name (@$names) {
85 18 my @named = map { _local_name($package, $_) } @{ $stages->{$name} };
86 18 $needs{$name} = [grep { $stages->{$_} } @named];
87 }
88 11 my(@order, %done);
89 36 ▶ my $ready = sub ($name) {
Subroutine __ANON__: called CC=2 SCAR=6.9
90 36 ▶ !$done{$name} && !grep { !$done{$_} } @{ $needs{$name} };
Condition100%3/3
$done{$name} || grep({not $done{$_};} @{$needs{$name};})
l!l&&r!l&&!r
9621
MC/DC100%2/2
$done{$name} || grep({not $done{$_};} @{$needs{$name};})
$done{$name} grep({not $done{$_};} @{$needs{$name};})
Truth table100%3/3
A || B
A= $done{$name}B= grep({not $done{$_};} @{$needs{$name};})
ABresult
1X1
011
000
91 11 };
92 11 ▶ while (@order < @$names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order < @$names) 17 9 26
93 17 my($next) = grep { $ready->($_) } @$names;
94 croakf q(Expected pipelines that don't name each other in a circle (%s)),
95 17 ▶ join ', ', grep { !$done{$_} } @$names
Branch: 1 branch
BranchTrueFalseTotal
unless defined $next 2 15 17
96 unless defined $next;
97 15 push @order, $next;
98 15 $done{$next} = 1;
99 }
100 9 return @order;
101 }
102  
103  
104 # Return the name a stage gives, without its package, if it's a name in
105 # $package; or nothing.
106 24 ▶sub _local_name ($package, $stage) {
Subroutine _local_name: called CC=5 SCAR=16.1
107 24 ▶ return if !defined $stage || is_ref($stage);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $stage or Ref::Util::XS::is_ref($stage) 11 13 24
Condition100%3/3
not defined $stage or Ref::Util::XS::is_ref($stage)
l!l&&r!l&&!r
11013
MC/DC100%2/2
not defined $stage or Ref::Util::XS::is_ref($stage)
not defined $stage Ref::Util::XS::is_ref($stage)
Truth table100%3/3
not(A) or B
A= not defined $stageB= Ref::Util::XS::is_ref($stage)
ABresult
1X1
011
000
108 13 my($local) = $stage =~ m( \A \Q$package\E :: (\w+) \z )x;
109 13 ▶ return $local if defined $local;
Branch: 1 branch
BranchTrueFalseTotal
if defined $local 2 11 13
110 11 ▶ return if $stage =~ m( :: )x;
Branch: 1 branch
BranchTrueFalseTotal
if $stage =~ / :: /ux 1 10 11
111 10 return $stage;
112 }
113  
114  
115 # Install a function of the full name given, running values through the
116 # pipeline, and, given a pipeline last, through that after it.
117 #
118 # Making the pipeline of both costs more than running a few values
119 # through it, so the last one made is kept, with the pipeline it was
120 # made for, and used again while that's the one given, as in a loop.
121 # Only the last is kept: the one made holds the one given, for naming
122 # its stages in errors, so a cache of every one given would keep them
123 # all for ever.
124 11 ▶sub _install ($name, $pipeline) {
Subroutine _install: called CC=1 SCAR=0.0
125 11 my($given, $both);
126 22 ▶ my $function = sub (@values) {
Subroutine plus_one: called CC=5 SCAR=16.1
127 22 ▶ return $pipeline->(@values)
Branch: 1 branch
BranchTrueFalseTotal
unless @values and $values[-1] isa "Ekorn::Pipeline" 12 10 22
Condition66%2/3
@values and $values[-1] isa "Ekorn::Pipeline"
!ll&&!rl&&r
01210
MC/DC50%1/2
@values and $values[-1] isa "Ekorn::Pipeline"
@values $values[-1] isa "Ekorn::Pipeline"
Truth table66%2/3
A and B
A= @valuesB= $values[-1] isa "Ekorn::Pipeline"
ABresult
0X0
100
111
128 unless @values && $values[-1] isa 'Ekorn::Pipeline';
129 10 my $more = pop @values;
130 10 ▶ ($given, $both) = ($more, Ekorn::Pipeline->new($pipeline, $more))
Branch: 1 branch
BranchTrueFalseTotal
unless $given and builtin::refaddr($given) == builtin::refaddr($more) 8 2 10
Condition100%3/3
$given and builtin::refaddr($given) == builtin::refaddr($more)
!ll&&!rl&&r
262
MC/DC100%2/2
$given and builtin::refaddr($given) == builtin::refaddr($more)
$given builtin::refaddr($given) == builtin::refaddr($more)
Truth table100%3/3
A and B
A= $givenB= builtin::refaddr($given) == builtin::refaddr($more)
ABresult
0X0
100
111
131 unless $given && refaddr($given) == refaddr($more);
132 10 return $both->(@values);
133 11 };
134 1 ▶ no strict 'refs'; ## no critic (TestingAndDebugging::ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
135 11 *{ $name } = set_subname($name, $function);
136 11 return;
137 }
138  
139 1 1;
140  
141 __END__
« Pipeline.pm ↑ Summary Pipeline/Operators.pm »