| 1 | package Iterator::Flex::Role::Wrap::Throw; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Role to add throw on exhaustion to an iterator which adapts another iterator | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use v5.28; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our $VERSION = '0.33'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use List::Util 'first'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Iterator::Flex::Utils qw( INPUT_EXHAUSTION PASSTHROUGH ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Ref::Util qw( is_ref is_blessed_ref is_regexpref is_arrayref is_coderef ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ▶ | around _construct_next => sub ( $orig, $class, $ipar, $gpar ) { | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.4
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 18 | my $next = $class->$orig( $ipar, $gpar ); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $exception = ( | ||||||||||||||||||||||||||||
| 24 | 18 | ▶ | $gpar->{ +INPUT_EXHAUSTION } // do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$gpar->{"input_exhaustion"} // do {
require Iterator::Flex::Failure;
"Iterator::Flex::Failure::parameter"->throw("internal error: input exhaustion policy was not registered")
}
MC/DC0%0/2
$gpar->{"input_exhaustion"} // do {
require Iterator::Flex::Failure;
"Iterator::Flex::Failure::parameter"->throw("internal error: input exhaustion policy was not registered")
}
$gpar->{"input_exhaustion"}
do {
require Iterator::Flex::Failure;
"Iterator::Flex::Failure::parameter"->throw("internal error: input exhaustion policy was not registered")
}
Truth table33%1/3
A // B
A= $gpar->{"input_exhaustion"}B= do {
require Iterator::Flex::Failure;
"Iterator::Flex::Failure::parameter"->throw("internal error: input exhaustion policy was not registered")
}
| |||||||||||||||||||||||||||||
| 25 | 0 | require Iterator::Flex::Failure; | |||||||||||||||||||||||||||
| 26 | 0 | Iterator::Flex::Failure::parameter->throw( | |||||||||||||||||||||||||||
| 27 | q{internal error: input exhaustion policy was not registered} ); | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | )->[1]; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 18 | my $wsub; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ## no critic ( CascadingIfElse ) | ||||||||||||||||||||||||||||
| 34 | 18 | ▶ | if ( !defined $exception ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | $wsub = sub { | ||||||||||||||||||||||||||||
| 37 | 58 | ▶ | my $self = $_[0] // $wsub; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
Condition33%1/3
$_[0] // $wsub
MC/DC0%0/2
$_[0] // $wsub
$_[0]
$wsub
Truth table33%1/3
A // B
A= $_[0]B= $wsub
| |||||||||||||||||||||||||||||
| 38 | 58 | my $val = eval { $next->( $self ) }; | |||||||||||||||||||||||||||
| 39 | 58 | ▶ | return $@ ne q{} ? $self->signal_exhaustion( $@ ) : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 6 | }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ▶ | elsif ( !is_ref( $exception ) && $exception eq PASSTHROUGH ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
not Ref::Util::XS::is_ref($exception) and $exception eq "passthrough"
MC/DC0%0/2
not Ref::Util::XS::is_ref($exception) and $exception eq "passthrough"
not Ref::Util::XS::is_ref($exception)
$exception eq "passthrough"
Truth table66%2/3
not(A) and B
A= not Ref::Util::XS::is_ref($exception)B= $exception eq "passthrough"
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | $wsub = sub { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $self = $_[0] // $wsub; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Condition0%0/3
$_[0] // $wsub
MC/DC0%0/2
$_[0] // $wsub
$_[0]
$wsub
Truth table0%0/3
A // B
A= $_[0]B= $wsub
| |||||||||||||||||||||||||||||
| 47 | 0 | my $val = eval { $next->( $self ) }; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | return $@ ne q{} ? $self->signal_exhaustion( $@ ) : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | }; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | ▶ | elsif ( !is_ref( $exception ) || is_arrayref( $exception ) ) { | |||||||||||||||||||||||||||
|
Condition66%2/3
not Ref::Util::XS::is_ref($exception) or Ref::Util::XS::is_arrayref($exception)
MC/DC50%1/2
not Ref::Util::XS::is_ref($exception) or Ref::Util::XS::is_arrayref($exception)
not Ref::Util::XS::is_ref($exception)
Ref::Util::XS::is_arrayref($exception)
Truth table66%2/3
not(A) or B
A= not Ref::Util::XS::is_ref($exception)B= Ref::Util::XS::is_arrayref($exception)
| |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | $exception = [$exception] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | unless is_arrayref( $exception ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | $wsub = sub { | ||||||||||||||||||||||||||||
| 57 | 36 | ▶ | my $self = $_[0] // $wsub; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
Condition33%1/3
$_[0] // $wsub
MC/DC0%0/2
$_[0] // $wsub
$_[0]
$wsub
Truth table33%1/3
A // B
A= $_[0]B= $wsub
| |||||||||||||||||||||||||||||
| 58 | 36 | my $val = eval { $next->( $self ) }; | |||||||||||||||||||||||||||
| 59 | 36 | ▶ | if ( $@ ne q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 4 | my $e = $@; | |||||||||||||||||||||||||||
| 61 | return $self->signal_exhaustion( $e ) | ||||||||||||||||||||||||||||
| 62 | 4 | ▶ | if is_blessed_ref( $e ) && first { $e->isa( $_ ) } @$exception; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
Ref::Util::XS::is_blessed_ref($e) and &first(sub {
$e->isa($_);
}
, @$exception)
MC/DC50%1/2
Ref::Util::XS::is_blessed_ref($e) and &first(sub {
$e->isa($_);
}
, @$exception)
Ref::Util::XS::is_blessed_ref($e)
&first(sub {
$e->isa($_);
}
, @$exception)
Truth table66%2/3
A and B
A= Ref::Util::XS::is_blessed_ref($e)B= &first(sub {
$e->isa($_);
}
, @$exception)
| |||||||||||||||||||||||||||||
| 63 | 2 | die $e; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 32 | return $val; | |||||||||||||||||||||||||||
| 66 | 4 | }; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | elsif ( is_regexpref( $exception ) ) { | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | $wsub = sub { | ||||||||||||||||||||||||||||
| 72 | 36 | ▶ | my $self = $_[0] // $wsub; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
Condition33%1/3
$_[0] // $wsub
MC/DC0%0/2
$_[0] // $wsub
$_[0]
$wsub
Truth table33%1/3
A // B
A= $_[0]B= $wsub
| |||||||||||||||||||||||||||||
| 73 | 36 | my $val = eval { $next->( $self ) }; | |||||||||||||||||||||||||||
| 74 | 36 | ▶ | if ( $@ ne q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 4 | my $e = $@; | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | return $self->signal_exhaustion( $e ) if $e =~ $exception; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 2 | die $e; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 32 | return $val; | |||||||||||||||||||||||||||
| 80 | 4 | }; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | elsif ( is_coderef( $exception ) ) { | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | $wsub = sub { | ||||||||||||||||||||||||||||
| 86 | 36 | ▶ | my $self = $_[0] // $wsub; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
Condition33%1/3
$_[0] // $wsub
MC/DC0%0/2
$_[0] // $wsub
$_[0]
$wsub
Truth table33%1/3
A // B
A= $_[0]B= $wsub
| |||||||||||||||||||||||||||||
| 87 | 36 | my $val = eval { $next->( $self ) }; | |||||||||||||||||||||||||||
| 88 | 36 | ▶ | if ( $@ ne q{} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 4 | my $e = $@; | |||||||||||||||||||||||||||
| 90 | 4 | ▶ | return $self->signal_exhaustion( $e ) if $exception->( $e ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 2 | die $e; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 32 | return $val; | |||||||||||||||||||||||||||
| 94 | 4 | }; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 0 | require Iterator::Flex::Failure; | |||||||||||||||||||||||||||
| 99 | 0 | require Scalar::Util; | |||||||||||||||||||||||||||
| 100 | 0 | Iterator::Flex::Failure::parameter->throw( | |||||||||||||||||||||||||||
| 101 | 0 | "internal error: unknown type for input exhaustion policy: ${ \Scalar::Util::reftype( $exception ) }", | |||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # create a second reference to $wsub, before we weaken it, | ||||||||||||||||||||||||||||
| 108 | # otherwise it will lose its contents, as it would be the only | ||||||||||||||||||||||||||||
| 109 | # reference. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 18 | my $sub = $wsub; | |||||||||||||||||||||||||||
| 112 | 18 | Scalar::Util::weaken( $wsub ); | |||||||||||||||||||||||||||
| 113 | 18 | return $sub; | |||||||||||||||||||||||||||
| 114 | 4 | }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 4 | requires 'signal_exhaustion'; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 4 | 1; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # | ||||||||||||||||||||||||||||
| 121 | # This file is part of Iterator-Flex | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory. | ||||||||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||||||||
| 125 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | # The GNU General Public License, Version 3, June 2007 | ||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||