| 1 | package OpenTracing::WrapScope; | ||||||||||||||||||||||||||||
| 2 | 19 | our $VERSION = 'v0.107.4'; | |||||||||||||||||||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 19 | ▶ | use warnings::register; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use feature qw[ state ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use B::Hooks::OP::Check::LeaveEval; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use Caller::Hide qw/hide_package/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 19 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 19 | ▶ | use List::Util qw/uniq/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 19 | ▶ | use OpenTracing::GlobalTracer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 19 | ▶ | use PerlX::Maybe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 19 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 19 | ▶ | use Sub::Info qw/sub_info/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 19 | hide_package(__PACKAGE__); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 19 | my %subs_to_install; | |||||||||||||||||||||||||||
| 19 | 19 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 20 | 19 | foreach my $sub (keys %subs_to_install) { | |||||||||||||||||||||||||||
| 21 | 4 | ▶ | next unless $subs_to_install{$sub}{warn_undetected}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 2 | warnings::warn "OpenTracing::WrapScope couldn't find sub: $sub"; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 48 | ▶ | sub _register_to_install { | ||||||||||||||||||||||||||
|
Subroutine
_register_to_install: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 48 | my ($signature, %args) = @_; | |||||||||||||||||||||||||||
| 28 | 48 | my $warn_undetected = $args{warn_undetected}; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 48 | my ($sub) = _split_signature($signature); | |||||||||||||||||||||||||||
| 31 | 48 | $subs_to_install{$sub} = {%args, signature => $signature }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 48 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 99 | ▶ | sub _split_signature { | ||||||||||||||||||||||||||
|
Subroutine
_split_signature: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 99 | my ($sig) = @_; | |||||||||||||||||||||||||||
| 38 | 99 | ▶ | return $sig unless $sig =~ s/\s*\((.*)\)\s*\z//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 3 | return ($sig, $1); # just the name left over | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # try to install any available subs whenever we get new code | ||||||||||||||||||||||||||||
| 43 | 1468 | ▶ | B::Hooks::OP::Check::LeaveEval::register(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 44 | 1468 | ▶ | return unless %subs_to_install; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 280 | foreach my $sub (keys %subs_to_install) { | |||||||||||||||||||||||||||
| 47 | 511 | ▶ | next unless defined &$sub; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 44 | install_wrapped($subs_to_install{$sub}{signature}); | |||||||||||||||||||||||||||
| 49 | 44 | delete $subs_to_install{$sub}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 280 | return; | |||||||||||||||||||||||||||
| 52 | 19 | }); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 24 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 55 | 24 | shift; # __PACKAGE__ | |||||||||||||||||||||||||||
| 56 | 24 | my $target_package = caller; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 24 | my $warn_undetected = 1; | |||||||||||||||||||||||||||
| 59 | 24 | my ($use_env, @subs, @files); | |||||||||||||||||||||||||||
| 60 | 24 | ▶ | while (my (undef, $arg) = each @_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 40 | ▶ | if ($arg eq '-env') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 62 | 2 | $use_env = 1; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ($arg eq '-file') { | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | my (undef, $next) = each @_ or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | push @files, ref $next eq 'ARRAY' ? @$next : $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | elsif ($arg eq '-quiet') { | ||||||||||||||||||||||||||||
| 69 | 2 | $warn_undetected = 0; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||
| 72 | 34 | push @subs, _qualify_sub($arg, $target_package); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 24 | ▶ | if ($use_env and $ENV{OPENTRACING_WRAPSCOPE_FILE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
MC/DC50%1/2
$use_env and $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
$use_env
$ENV{"OPENTRACING_WRAPSCOPE_FILE"}
Truth table66%2/3
A and B
A= $use_envB= $ENV{"OPENTRACING_WRAPSCOPE_FILE"}
| |||||||||||||||||||||||||||||
| 76 | 2 | push @files, split ':', $ENV{OPENTRACING_WRAPSCOPE_FILE}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 19 | ▶ | push @subs, map { _load_sub_spec($_) } grep { -f } map { glob } uniq @files; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 24 | foreach my $sub (@subs) { | |||||||||||||||||||||||||||
| 81 | 48 | _register_to_install($sub, warn_undetected => $warn_undetected); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 24 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 52 | ▶ | sub install_wrapped { | ||||||||||||||||||||||||||
|
Subroutine
install_wrapped: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 52 | foreach my $sub (@_) { | |||||||||||||||||||||||||||
| 89 | 51 | my ($sub_name, $args) = _split_signature($sub); | |||||||||||||||||||||||||||
| 90 | 51 | my $full_sub = _qualify_sub($sub_name, scalar caller); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 51 | ▶ | if (not defined &$full_sub) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | warnings::warn "Couldn't find sub: $full_sub"; | |||||||||||||||||||||||||||
| 94 | 1 | next; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 49 | my $wrapped = wrapped(\&$full_sub, $args); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 49 | my ($class, $method) = split /(?:'|::)(?=\w+\z)/, $full_sub; | |||||||||||||||||||||||||||
| 100 | 49 | ▶ | if (_is_moose_class($class)) { # Moose complains about replaced subs | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | if ($class->meta->is_immutable) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | warnings::warn "Can't wrap Moose method $sub from an immutable class"; | |||||||||||||||||||||||||||
| 103 | 1 | next; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 4 | $class->meta->add_method($method => $wrapped); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | else { | ||||||||||||||||||||||||||||
| 108 | 19 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 19 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | 44 | *$full_sub = $wrapped; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 51 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 49 | ▶ | sub _is_moose_class { | ||||||||||||||||||||||||||
|
Subroutine
_is_moose_class: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||
| 117 | 49 | my ($class) = @_; | |||||||||||||||||||||||||||
| 118 | 49 | ▶ | my $meta = eval { $class->meta } or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 5 | ▶ | return blessed $meta && $meta->isa('Moose::Meta::Class'); | ||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($meta) && $meta->isa('Moose::Meta::Class')
MC/DC0%0/2
builtin::blessed($meta) && $meta->isa('Moose::Meta::Class')
builtin::blessed($meta)
$meta->isa('Moose::Meta::Class')
Truth table33%1/3
A && B
A= builtin::blessed($meta)B= $meta->isa('Moose::Meta::Class')
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 76 | ▶ | sub wrapped { | ||||||||||||||||||||||||||
|
Subroutine
wrapped: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 76 | my ($coderef, $signature) = @_; | |||||||||||||||||||||||||||
| 124 | 76 | my $info = sub_info($coderef); | |||||||||||||||||||||||||||
| 125 | 76 | my @tag_generators = _parse_signature($signature); | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 78 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.9
Subroutine
inside: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||
| 128 | 78 | my ($call_package, $call_filename, $call_line) = caller(0); | |||||||||||||||||||||||||||
| 129 | 78 | my $call_sub = (caller(1))[3]; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 78 | my $tracer = OpenTracing::GlobalTracer->get_global_tracer; | |||||||||||||||||||||||||||
| 132 | my $scope = $tracer->start_active_span( | ||||||||||||||||||||||||||||
| 133 | "$info->{package}::$info->{name}", | ||||||||||||||||||||||||||||
| 134 | tags => { | ||||||||||||||||||||||||||||
| 135 | 'source.subname' => $info->{name}, | ||||||||||||||||||||||||||||
| 136 | 'source.file' => $info->{file}, | ||||||||||||||||||||||||||||
| 137 | 'source.line' => $info->{start_line}, | ||||||||||||||||||||||||||||
| 138 | 'source.package' => $info->{package}, | ||||||||||||||||||||||||||||
| 139 | maybe | ||||||||||||||||||||||||||||
| 140 | 'caller.subname' => $call_sub, | ||||||||||||||||||||||||||||
| 141 | 'caller.file' => $call_filename, | ||||||||||||||||||||||||||||
| 142 | 'caller.line' => $call_line, | ||||||||||||||||||||||||||||
| 143 | 'caller.package' => $call_package, | ||||||||||||||||||||||||||||
| 144 | 0 | map { $_->(@_) } @tag_generators, | |||||||||||||||||||||||||||
| 145 | }, | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 78 | my $result; | |||||||||||||||||||||||||||
| 149 | 78 | my $wantarray = wantarray; # eval will have its own | |||||||||||||||||||||||||||
| 150 | 78 | my $ok = eval { | |||||||||||||||||||||||||||
| 151 | 78 | ▶ | if (defined $wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $result = $wantarray ? [&$coderef] : &$coderef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||
| 155 | 74 | &$coderef; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 77 | 1; | |||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||
| 159 | 78 | my $err = $@; | |||||||||||||||||||||||||||
| 160 | # TODO: message should go to logs but we don't have those yet | ||||||||||||||||||||||||||||
| 161 | 78 | ▶ | $scope->get_span->add_tags(error => 1, message => "$err") | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 162 | if not $ok; | ||||||||||||||||||||||||||||
| 163 | 78 | $scope->close(); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 78 | ▶ | die $err | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 166 | if not $ok; | ||||||||||||||||||||||||||||
| 167 | return | ||||||||||||||||||||||||||||
| 168 | 77 | ▶ | unless defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return wantarray ? @$result : $result; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 170 | 69 | }; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 106 | ▶ | sub _is_qualified { $_[0] =~ /\A\w+(?:'|::)/ } | ||||||||||||||||||||||||||
|
Subroutine
_is_qualified: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 85 | ▶ | sub _qualify_sub { | ||||||||||||||||||||||||||
|
Subroutine
_qualify_sub: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 176 | 85 | my ($sub, $pkg) = @_; | |||||||||||||||||||||||||||
| 177 | 85 | ▶ | return $sub if _is_qualified($sub); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 27 | $sub =~ s/\A(\w+)/${pkg}::$1/; | |||||||||||||||||||||||||||
| 179 | 27 | return $sub; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 15 | ▶ | sub _load_sub_spec { | ||||||||||||||||||||||||||
|
Subroutine
_load_sub_spec: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 183 | 15 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 15 | ▶ | open my $fh_subs, '<', $filename or die "$filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 15 | my @subs; | |||||||||||||||||||||||||||
| 188 | 15 | ▶ | while (<$fh_subs>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 25 | chomp; | |||||||||||||||||||||||||||
| 190 | 25 | s/\s*#.*\Z//; # remove comments | |||||||||||||||||||||||||||
| 191 | 25 | ▶ | next unless $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 21 | ▶ | croak "Unqualified subroutine: $_" unless _is_qualified($_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 18 | push @subs, $_; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | 12 | close $fh_subs; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 12 | return @subs; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 8 | ▶ | sub wrap_from_file { | ||||||||||||||||||||||||||
|
Subroutine
wrap_from_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 202 | 8 | my ($filename) = @_; | |||||||||||||||||||||||||||
| 203 | 8 | install_wrapped( _load_sub_spec($filename) ); | |||||||||||||||||||||||||||
| 204 | 5 | return; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||
| 209 | 19 | my $gr = qr{ | |||||||||||||||||||||||||||
| 210 | (?(DEFINE) | ||||||||||||||||||||||||||||
| 211 | (?<COMMA> \s* , \s* ) | ||||||||||||||||||||||||||||
| 212 | (?<INT> [0-9]+ ) | ||||||||||||||||||||||||||||
| 213 | (?<RANGE> (?&INT) \s* \.\. \s* (?&INT) ) | ||||||||||||||||||||||||||||
| 214 | (?<AS_ARG> (?&RANGE) | (?&INT) ) | ||||||||||||||||||||||||||||
| 215 | (?<HS_ARG> '[^']*' | "[^"]*" ) | ||||||||||||||||||||||||||||
| 216 | (?<ARRAY_SLICE> \s* (?&AS_ARG) (?: (?&COMMA) (?&AS_ARG) )* \s* ) | ||||||||||||||||||||||||||||
| 217 | (?<HASH_SLICE> \s* (?&HS_ARG) (?: (?&COMMA) (?&HS_ARG) )* \s* ) | ||||||||||||||||||||||||||||
| 218 | ) | ||||||||||||||||||||||||||||
| 219 | }x; | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 76 | ▶ | sub _parse_signature { | ||||||||||||||||||||||||||
|
Subroutine
_parse_signature: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 222 | 76 | my ($sig) = @_; | |||||||||||||||||||||||||||
| 223 | 76 | ▶ | return unless $sig; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 29 | state $types = { | |||||||||||||||||||||||||||
| 226 | '$' => { greedy => 0, generator => \&_gen_scalar }, | ||||||||||||||||||||||||||||
| 227 | '%' => { greedy => 1, generator => \&_gen_hash }, | ||||||||||||||||||||||||||||
| 228 | '@' => { greedy => 1, generator => \&_gen_array }, | ||||||||||||||||||||||||||||
| 229 | '\%' => { greedy => 0, generator => \&_gen_hash_ref }, | ||||||||||||||||||||||||||||
| 230 | '\@' => { greedy => 0, generator => \&_gen_array_ref }, | ||||||||||||||||||||||||||||
| 231 | }; | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 29 | state $re = qr{ | |||||||||||||||||||||||||||
| 234 | (?: \A \s* | (?&COMMA) ) | ||||||||||||||||||||||||||||
| 235 | (?: | ||||||||||||||||||||||||||||
| 236 | (?<type> undef ) | ||||||||||||||||||||||||||||
| 237 | | (?<type> \$ ) (?<name> \w+ ) | ||||||||||||||||||||||||||||
| 238 | | (?<type> (?:\\)? % ) (?<name> \w+ ) (?: \{ (?<slice> (?&HASH_SLICE) ) \} )? | ||||||||||||||||||||||||||||
| 239 | | (?<type> (?:\\)? @ ) (?<name> \w+ ) (?: \[ (?<slice> (?&ARRAY_SLICE) ) \] )? | ||||||||||||||||||||||||||||
| 240 | ) | ||||||||||||||||||||||||||||
| 241 | $gr | ||||||||||||||||||||||||||||
| 242 | }x; | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 29 | my @tag_generators; | |||||||||||||||||||||||||||
| 245 | 29 | my $arg_idx = 0; | |||||||||||||||||||||||||||
| 246 | 29 | ▶ | while ($sig =~ /\G$re/xcg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 44 | my ($type, $name, $slice) = @+{qw[ type name slice ]}; | |||||||||||||||||||||||||||
| 248 | 44 | ▶ | next if $type eq 'undef'; # hidden argument | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 43 | my $slice_ref = _parse_slice($type, $slice); | |||||||||||||||||||||||||||
| 251 | 43 | ▶ | my $type_ref = $types->{$type} or die "No such type: $type"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 43 | my $generator = $type_ref->{generator}->($name, $arg_idx, $slice_ref); | |||||||||||||||||||||||||||
| 253 | 43 | push @tag_generators, $generator; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 43 | ▶ | last if $type_ref->{greedy}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | continue { | ||||||||||||||||||||||||||||
| 258 | 31 | ++$arg_idx; | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 29 | ▶ | my $pos = pos($sig) // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
pos $sig // 0
MC/DC100%1/1
pos $sig // 0
pos $sig
Truth table100%2/2
A
A= pos $sig
| |||||||||||||||||||||||||||||
| 262 | 29 | ▶ | if ($pos != length($sig)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 7 | Carp::croak "Failed to parse signature:\n$sig\n", ' ' x $pos, '^'; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 22 | return @tag_generators; | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | 43 | ▶ | sub _parse_slice { | ||||||||||||||||||||||||||
|
Subroutine
_parse_slice: called CC=10 SCAR=23.2
| |||||||||||||||||||||||||||||
| 270 | 43 | my ($type, $slice) = @_; | |||||||||||||||||||||||||||
| 271 | 43 | ▶ | return unless $type and $slice; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type and $slice
MC/DC50%1/2
$type and $slice
$type
$slice
Truth table66%2/3
A and B
A= $typeB= $slice
| |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 10 | ▶ | if ($type eq '@' or $type eq '\@') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type eq "\@" or $type eq "\\\@"
MC/DC100%2/2
$type eq "\@" or $type eq "\\\@"
$type eq "\@"
$type eq "\\\@"
Truth table100%3/3
A or B
A= $type eq "\@"B= $type eq "\\\@"
| |||||||||||||||||||||||||||||
| 274 | 4 | my @args; | |||||||||||||||||||||||||||
| 275 | 4 | ▶ | while ($slice =~ /((?&AS_ARG)) $gr/gx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | 8 | my $arg = $1; | |||||||||||||||||||||||||||
| 277 | 8 | ▶ | push @args, $arg =~ /(\d+)\s*\.\.\s(\d+)/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | ? $1 .. $2 # expand ranges | ||||||||||||||||||||||||||||
| 279 | : $arg; | ||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 4 | return \@args; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 6 | ▶ | if ($type eq '%' or $type eq '\%') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$type eq "%" or $type eq "\\%"
MC/DC0%0/2
$type eq "%" or $type eq "\\%"
$type eq "%"
$type eq "\\%"
Truth table66%2/3
A or B
A= $type eq "%"B= $type eq "\\%"
| |||||||||||||||||||||||||||||
| 285 | 6 | my @args; | |||||||||||||||||||||||||||
| 286 | 6 | ▶ | while ($slice =~ /((?&HS_ARG)) $gr/gx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 12 | push @args, $1 =~ s/\A['"]|['"]\z//gr; # remove quotes | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 6 | return \@args; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 0 | return; | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # undefs and references break OpenTracing::Interface type constraints | ||||||||||||||||||||||||||||
| 297 | 66 | ▶ | sub _str { defined $_[0] ? "$_[0]" : 'undef' } | ||||||||||||||||||||||||||
|
Subroutine
_str: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 20 | ▶ | sub _gen_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_gen_scalar: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 300 | 20 | my ($name, $arg_idx) = @_; | |||||||||||||||||||||||||||
| 301 | 22 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 302 | 22 | ▶ | return if $arg_idx > $#_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 21 | return ("arguments.$name" => _str($_[$arg_idx])); | |||||||||||||||||||||||||||
| 304 | 20 | }; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 7 | ▶ | sub _gen_hash { | ||||||||||||||||||||||||||
|
Subroutine
_gen_hash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 308 | 7 | my ($name, $arg_idx, $slice_ref) = @_; | |||||||||||||||||||||||||||
| 309 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 310 | 7 | ▶ | return if $arg_idx > $#_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 19 | ▶ | no warnings 'misc'; # odd-sized list, etc. these are not our fault | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 313 | 6 | my %hash = @_[ $arg_idx .. $#_ ]; | |||||||||||||||||||||||||||
| 314 | 6 | ▶ | return map {; "arguments.$name.$_" => _str($hash{$_}) } keys %hash unless $slice_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 3 | @$slice_ref = grep { exists $hash{$_} } @$slice_ref; | |||||||||||||||||||||||||||
| 317 | 3 | my %sliced; | |||||||||||||||||||||||||||
| 318 | 3 | @sliced{@$slice_ref} = @hash{@$slice_ref}; | |||||||||||||||||||||||||||
| 319 | 3 | return map { ; "arguments.$name.$_" => _str($sliced{$_}) } keys %sliced; | |||||||||||||||||||||||||||
| 320 | 7 | }; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 6 | ▶ | sub _gen_array { | ||||||||||||||||||||||||||
|
Subroutine
_gen_array: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 324 | 6 | my ($name, $arg_idx, $slice_ref) = @_; | |||||||||||||||||||||||||||
| 325 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 326 | 7 | ▶ | return if $arg_idx > $#_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 6 | my @args = @_[ $arg_idx .. $#_ ]; | |||||||||||||||||||||||||||
| 329 | return | ||||||||||||||||||||||||||||
| 330 | 16 | ▶ | map {; "arguments.$name.$_" => _str($args[$_]) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 6 | $slice_ref ? grep { $_ <= $#args } @$slice_ref : keys @args; | |||||||||||||||||||||||||||
| 332 | 6 | }; | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 5 | ▶ | sub _gen_hash_ref { | ||||||||||||||||||||||||||
|
Subroutine
_gen_hash_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 336 | 5 | my ($name, $arg_idx, $slice_ref) = @_; | |||||||||||||||||||||||||||
| 337 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 338 | 7 | ▶ | return if $arg_idx > $#_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 6 | ▶ | return if ref $_[$arg_idx] ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 19 | ▶ | no warnings 'misc'; # odd-sized list, etc. these are not our fault | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 342 | 4 | my %hash = %{ $_[$arg_idx] }; | |||||||||||||||||||||||||||
| 343 | 4 | ▶ | return map {; "arguments.$name.$_" => _str($hash{$_}) } keys %hash unless $slice_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 3 | @$slice_ref = grep { exists $hash{$_} } @$slice_ref; | |||||||||||||||||||||||||||
| 346 | 3 | my %sliced; | |||||||||||||||||||||||||||
| 347 | 3 | @sliced{@$slice_ref} = @hash{@$slice_ref}; | |||||||||||||||||||||||||||
| 348 | 3 | return map {; "arguments.$name.$_" => _str($sliced{$_}) } keys %sliced; | |||||||||||||||||||||||||||
| 349 | 5 | }; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 5 | ▶ | sub _gen_array_ref { | ||||||||||||||||||||||||||
|
Subroutine
_gen_array_ref: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 353 | 5 | my ($name, $arg_idx, $slice_ref) = @_; | |||||||||||||||||||||||||||
| 354 | 9 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 355 | 9 | ▶ | return if $arg_idx > $#_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 7 | ▶ | return if ref $_[$arg_idx] ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 4 | my @args = @{ $_[$arg_idx] }; | |||||||||||||||||||||||||||
| 359 | return | ||||||||||||||||||||||||||||
| 360 | 13 | ▶ | map {; "arguments.$name.$_" => _str($args[$_]) } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 4 | $slice_ref ? grep { $_ <= $#args } @$slice_ref : keys @args; | |||||||||||||||||||||||||||
| 362 | 5 | }; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 19 | 1; | |||||||||||||||||||||||||||