| 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
| |||||||||||||||||||||||||||||||||||||||
| 29 | 23 | my $key = shift @args; | |||||||||||||||||||||||||||||||||||||
| 30 | 23 | ▶ | croakf 'Expected pipeline, not %s', $key // 'undef' | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
defined $key and $key eq "pipeline"
$key // "undef"
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"
Truth table100%2/2
A
A= $key
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 53 | 26 | ▶ | croakf 'Expected a name for a pipeline (%s)', $name // 'undef' | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
defined $name and $name =~ / \A [^\W\d] \w* \z /ux
$name // "undef"
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
Truth table100%2/2
A
A= $name
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 57 | 22 | ▶ | croakf 'Expected the pipeline %s in an ARRAY', $name | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 58 | unless is_plain_arrayref($stages); | ||||||||||||||||||||||||||||||||||||||
| 59 | 21 | ▶ | croakf 'Expected no function of the name in %s already (%s)', $package, $name | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@stages == 1 and not is_stage($stages[0])
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])
| |||||||||||||||||||||||||||||||||||||||
| 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};})
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};})
| |||||||||||||||||||||||||||||||||||||||
| 91 | 11 | }; | |||||||||||||||||||||||||||||||||||||
| 92 | 11 | ▶ | while (@order < @$names) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
not defined $stage or Ref::Util::XS::is_ref($stage)
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)
| |||||||||||||||||||||||||||||||||||||||
| 108 | 13 | my($local) = $stage =~ m( \A \Q$package\E :: (\w+) \z )x; | |||||||||||||||||||||||||||||||||||||
| 109 | 13 | ▶ | return $local if defined $local; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 110 | 11 | ▶ | return if $stage =~ m( :: )x; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@values and $values[-1] isa "Ekorn::Pipeline"
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"
| |||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$given and builtin::refaddr($given) == builtin::refaddr($more)
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)
| |||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||