File Coverage

blib/lib/Evo/Internal/Util.pm
Criterion Covered Total %
statement 116 124 93.5
branch 31 56 55.3
condition 0 2 0.0
subroutine 23 24 95.8
pod 0 10 0.0
total 170 216 78.7


line stmt bran cond sub pod time code
1             package Evo::Internal::Util;
2 67     67   468 use strict;
  67         138  
  67         1795  
3 67     67   337 use warnings;
  67         477  
  67         2374  
4 67     67   30236 use experimental 'signatures';
  67         219614  
  67         413  
5 67     67   10003 no warnings 'experimental::signatures';
  67         174  
  67         2101  
6 67     67   406 use Carp qw(carp croak);
  67         147  
  67         3730  
7 67     67   446 use B qw(svref_2object);
  67         166  
  67         9560  
8              
9             our $RX_PKG_NOT_FIRST = qr/[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*/;
10             our $RX_PKG = qr/^[A-Z_a-z]$RX_PKG_NOT_FIRST*$/;
11              
12              
13 67     67   502 use constant SUBRE => qr/^[a-zA-Z_]\w*$/;
  67         159  
  67         15735  
14 728     728 0 4975 sub check_subname { $_[0] =~ SUBRE }
15              
16             # usefull?
17 0 0   0 0 0 sub find_caller_except ($skip_ns, $i, $caller) {
  0 0       0  
  0         0  
  0         0  
  0         0  
  0         0  
18 0         0 while ($caller = (caller($i++))[0]) {
19 0 0       0 return $caller if $caller ne $skip_ns;
20             }
21             }
22              
23 2168 50   2168 0 6196 sub monkey_patch ($pkg, %hash) {
  2168 50       5581  
  2168         3788  
  2168         8406  
  2168         3935  
24 67     67   519 no strict 'refs'; ## no critic
  67         177  
  67         8113  
25 2168         9017 *{"${pkg}::$_"} = $hash{$_} for keys %hash;
  5754         90788  
26             }
27              
28             #todo: decide what to do with empty subroutins
29 9 50   9 0 2576 sub monkey_patch_silent ($pkg, %hash) {
  9 50       29  
  9         14  
  9         31  
  9         17  
30 67     67   469 no strict 'refs'; ## no critic
  67         157  
  67         2928  
31 67     67   392 no warnings 'redefine';
  67         168  
  67         17185  
32 9         14 my %restore;
33 9         25 foreach my $name (keys %hash) {
34 10         24 $restore{$name} = *{"${pkg}::$name"}{CODE};
  10         44  
35 10 50 0     29 warn "Can't delete empty ${pkg}::$name" and next unless $hash{$name};
36 10         18 *{"${pkg}::$name"} = $hash{$name};
  10         55  
37             }
38 9         41 \%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 2807 50   2807 0 9888 sub code2names($r) {
  2807 50       6853  
  2807         4955  
  2807         4294  
46 2807         8023 my $sv = svref_2object($r);
47 2807         8406 my $gv = $sv->GV;
48 2807         7411 my $stash = $gv->STASH;
49 2807         16066 ($stash->NAME, $gv->NAME, $sv->CONST);
50             }
51              
52 3903 50   3903 0 11218 sub names2code ($pkg, $name) {
  3903 50       7918  
  3903         5784  
  3903         5719  
  3903         5317  
53 67     67   596 no strict 'refs'; ## no critic
  67         149  
  67         6279  
54 3903         5268 *{"${pkg}::$name"}{CODE};
  3903         19029  
55             }
56              
57              
58 62 50   62 0 1136 sub list_symbols($pkg) {
  62 50       192  
  62         124  
  62         119  
59 67     67   449 no strict 'refs'; ##no critic
  67         150  
  67         32086  
60 62         118 grep { $_ =~ /^[a-zA-Z_]\w*$/ } keys %{"${pkg}::"};
  1658         4576  
  62         517  
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 1979 50   1979 0 5825 sub inject(%opts) {
  1979         8231  
  1979         3241  
76 1979         5975 my ($package, $filename, $line, $code) = @opts{qw(package filename line code)};
77              
78             ## no critic
79             (
80 1979         127341 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 140 50   140   533 sub _parent ($caller) {
  140 50       721  
  140         317  
  140         260  
97 140         589 my @arr = split /::/, $caller;
98 140         313 pop @arr;
99 140         772 join '::', @arr;
100             }
101              
102 2698 50   2698 0 7673 sub resolve_package ($caller, $pkg) {
  2698 50       6369  
  2698         4887  
  2698         5688  
  2698         4296  
103              
104 2698 100       18506 return $pkg if $pkg =~ $RX_PKG;
105              
106 1780 100       16330 return "Evo::$1" if $pkg =~ /^\-($RX_PKG_NOT_FIRST)$/;
107              
108             # parent. TODO: many //
109 251 100       1146 if ($pkg =~ /^\/(.*)$/) {
110 140         403 my $rest = $1;
111 140 100       552 my $parent = _parent($caller)
112             or croak "Can't resolve $pkg: can't find parent of caller $caller";
113              
114 139 50       3092 return "$parent$rest" if "$parent$rest" =~ /^$RX_PKG$/;
115             }
116              
117 111 100       2825 return "${caller}::$1" if $pkg =~ /^::($RX_PKG_NOT_FIRST)$/;
118              
119 4         351 croak "Can't resolve $pkg for caller $caller";
120             }
121              
122              
123 3 50   3 0 2840 sub suppress_carp ($me, $caller) {
  3 50       8  
  3         5  
  3         5  
  3         5  
124 67     67   503 no strict 'refs'; ## no critic
  67         166  
  67         6260  
125 3 100       4 push @{"${caller}::CARP_NOT"}, $me if !grep { $_ eq $me } @{"${caller}::CARP_NOT"};
  2         7  
  3         10  
  3         14  
126             }
127              
128             1;
129              
130             __END__