blib/lib/Devel/Init.pm

filter: Statementstmt 71.7% 137 / 191 Branchbran 56.9% 41 / 72 Conditioncond 45.4% 10 / 22 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 80.7% 21 / 26 Podpod 100% 4 / 4 totaltotal 66.3% 217 / 327 CC 51 SCAR 19.9
CC
51
Cov
66%
_runOneInit
17.7
_registerInitFor
14.1
_getAttribs
12.0
CRAP 7.3
↑ Summary
1 package Devel::Init;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use vars qw<
Subroutine BEGIN: called CC=1 SCAR=0.0
5 $VERSION @EXPORT_OK @Predicates
6 %Attribs @Inits $StackTrace $DefaultInit
7 9 >;
8 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 $VERSION= 0.001_002;
10 9 $DefaultInit= 1; # RunInits('default') at INIT-time?
11  
12 # Just in case somebody wants Exporter.pm-like information:
13 9 @EXPORT_OK= (
14 ':Init', # Allows: sub foo :Init(...)
15 ':NoDefault', # Disables default initialization step
16 'RegisterInit', # RegisterInit(\&sub,@predicates)
17 'RunInits', # RunInits(@options,@predicates)
18 'InitBlock', # $single ||= InitBlock { ... return $init; };
19 'InitSub', # $single ||= InitSub( sub { ... return $init; } );
20 '-StackTrace', # Add a stack trace to a fatal error
21 '-NoStackTrace', # Restore default of no stack traces
22 '@Predicates', # Current list of predicates in RunInits()
23 );
24 }
25  
26 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # Name of sub called when "sub foo :Init" code is compiled:
28 9 AttrSetter => 'MODIFY_CODE_ATTRIBUTES',
29 # Name of sub called by attributes::get() to retrieve sub attributes:
30 AttrGetter => 'FETCH_CODE_ATTRIBUTES',
31 9 };
32  
33 9 return 1; # Only subroutine definitions to be found below this point
34  
35  
36 9 ▶INIT {
Subroutine INIT: called CC=2 SCAR=6.9
37 9 ▶ RunInits( 'default' )
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Init::DefaultInit 7 2 9
38 if $DefaultInit;
39 }
40  
41  
42 sub import
43 11 ▶{
Subroutine import: called CC=8 SCAR=20.8
44 11 my( $us, @what )= @_;
45 11 my $pkg= caller();
46  
47 11 local( $_ );
48 11 for( @what ) {
49 15 ▶ if( /^:Init$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if (/^:Init$/) { } 5 10 15
elsif (/^:NoDefault$/) { } 1 9 10
elsif (/^-(No)?StackTrace$/) { } 0 9 9
elsif (/^\@(Predicates)$/) { } 2 7 9
elsif (/^&?(RunInits|RegisterInit|InitBlock|InitSub)$/) { } 6 1 7
50 5 _exportInit( $pkg );
51 } elsif( /^:NoDefault$/ ) {
52 1 $DefaultInit= 0;
53 } elsif( /^-(No)?StackTrace$/ ) {
54 0 $StackTrace= ! $1;
55 } elsif( /^\@(Predicates)$/ ) {
56 2 _export( $pkg, 'ARRAY', '@', $1 );
57 } elsif( /^&?(RunInits|RegisterInit|InitBlock|InitSub)$/ ) {
58 6 my $sub= $1;
59 6 ▶ $DefaultInit= 0
Branch: 1 branch
BranchTrueFalseTotal
if "RunInits" eq $sub 1 5 6
60 if 'RunInits' eq $sub;
61 6 _export( $pkg, 'CODE', '&', $sub );
62 } else {
63 1 _blameCaller( 1, __PACKAGE__, " does not export ($_)" );
64 }
65 }
66 }
67  
68  
69 sub _blameCaller
70 2 ▶{
Subroutine _blameCaller: called CC=1 SCAR=0.0
71 2 my( $depth, @text )= @_;
72 2 my( $pkg, $file, $line )= caller($depth);
73 2 die @text, " at $file line $line\n";
74 }
75  
76  
77 sub _export
78 8 ▶{
Subroutine _export: called CC=4 SCAR=13.9
79 8 my( $pkg, $type, $sig, $name )= @_;
80 8 my $glob= _getGlob( $pkg, $name );
81 8 my $src= *{ _getGlob( __PACKAGE__, $name ) }{$type};
82 8 ▶ die $sig, __PACKAGE__, "::$name is not of type $type?!"
Branch: 1 branch
BranchTrueFalseTotal
if $type ne ref $src 0 8 8
83 if $type ne ref($src);
84 8 ▶ die __PACKAGE__, "::$name not defined?!"
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 8 8
85 if ! $src;
86 8 ▶ if( *{$glob}{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (*{$glob;}{$type}) 1 7 8
87 1 _blameCaller( 2, __PACKAGE__,
88 " won't export $sig$name over existing definition" );
89 }
90 7 *{$glob}= $src;
91 }
92  
93  
94 sub _getGlob
95 26 ▶{
Subroutine _getGlob: called CC=3 SCAR=11.0
96 26 my( $pkg, $name )= @_;
97 9 ▶ my $stash= do { no strict 'refs'; \%{$pkg.'::'} };
Subroutine BEGIN: called CC=1 SCAR=0.0
98 26 my $glob= \$stash->{$name};
99 26 ▶ if( 'GLOB' ne ref $glob ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("GLOB" ne ref $glob) 17 9 26
100 # Must be 5.10 with its space optimization!
101 # So we need to force a real glob to be created.
102 # This means that that sub was actually defined as a constant, but
103 # we'll let the likely *{$glob}= sub ...; code trigger the warning.
104 # We only do this to prevent the completely unenlightening
105 # but fatal "not a GLOB reference" error.
106 {
107 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 17 ${$pkg.'::'.$name}= undef;
109 }
110 17 $glob= \$stash->{$name};
111 17 ▶ die "Can't coerce optimized STAB entry ($pkg\::$name) into real GLOB"
Branch: 1 branch
BranchTrueFalseTotal
if "GLOB" ne ref $glob 0 17 17
112 if 'GLOB' ne ref $glob;
113 }
114 26 return $glob;
115 }
116  
117  
118 sub _exportInit
119 5 ▶{
Subroutine _exportInit: called CC=3 SCAR=12.7
120 5 my( $pkg )= @_;
121 # Export a &MODIFY_CODE_ATTRIBUTES to the calling package
122  
123 5 my $glob= _getGlob( $pkg, AttrSetter() );
124 5 my $prev= *{$glob}{CODE};
125 5 ▶ if( ! $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $prev) { } 5 0 5
126 5 *{$glob}= \&_newAttrib;
127 } else {
128 0 my $prevSetter= $prev;
129 0 ▶ *{$glob}= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 return _newAttrib( @_[0,1], $prevSetter->(@_) );
131 0 };
132 }
133  
134 5 $glob= _getGlob( $pkg, AttrGetter() );
135 5 $prev= *{$glob}{CODE};
136 5 ▶ if( ! $prev ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $prev) { } 5 0 5
137 5 *{$glob}= \&_getAttribs;
138 } else {
139 0 my $prevGetter= $prev;
140 0 ▶ *{$glob}= sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
141 0 return( $prevGetter->(@_), _getAttribs(@_) );
142 0 };
143 }
144 }
145  
146  
147 sub _getAttribs
148 0 ▶{
Subroutine _getAttribs: not called CC=3 SCAR=24.8
149 0 my( $code )= @_;
150 0 my $addr;
151 0 ▶ if( 'CODE' eq ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $code) { } 0 0 0
152 0 $addr= 0 + $code;
153 } else {
154 0 die AttrGetter(), " given a non-code reference: $code";
155 }
156 0 ▶ return @{ $Attribs{ 0 + $code } || [] };
Condition0%0/2
$Devel::Init::Attribs{0 + $code} || []
l!l
00
MC/DC0%0/1
$Devel::Init::Attribs{0 + $code} || []
$Devel::Init::Attribs{0 + $code}
Truth table0%0/2
A
A= $Devel::Init::Attribs{0 + $code}
Aresult
11
00
157 }
158  
159  
160 sub _newAttrib
161 5 ▶{
Subroutine _newAttrib: called CC=3 SCAR=11.1
162 5 my( $pkg, $code, @attribs )= @_;
163 5 my @left;
164  
165 5 local( $_ );
166 5 for( @attribs ) {
167 5 ▶ if( /^Init\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^Init\b/) { } 5 0 5
168 5 push @{ $Attribs{ 0 + $code } }, $_;
169 5 s/^Init\(?//;
170 5 s/\)$//;
171 5 _registerInitFor( $pkg, $code, split /[,\s]+/, $_ );
172 } else {
173 0 push @left, $_;
174 }
175 }
176  
177 5 return @left;
178 }
179  
180  
181 sub _registerInitFor
182 15 ▶{
Subroutine _registerInitFor: called CC=6 SCAR=26.5
183 15 my( $pkg, $code, @conds )= @_;
184 @conds= map {
185 15 ▶ if( /^(\\?)\@(\w+)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^(\\?)\@(\w+)$/) { } 0 13 13
elsif (not /^\w[-.\w]*$/) { } 0 13 13
186 0 my( $asRef, $name )= ( $1, $2 );
187 0 my $av= *{ _getGlob( $pkg, $name ) }{ARRAY};
188 0 ▶ if( ! $av ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $av) { } 0 0 0
elsif ($asRef) { } 0 0 0
189 0 _blameCaller( 2,
190 "Undeclared array name, \@$pkg\::$name, in :Init(@conds)",
191 );
192 } elsif( $asRef ) {
193 0 $av;
194 } else {
195 0 @$av;
196 }
197 } elsif( ! /^\w[-.\w]*$/ ) {
198 0 _blameCaller( 2,
199 "Invalid predicate name, $_, in :Init(@conds)",
200 );
201 } else {
202 13 $_;
203 }
204 } @conds;
205 15 ▶ push @Inits, @conds ? [ $code, @conds ] : $code;
Branch: 1 branch
BranchTrueFalseTotal
@conds ? : 13 2 15
206 }
207  
208  
209 sub InitBlock(&)
210 4 ▶{
Subroutine InitBlock: called CC=1 SCAR=0.0
Pod documented
211 4 my( $code )= @_;
212 4 return InitSub( $code, 1 );
213 }
214  
215  
216 {
217 0 my %running;
218 0 my @stack;
219  
220  
221 sub InitSub
222 5 ▶ {
Subroutine InitSub: called CC=6 SCAR=18.5
Pod documented
223 5 my( $code, $depth )= @_;
224 5 ▶ my( $pkg, $file, $line, $sub )= caller( $depth || 0 );
Condition100%2/2
$depth || 0
l!l
41
MC/DC100%1/1
$depth || 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
225 5 my $key= join ",", $pkg, $sub, $line, $file;
226 5 for my $val ( $running{$key} ) {
227 5 ▶ if( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 0 5 5
228 0 die _cycleReport( $val, $pkg, $sub, $line );
229 }
230 5 $val= @stack;
231 }
232 5 push @stack, $key;
233 5 my @ret;
234 5 ▶ if( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 5 5
235 0 @ret= $code->();
236 } else {
237 5 $ret[0]= $code->();
238 }
239 5 delete $running{$key};
240 5 pop @stack;
241 5 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
242 }
243  
244  
245 sub _cycleReport
246 0 ▶ {
Subroutine _cycleReport: not called CC=3 SCAR=24.8
247 0 my( $start, $pkg, $sub, $line )= @_;
248 0 my $steps= @stack - $start;
249 0 my $err= "Circular initialization dependency detected.\n";
250 0 ▶ if( $StackTrace ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::Init::StackTrace) 0 0 0
251 0 require Carp;
252 0 $err= Carp::longmess( $err );
253 }
254 0 $err .= join ' ',
255 "The following initialization steps",
256 "form the $steps-step cycle:\n";
257 0 for my $i ( $start .. $#stack ) {
258 0 my( $pkg, $sub, $line, $file )= split /,/, $stack[$i];
259 0 $err .= " $pkg\::$sub() (line $line) calls\n";
260 }
261 0 $err .= " $pkg\::$sub().\n";
262 0 $err .= "$pkg\::$sub() indirectly calls itself!\n";
263 0 return $err;
264 }
265  
266 }
267  
268  
269 sub RegisterInit
270 0 ▶{
Subroutine RegisterInit: called CC=1 SCAR=0.4
Pod documented
271 # my( $code, @conds )= @_;
272 10 my $pkg= caller();
273 10 _registerInitFor( $pkg, @_ );
274 }
275  
276  
277 sub _runOneInit
278 13 ▶{
Subroutine _runOneInit: called CC=6 SCAR=28.7
279 13 my( $code, $svFail )= @_;
280 13 my $error;
281 13 my $okay= do {
282 13 my $prev= $SIG{__DIE__};
283 0 ▶ local $SIG{__DIE__}= sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
284 0 ▶ if( ! $StackTrace ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Devel::Init::StackTrace) { } 0 0 0
285 0 $error= $_[0];
286 } else {
287 0 require Carp;
288 0 $error= Carp::longmess( $_[0] );
289 }
290 0 ▶ die @_
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 0 0 0
291 if ! $prev;
292 0 $prev->( @_ );
293 13 };
294  
295 13 eval { $code->(); 1 };
296 };
297 13 ▶ if( ! $okay ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $okay) 0 13 13
298 0 ▶ $$svFail=
Branch: 1 branch
BranchTrueFalseTotal
$Devel::Init::StackTrace && $error ? : 0 0 0
Condition0%0/8
$Devel::Init::StackTrace and $error
!ll&&!rl&&r
000
$@ or $error or "Unknown failure initializing"
l!l
00
$@ or $error
l!l&&r!l&&!r
000
MC/DC0%0/2
$Devel::Init::StackTrace and $error
$Devel::Init::StackTrace $error
MC/DC0%0/2
$@ or $error or "Unknown failure initializing"
$@ $error
Truth table0%0/3
A and B
A= $Devel::Init::StackTraceB= $error
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $@B= $error
ABresult
1X1
011
000
299 $StackTrace && $error
300 ? $error
301 : $@ || $error || "Unknown failure initializing";
302 }
303 }
304  
305  
306 sub _rankConds
307 29 ▶{
Subroutine _rankConds: called CC=3 SCAR=11.0
308 29 my( @conds )= @_;
309 return map {
310 29 ▶ if( s/^not_// ) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^not_//) { } 7 17 24
elsif (s/^only_//) { } 8 9 17
311 7 ( $_, -1 );
312 } elsif( s/^only_// ) {
313 8 ( $_, 2 );
314 } else {
315 9 ( $_, 1 );
316 }
317 } @conds;
318 }
319  
320  
321 sub _skipInit
322 17 ▶{
Subroutine _skipInit: called CC=6 SCAR=17.9
323 17 my( $code, $hvHave, $svFail )= @_;
324 17 my @need;
325 17 ▶ ( $code, @need )= @$code
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $code 15 2 17
326 if 'ARRAY' eq ref($code);
327  
328 17 my %need= _rankConds( @need );
329 17 for my $cond ( keys %need, keys %$hvHave ) {
330 return 1 # We skipped this init; keep it around to run later
331 25 ▶ if 1 < abs( ($need{$cond}||0) - ($hvHave->{$cond}||0) );
Branch: 1 branch
BranchTrueFalseTotal
if 1 < abs(($need{$cond} || 0) - ($hvHave->{$cond} || 0)) 4 21 25
Condition100%4/4
$need{$cond} || 0
l!l
214
$hvHave->{$cond} || 0
l!l
187
MC/DC100%1/1
$need{$cond} || 0
$need{$cond}
MC/DC100%1/1
$hvHave->{$cond} || 0
$hvHave->{$cond}
Truth table100%2/2
A
A= $need{$cond}
Aresult
11
00
Truth table100%2/2
A
A= $hvHave->{$cond}
Aresult
11
00
332 }
333  
334 13 _runOneInit( $code, $svFail );
335 13 return 0; # We didn't skip this init; don't run it again
336 }
337  
338  
339 sub RunInits
340 12 ▶{
Subroutine RunInits: called CC=6 SCAR=20.9
Pod documented
341 12 local( $StackTrace )= $StackTrace;
342 12 ▶ while( @_ && $_[0] =~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] =~ /^-/) 0 12 12
Condition66%2/3
@_ and $_[0] =~ /^-/
!ll&&!rl&&r
390
MC/DC0%0/2
@_ and $_[0] =~ /^-/
@_ $_[0] =~ /^-/
Truth table66%2/3
A and B
A= @_B= $_[0] =~ /^-/
ABresult
0X0
100
111
343 0 local $_= shift @_;
344 0 ▶ if( /^-(No)StackTrace$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-(No)StackTrace$/i) { } 0 0 0
345 0 $StackTrace= ! $1;
346 } else {
347 0 _blameCaller( 1, "Unknown RunInits() option, $_" );
348 }
349 }
350 12 local( @Predicates )= @_;
351 12 my %have= _rankConds( @_ );
352 12 my $inits= 0+@Inits;
353  
354 12 my $fail;
355 @Inits= grep {
356 12 ▶ $fail || _skipInit( $_, \%have, \$fail );
Condition66%2/3
$fail || _skipInit($_, \%have, \$fail)
l!l&&r!l&&!r
0413
MC/DC50%1/2
$fail || _skipInit($_, \%have, \$fail)
$fail _skipInit($_, \%have, \$fail)
Truth table66%2/3
A || B
A= $failB= _skipInit($_, \%have, \$fail)
ABresult
1X1
011
000
357 } @Inits;
358 12 ▶ die $fail
Branch: 1 branch
BranchTrueFalseTotal
if $fail 0 12 12
359 if $fail;
360  
361 12 return $inits;
362 }
363  
364  
365 __END__
↑ Summary