Evo/Internal/Util.pm

filter: Statementstmt 69.1% 56 / 81 Branchbran 45.0% 9 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 70.8% 17 / 24 Podpod 0.0% 0 / 10 totaltotal 57.7% 82 / 142 SCAR 32.3
CC
14
Cov
61%
find_caller_except
12.0
monkey_patch_silent
9.8
resolve_package
7.4
CRAP 25.3
« Evo/Lib/PP.pm ↑ Summary Evo.pm »
1 package Evo::Internal::Util;
2 67 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 67 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 67 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 67 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 67 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 67 ▶use B qw(svref_2object);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 67 our $RX_PKG_NOT_FIRST = qr/[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*/;
10 67 our $RX_PKG = qr/^[A-Z_a-z]$RX_PKG_NOT_FIRST*$/;
11  
12  
13 67 ▶use constant SUBRE => qr/^[a-zA-Z_]\w*$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶sub check_subname { $_[0] =~ SUBRE }
Subroutine check_subname: not called CC=1 SCAR=6.9
Pod undocumented
15  
16 # usefull?
17 ▶sub find_caller_except ($skip_ns, $i, $caller) {
Subroutine find_caller_except: not called CC=3 SCAR=24.8
Pod undocumented
18 0 ▶ while ($caller = (caller($i++))[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller = (caller $i++)[0]) 0 0 0
19 0 ▶ return $caller if $caller ne $skip_ns;
Branch: 1 branch
BranchTrueFalseTotal
if $caller ne $skip_ns 0 0 0
20 }
21 }
22  
23 ▶sub monkey_patch ($pkg, %hash) {
Subroutine monkey_patch: called CC=2 SCAR=6.9
Pod undocumented
24 67 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
25 258 *{"${pkg}::$_"} = $hash{$_} for keys %hash;
26 }
27  
28 #todo: decide what to do with empty subroutins
29 ▶sub monkey_patch_silent ($pkg, %hash) {
Subroutine monkey_patch_silent: not called CC=4 SCAR=22.9
Pod undocumented
30 67 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
31 67 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
32 0 my %restore;
33 0 foreach my $name (keys %hash) {
34 0 $restore{$name} = *{"${pkg}::$name"}{CODE};
35 0 ▶ warn "Can't delete empty ${pkg}::$name" and next unless $hash{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{$name} 0 0 0
Condition0%0/5
$hash{$name} || warn("Can't delete empty ${pkg}::$name") && (next)
l!l&&r!l&&!r
000
warn "Can't delete empty ${pkg}::$name" and next
!ll
00
MC/DC0%0/2
$hash{$name} || warn("Can't delete empty ${pkg}::$name") && (next)
$hash{$name} warn "Can't delete empty ${pkg}::$name"
Truth table0%0/3
A || B
A= $hash{$name}B= warn "Can't delete empty ${pkg}::$name"
ABresult
1X1
011
000
36 0 *{"${pkg}::$name"} = $hash{$name};
37 }
38 0 \%restore;
39 }
40  
41 # returns a package where code was declared and a name
42 # ->CONST not documented, but exists in B::CV and defined in /CORE/cv.h as
43 # define CvCONST(cv) (CvFLAGS(cv) & CVf_CONST)
44 # this flag is used by Evo::Class to determine that constants should be inherited
45 ▶sub code2names($r) {
Subroutine code2names: not called CC=1 SCAR=6.9
Pod undocumented
46 0 my $sv = svref_2object($r);
47 0 my $gv = $sv->GV;
48 0 my $stash = $gv->STASH;
49 0 ($stash->NAME, $gv->NAME, $sv->CONST);
50 }
51  
52 ▶sub names2code ($pkg, $name) {
Subroutine names2code: not called CC=1 SCAR=0.6
Pod undocumented
53 67 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
54 0 *{"${pkg}::$name"}{CODE};
55 }
56  
57  
58 ▶sub list_symbols($pkg) {
Subroutine list_symbols: not called CC=1 SCAR=1.2
Pod undocumented
59 67 ▶ no strict 'refs'; ##no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
60 0 grep { $_ =~ /^[a-zA-Z_]\w*$/ } keys %{"${pkg}::"};
61 }
62  
63 #sub undef_symbols($ns) {
64 # no strict 'refs'; ## no critic
65 # undef *{"${ns}::$_"} for list_symbols($ns);
66 #}
67  
68  
69 #sub uniq {
70 # my %seen;
71 # return grep { !$seen{$_}++ } @_;
72 #}
73  
74 # returns a subroutine than can pretend a code in the other package/file/line
75 ▶sub inject(%opts) {
Subroutine inject: called CC=1 SCAR=0.0
Pod undocumented
76 491 my ($package, $filename, $line, $code) = @opts{qw(package filename line code)};
77  
78 ## no critic
79 (
80 491 eval qq{package $package;
81 #line $line "$filename"
82 sub { \$code->(\@_) }
83 }
84 );
85 }
86  
87 #sub find_subnames ($pkg, $code) {
88 # no strict 'refs'; ## no critic
89 # my %symbols = %{$pkg . "::"};
90 #
91 # # because use constant adds refs to package symbols hash
92 # grep { !ref($symbols{$_}) && (*{$symbols{$_}}{CODE} // 0) == $code } keys %symbols;
93 #}
94  
95  
96 ▶sub _parent ($caller) {
Subroutine _parent: called CC=1 SCAR=0.0
97 1 my @arr = split /::/, $caller;
98 1 pop @arr;
99 1 join '::', @arr;
100 }
101  
102 ▶sub resolve_package ($caller, $pkg) {
Subroutine resolve_package: called CC=7 SCAR=20.0
Pod undocumented
103  
104 595 ▶ return $pkg if $pkg =~ $RX_PKG;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /$Evo::Internal::Util::RX_PKG/ 193 402 595
105  
106 402 ▶ return "Evo::$1" if $pkg =~ /^\-($RX_PKG_NOT_FIRST)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /^\-($Evo::Internal::Util::RX_PKG_NOT_FIRST)$/ 359 43 402
107  
108 # parent. TODO: many //
109 43 ▶ if ($pkg =~ /^\/(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg =~ m[^/(.*)$]) 1 42 43
110 1 my $rest = $1;
111 1 ▶ my $parent = _parent($caller)
Branch: 1 branch
BranchTrueFalseTotal
my $parent = _parent($caller) or croak("Can't resolve ${pkg}: can't find parent of caller $caller") 0 1 1
112 or croak "Can't resolve $pkg: can't find parent of caller $caller";
113  
114 1 ▶ return "$parent$rest" if "$parent$rest" =~ /^$RX_PKG$/;
Branch: 1 branch
BranchTrueFalseTotal
if "$parent$rest" =~ /^$Evo::Internal::Util::RX_PKG$/ 1 0 1
115 }
116  
117 42 ▶ return "${caller}::$1" if $pkg =~ /^::($RX_PKG_NOT_FIRST)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /^::($Evo::Internal::Util::RX_PKG_NOT_FIRST)$/ 42 0 42
118  
119 0 croak "Can't resolve $pkg for caller $caller";
120 }
121  
122  
123 ▶sub suppress_carp ($me, $caller) {
Subroutine suppress_carp: not called CC=2 SCAR=11.6
Pod undocumented
124 67 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
125 0 ▶ push @{"${caller}::CARP_NOT"}, $me if !grep { $_ eq $me } @{"${caller}::CARP_NOT"};
Branch: 1 branch
BranchTrueFalseTotal
if not grep({$_ eq $me;} @{"${caller}::CARP_NOT";}) 0 0 0
126 }
127  
128 67 1;
129  
130 __END__
« Evo/Lib/PP.pm ↑ Summary Evo.pm »