lib/Perlmazing/Engine.pm

filter: Statementstmt 74.5% 126 / 169 Branchbran 48.2% 27 / 56 Conditioncond 51.4% 18 / 35 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 79.1% 19 / 24 Podpod 100% 4 / 4 totaltotal 65.0% 199 / 306 CC 49 SCAR 34.3
CC
49
Cov
66%
__ANON__
90.0
_load_symbol
57.3
loaded_symbols
12.0
CRAP 30.9
« lib/Perlmazing/Perlmazing/Precompile/sort_by_value.pm ↑ Summary blib/lib/Perlmazing/Perlmazing/find_parent_classes.pm »
1 package Perlmazing::Engine;
2 30 ▶use Perlmazing::Feature;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use Submodules;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Scalar::Util qw(set_prototype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Taint::Util 'untaint';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Data::Dump 'dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 our $VERSION = '1.2812';
9 30 my $found_symbols;
10 my $loaded_symbols;
11 30 my $precompile_symbols;
12 30 my $parameters;
13  
14 30 ▶sub found_symbols {
Subroutine found_symbols: called CC=3 SCAR=11.8
Pod documented
15 30 my $self = shift;
16 30 ▶ my $package = (shift or caller);
Condition33%1/3
shift() || caller
l!l&&r!l&&!r
0300
MC/DC0%0/2
shift() || caller
shift() caller
Truth table33%1/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
17 30 ▶ return unless exists $found_symbols->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $found_symbols->{$package} 0 30 30
18 30 sort keys %{$found_symbols->{$package}};
19 }
20  
21 0 ▶sub loaded_symbols {
Subroutine loaded_symbols: not called CC=3 SCAR=24.8
Pod documented
22 0 my $self = shift;
23 0 ▶ my $package = (shift or caller);
Condition0%0/3
shift() || caller
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || caller
shift() caller
Truth table0%0/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
24 0 ▶ return unless exists $loaded_symbols->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $loaded_symbols->{$package} 0 0 0
25 0 return sort keys %{$loaded_symbols->{$package}};
26 }
27  
28 30 ▶sub preload {
Subroutine preload: called CC=2 SCAR=8.1
Pod documented
29 30 my $self = shift;
30 30 my $package = caller;
31 30 for my $i (@_) {
32 0 _debug("Preloading symbol $i", $package);
33 0 _load_symbol($package, $i);
34 }
35 }
36  
37 30 ▶sub import {
Subroutine import: called CC=4 SCAR=14.1
38 30 my $self = shift;
39 30 ▶ carp "If passing arguments to this module, it most be using named arguments" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 30 30
40 30 my $package = caller;
41 30 my $p = {@_};
42 30 ▶ $parameters->{$package} = {} unless $parameters->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless $parameters->{$package} 30 0 30
43 30 $parameters->{$package} = {%{$parameters->{$package}}, %$p};
44 30 ▶ return if exists $found_symbols->{$package};
Branch: 1 branch
BranchTrueFalseTotal
if exists $found_symbols->{$package} 0 30 30
45 30 $found_symbols->{$package} = {};
46 30 _debug("Importing $self");
47 30 $self->_find_symbols($package);
48 }
49  
50 0 ▶sub _is_compile_phase {
Subroutine _is_compile_phase: not called CC=2 SCAR=17.9
51 0 my $code = q[
52 BEGIN {
53 use warnings 'FATAL' => 'all';
54 eval 'INIT{} 1' or die;
55 }
56 ];
57 0 eval $code;
58 0 ▶ return 0 if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
59 0 1;
60 }
61  
62 12112 ▶sub _debug {
Subroutine _debug: called CC=3 SCAR=11.4
63 12112 my $msg = shift;
64 12112 ▶ my $caller = (shift or caller);
Condition66%2/3
shift() || caller
l!l&&r!l&&!r
12082300
MC/DC0%0/2
shift() || caller
shift() caller
Truth table66%2/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
65 12112 ▶ print STDERR __PACKAGE__." DEBUG: Package $caller, $msg\n" if $parameters->{$caller}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $parameters->{$caller}{"debug"} 0 12112 12112
66 }
67  
68 30 ▶sub _find_symbols {
Subroutine _find_symbols: called CC=4 SCAR=13.9
69 30 my $self = shift;
70 30 my $package = shift;
71 30 my @paths;
72 my $seen_paths;
73 30 _debug("Looking for symbols", $package);
74 30 for my $i (Submodules->find("${package}::Perlmazing")) {
75 10800 ▶ next if $i->{Clobber};
Branch: 1 branch
BranchTrueFalseTotal
if $i->{"Clobber"} 7200 3600 10800
76 3600 _debug("Found file $i->{AbsPath} for symbol $i->{Name}", $package);
77 3600 $found_symbols->{$package}->{$i->Name} = {%$i};
78 3600 ▶ if ($i->Module eq "${package}::Perlmazing::Precompile::$i->{Name}") {
Branch: 1 branch
BranchTrueFalseTotal
if ($i->Module eq qq[${package}::Perlmazing::Precompile::$i->{"Name"}]) 1680 1920 3600
79 1680 $precompile_symbols->{$package}->{$i->Name} = $i;
80 }
81 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 30 ▶ no warnings; # prevents during-cleanup warnings when Submodules was destroyed and $i is undefined
Subroutine BEGIN: called CC=1 SCAR=0.0
83 3600 my $name = $i->Name;
84 3600 ▶ *{"${package}::$i->{Name}"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 20 unshift @_, $package, $name;
86 20 goto &_autoload;
87 3600 };
88 }
89 }
90  
91 30 ▶sub precompile {
Subroutine precompile: called CC=4 SCAR=14.0
Pod documented
92 30 my $self = shift;
93 30 my $package = caller;
94 30 ▶ return unless exists $precompile_symbols->{$package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $precompile_symbols->{$package} 0 30 30
95 30 for my $name (sort keys %{$precompile_symbols->{$package}}) {
96 # We detect already precompiled symbols by undefining this variable
97 # Note that symbols can in some cases (by internal recursion) be called
98 # more than once, not allowing _load_symbol to complete and mark it as loaded
99 # before being called again, so this part comes handy in those cases.
100 1680 ▶ next if not defined $precompile_symbols->{$package}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $precompile_symbols->{$package}{$name} 0 1680 1680
101 1680 undef $precompile_symbols->{$package}->{$name};
102 1680 _debug("Precompiling symbol $name", $package);
103 1680 _load_symbol($package, $name);
104 }
105 }
106  
107 0 ▶sub _preload {
Subroutine _preload: not called CC=2 SCAR=17.9
108 0 my $self = shift;
109 0 my $package = shift;
110 0 for my $i (@_) {
111 0 _debug("Preloading symbol $i", $package);
112 0 _load_symbol($package, $i);
113 }
114 }
115  
116 20 ▶sub _autoload {
Subroutine _autoload: called CC=1 SCAR=0.0
117 20 my ($package, $symbol) = (shift, shift);
118 20 _debug("Autoloading symbol $symbol", $package);
119 20 my $code = _load_symbol($package, $symbol);
120 20 goto $code;
121 }
122  
123 1700 ▶sub _load_symbol {
Subroutine _load_symbol: called CC=22 SCAR=40.5
124 1700 my ($package, $symbol) = (shift, shift);
125 1700 local $@;
126 1700 ▶ return $loaded_symbols->{$package}->{$symbol} if exists $loaded_symbols->{$package} and exists $loaded_symbols->{$package}->{$symbol};
Branch: 1 branch
BranchTrueFalseTotal
if exists $loaded_symbols->{$package} and exists $loaded_symbols->{$package}{$symbol} 12 1688 1700
Condition100%3/3
exists $loaded_symbols->{$package} and exists $loaded_symbols->{$package}{$symbol}
!ll&&!rl&&r
30165812
MC/DC100%2/2
exists $loaded_symbols->{$package} and exists $loaded_symbols->{$package}{$symbol}
exists $loaded_symbols->{$package} exists $loaded_symbols->{$package}{$symbol}
Truth table100%3/3
A and B
A= exists $loaded_symbols->{$package}B= exists $loaded_symbols->{$package}{$symbol}
ABresult
0X0
100
111
127 1688 ▶ croak "File $package/Perlmazing/$symbol.pm cannot be found in \@INC for symbol \&${package}::$symbol - \@INC contains: @INC" unless exists $found_symbols->{$package} and exists $found_symbols->{$package}->{$symbol};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $found_symbols->{$package} and exists $found_symbols->{$package}{$symbol} 0 1688 1688
Condition33%1/3
exists $found_symbols->{$package} and exists $found_symbols->{$package}{$symbol}
!ll&&!rl&&r
001688
MC/DC0%0/2
exists $found_symbols->{$package} and exists $found_symbols->{$package}{$symbol}
exists $found_symbols->{$package} exists $found_symbols->{$package}{$symbol}
Truth table33%1/3
A and B
A= exists $found_symbols->{$package}B= exists $found_symbols->{$package}{$symbol}
ABresult
0X0
100
111
128 1688 _debug("Reading file $found_symbols->{$package}->{$symbol}->{AbsPath}", $package);
129 1688 my $code = Submodules::Result::read($found_symbols->{$package}->{$symbol});
130 1688 _debug("Parsing contents of $found_symbols->{$package}->{$symbol}->{AbsPath}", $package);
131 1688 my $stderr = '';
132 1688 my $eval_string = "\n#line 1 $found_symbols->{$package}->{$symbol}->{AbsPath}\npackage ${package}::Perlmazing::$symbol; $code";
133 {
134 1688 local *STDERR;
135 1688 open STDERR, '>>', \$stderr;
136 1688 untaint $eval_string;
137 1688 eval $eval_string;
138 }
139 1688 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1688 1688
140 0 croak "While attempting to load symbol '$symbol': $e";
141 }
142 1688 ▶ print STDERR $stderr if length $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if length $stderr 0 1688 1688
143 1688 $loaded_symbols->{$package}->{$symbol} = "${package}::Perlmazing::${symbol}"->can('main');
144 1688 ▶ die "Unable to find sub 'main' at $found_symbols->{$package}->{$symbol}->{AbsPath} line 1 to EOF\n" unless $loaded_symbols->{$package}->{$symbol};
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded_symbols->{$package}{$symbol} 0 1688 1688
145 1688 _debug("Replacing skeleton symbol with actual code from $found_symbols->{$package}->{$symbol}->{AbsPath}", $package);
146 1688 ▶ if ("${package}::Perlmazing::${symbol}"->isa('Perlmazing::Listable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (("${package}::Perlmazing::$symbol")->isa("Perlmazing::Listable")) 0 1688 1688
147 0 _debug("Symbol &${package}::$symbol isa Perlmazing::Listable, creating wrapper sub around it", $package);
148 0 my $sub_main = $loaded_symbols->{$package}->{$symbol};
149 0 ▶ my $sub_pre = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
150 0 for my $i (@_) {
151 0 $sub_main->($i);
152 }
153 0 };
154 0 ▶ $loaded_symbols->{$package}->{$symbol} = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
155 0 my $wantarray = wantarray;
156 0 my @call = caller(1);
157 0 my @res = eval {
158 0 ▶ (@_) = ($_) if not @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
159 0 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
elsif (defined $wantarray) { } 0 0 0
160 0 my @res = @_;
161 0 foreach my $i (@res) {
162 0 $sub_pre->($i);
163 }
164 0 return @res;
165 } elsif (defined $wantarray) {
166 0 my $i = $_[0];
167 0 $sub_pre->($i);
168 0 return $i;
169 } else {
170 0 foreach my $i (@_) {
171 0 $sub_pre->($i);
172 }
173 }
174 };
175 0 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
176 0 ▶ if ($e =~ /^Modification of a read\-only value attempted/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^Modification of a read\-only value attempted/u) { } 0 0 0
177 0 die "Modification of a read-only value attempted at $call[1] line $call[2]\n";
178 } else {
179 0 die "Unhandled error in listable function ${package}::$symbol: $e\n";
180 }
181 }
182 0 ▶ return @res if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
183 0 $res[0];
184 0 };
185 }
186 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
187 30 ▶ no warnings qw(redefine once);
Subroutine BEGIN: called CC=1 SCAR=0.0
188 1688 my $skeleton = *{"${package}::$symbol"}{CODE};
189 1688 my ($callers, $offset);
190 1688 ▶ while (my $caller = caller($offset++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = caller $offset++) 8698 1688 10386
191 8698 $callers->{$caller}++;
192 }
193 1688 $callers->{$package}++;
194 1688 for my $i (keys %$callers) {
195 5121 ▶ next if $i eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $i eq __PACKAGE__ 1688 3433 5121
196 3433 ▶ if (exists ${"${i}::"}{$symbol} and my $ref = *{"${i}::$symbol"}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { "${i}::" }->{$symbol} and my $ref = *{"${i}::$symbol";}{"CODE"}) 1696 1737 3433
Condition100%2/2
exists do { "${i}::" }->{$symbol} and my $ref = *{"${i}::$symbol";}{"CODE"}
!ll
17371696
MC/DC100%1/1
exists do { "${i}::" }->{$symbol} and my $ref = *{"${i}::$symbol";}{"CODE"}
exists do { "${i}::" }->{$symbol}
Truth table100%2/2
A
A= exists do { "${i}::" }->{$symbol}
Aresult
00
11
197 1696 my $proto_old = prototype \&{"${i}::$symbol"};
198 1696 my $proto_new = prototype $loaded_symbols->{$package}->{$symbol};
199 1696 ▶ if ((defined $proto_new and defined $proto_old and $proto_old ne $proto_new) or (defined $proto_old and not defined $proto_new) or (defined $proto_new and not defined $proto_old)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto_new and defined $proto_old and $proto_old ne $proto_new or defined $proto_old and not defined $proto_new or defined $proto_new and not defined $proto_old) 1500 196 1696
Condition50%9/18
defined $proto_new and defined $proto_old and $proto_old ne $proto_new or defined $proto_old and not defined $proto_new or defined $proto_new and not defined $proto_old
l!l&&r!l&&!r
01500196
defined $proto_new and defined $proto_old and $proto_old ne $proto_new or defined $proto_old and not defined $proto_new
l!l&&r!l&&!r
001696
defined $proto_new and defined $proto_old and $proto_old ne $proto_new
!ll&&!rl&&r
169600
defined $proto_new and defined $proto_old
!ll&&!rl&&r
19615000
defined $proto_old and not defined $proto_new
!ll&&!rl&&r
169600
defined $proto_new and not defined $proto_old
!ll&&!rl&&r
19601500
MC/DC28%2/7
defined $proto_new and defined $proto_old and $proto_old ne $proto_new or defined $proto_old and not defined $proto_new or defined $proto_new and not defined $proto_old
defined $proto_new defined $proto_old $proto_old ne $proto_new defined $proto_old not defined $proto_new defined $proto_new not defined $proto_old
Truth table9%2/22
A and B and C or D and not(E) or F and not(G)
A= defined $proto_newB= defined $proto_oldC= $proto_old ne $proto_newD= defined $proto_oldE= not defined $proto_newF= defined $proto_newG= not defined $proto_old
ABCDEFGresult
111XXXX1
0XX11XX1
10X11XX1
11011XX1
0XX0X111
0XX10111
10X0X111
10X10111
1100X111
11010111
0XX0X0X0
0XX0X100
0XX100X0
0XX10100
10X0X0X0
10X0X100
10X100X0
10X10100
1100X0X0
1100X100
110100X0
11010100
200 # Disabling this as it is noisy and seems to be somewhat unimportant for general use. Leaving it commented in case I change my mind in a future version.
201 #carp "Warning: Too late to apply prototype ($proto_new) to symbol &${i}::$symbol - perl compilation phase has passed already" unless _is_compile_phase();
202 1500 set_prototype \&{"${i}::$symbol"}, $proto_new;
203 }
204 1696 ▶ *{"${i}::$symbol"} = $loaded_symbols->{$package}->{$symbol} if $ref eq $skeleton;
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq $skeleton 1696 0 1696
205 }
206 }
207 1688 _debug(__PACKAGE__." no longer has power over symbol &${package}::$symbol (it's now loaded on its own code)", $package);
208 1688 $loaded_symbols->{$package}->{$symbol};
209 }
210  
211 package Perlmazing::Listable;
212  
213 30 1;
214  
215 __END__
« lib/Perlmazing/Perlmazing/Precompile/sort_by_value.pm ↑ Summary blib/lib/Perlmazing/Perlmazing/find_parent_classes.pm »