blib/lib/SelfLoader.pm

filter: Statementstmt 85.8% 103 / 120 Branchbran 64.2% 36 / 56 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 85.0% 17 / 20 Podpod 25.0% 1 / 4 totaltotal 79.0% 170 / 215 SCAR 37.1
CC
32
Cov
79%
_load_stubs
29.9
AUTOLOAD
8.0
_add_to_cache
4.1
CRAP 40.9
↑ Summary
1 package SelfLoader;
2 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = "1.20";
6  
7 # The following bit of eval-magic is necessary to make this work on
8 # perls < 5.009005.
9 2 ▶use vars qw/$AttrList/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 BEGIN {
11 2 ▶ if ($] > 5.009004) {
Subroutine BEGIN: called CC=2 SCAR=7.4
Branch: 1 branch
BranchTrueFalseTotal
if ($] > 5.009004) { } 2 0 2
12 2 ▶ eval <<'NEWERPERL';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use 5.009005; # due to new regexp features
14 # allow checking for valid ': attrlist' attachments
15 # see also AutoSplit
16 $AttrList = qr{
17 \s* : \s*
18 (?:
19 # one attribute
20 (?> # no backtrack
21 (?! \d) \w+
22 (?<nested> \( (?: [^()]++ | (?&nested)++ )*+ \) ) ?
23 )
24 (?: \s* : \s* | \s+ (?! :) )
25 )*
26 }x;
27  
28 NEWERPERL
29 }
30 else {
31 0 eval <<'OLDERPERL';
32 # allow checking for valid ': attrlist' attachments
33 # (we use 'our' rather than 'my' here, due to the rather complex and buggy
34 # behaviour of lexicals with qr// and (??{$lex}) )
35 our $nested;
36 $nested = qr{ \( (?: (?> [^()]+ ) | (??{ $nested }) )* \) }x;
37 our $one_attr = qr{ (?> (?! \d) \w+ (?:$nested)? ) (?:\s*\:\s*|\s+(?!\:)) }x;
38 $AttrList = qr{ \s* : \s* (?: $one_attr )* }x;
39 OLDERPERL
40 }
41 }
42 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 our @ISA = qw(Exporter);
44 2 our @EXPORT = qw(AUTOLOAD);
45 0 ▶sub Version {$VERSION}
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
46 sub DEBUG () { 0 }
47  
48 2 my %Cache; # private cache for all SelfLoader's client packages
49  
50 # in croak and carp, protect $@ from "require Carp;" RT #40216
51  
52 4 ▶sub croak { { local $@; require Carp; } goto &Carp::croak }
Subroutine croak: called CC=1 SCAR=0.0
Pod undocumented
53 0 ▶sub carp { { local $@; require Carp; } goto &Carp::carp }
Subroutine carp: not called CC=1 SCAR=6.9
Pod undocumented
54  
55 AUTOLOAD {
56 12 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=8 SCAR=20.8
57 12 print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if DEBUG;
58 12 my $SL_code = $Cache{$AUTOLOAD};
59 12 my $save = $@; # evals in both AUTOLOAD and _load_stubs can corrupt $@
60 12 ▶ unless ($SL_code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SL_code) 7 5 12
61 # Maybe this pack had stubs before __DATA__, and never initialized.
62 # Or, this maybe an automatic DESTROY method call when none exists.
63 7 $AUTOLOAD =~ m/^(.*)::/;
64 7 ▶ SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Cache{"$1::<DATA"} 3 4 7
65 7 $SL_code = $Cache{$AUTOLOAD};
66 7 ▶ $SL_code = "sub $AUTOLOAD { }"
Branch: 1 branch
BranchTrueFalseTotal
if not $SL_code and $SelfLoader::AUTOLOAD =~ /::DESTROY$/ 1 6 7
Condition100%3/3
not $SL_code and $SelfLoader::AUTOLOAD =~ /::DESTROY$/
!ll&&!rl&&r
331
MC/DC100%2/2
not $SL_code and $SelfLoader::AUTOLOAD =~ /::DESTROY$/
not $SL_code $SelfLoader::AUTOLOAD =~ /::DESTROY$/
Truth table100%3/3
not(A) and B
A= not $SL_codeB= $SelfLoader::AUTOLOAD =~ /::DESTROY$/
ABresult
0X0
100
111
67 if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/);
68 7 ▶ croak "Undefined subroutine $AUTOLOAD" unless $SL_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $SL_code 3 4 7
69 }
70 9 print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if DEBUG;
71  
72 {
73 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 9 ▶ eval $SL_code;
Subroutine DESTROY: called CC=1 SCAR=0.0
75 }
76 9 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 8 9
77 1 $@ =~ s/ at .*\n//;
78 1 croak $@;
79 }
80 8 $@ = $save;
81 8 ▶ defined(&$AUTOLOAD) || die "SelfLoader inconsistency error";
Branch: 1 branch
BranchTrueFalseTotal
defined &$SelfLoader::AUTOLOAD or die "SelfLoader inconsistency error" 0 8 8
82 8 delete $Cache{$AUTOLOAD};
83 8 goto &$AUTOLOAD
84 }
85  
86 0 ▶sub load_stubs { shift->_load_stubs((caller)[0]) }
Subroutine load_stubs: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 sub _load_stubs {
89 # $endlines is used by Devel::SelfStubber to capture lines after __END__
90 3 ▶ my($self, $callpack, $endlines) = @_;
Subroutine _load_stubs: called CC=21 SCAR=34.0
91 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
92 3 my $fh = \*{"${callpack}::DATA"};
93 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 3 my $currpack = $callpack;
95 3 my($line,$name,@lines, @stubs, $protoype);
96  
97 3 print STDERR "SelfLoader::load_stubs($callpack)\n" if DEBUG;
98 3 ▶ croak("$callpack doesn't contain an __DATA__ token")
Branch: 1 branch
BranchTrueFalseTotal
unless defined fileno $fh 0 3 3
99 unless defined fileno($fh);
100 # Protect: fork() shares the file pointer between the parent and the kid
101 3 ▶ if(sysseek($fh, tell($fh), 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysseek $fh, tell $fh, 0) 3 0 3
102 3 ▶ open my $nfh, '<&', $fh or croak "reopen: $!";# dup() the fd
Branch: 1 branch
BranchTrueFalseTotal
open my $nfh, "<&", $fh or croak("reopen: $!") 0 3 3
103 3 ▶ close $fh or die "close: $!"; # autocloses, but be paranoid
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close: $!" 0 3 3
104 3 ▶ open $fh, '<&', $nfh or croak "reopen2: $!"; # dup() the fd "back"
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<&", $nfh or croak("reopen2: $!") 0 3 3
105 3 ▶ close $nfh or die "close after reopen: $!"; # autocloses, but be paranoid
Branch: 1 branch
BranchTrueFalseTotal
close $nfh or die "close after reopen: $!" 0 3 3
106 3 $fh->untaint;
107 }
108 3 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
109  
110 3 local($/) = "\n";
111 3 ▶ while(defined($line = <$fh>) and $line !~ m/^__END__/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh) and not $line =~ /^__END__/) 43 3 46
Condition100%3/3
defined($line = readline $fh) and not $line =~ /^__END__/
!ll&&!rl&&r
1243
MC/DC100%2/2
defined($line = readline $fh) and not $line =~ /^__END__/
defined($line = readline $fh) not $line =~ /^__END__/
Truth table100%3/3
A and not(B)
A= defined($line = readline $fh)B= not $line =~ /^__END__/
ABresult
0X0
100
111
112 43 ▶ if ($line =~ m/^\s*sub\s+([\w:]+)\s*((?:\([\\\$\@\%\&\*\;]*\))?(?:$AttrList)?)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\s*sub\s+([\w:]+)\s*((?:\([\\\$\@\%\&\*\;]*\))?(?:$SelfLoader::AttrList)?)/) { } 8 35 43
elsif ($line =~ /^package\s+([\w:]+)/) { } 1 34 35
113 8 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
114 8 $protoype = $2;
115 8 @lines = ($line);
116 8 ▶ if (index($1,'::') == -1) { # simple sub name
Branch: 1 branch
BranchTrueFalseTotal
if (index($1, '::') == -1) { } 8 0 8
117 8 $name = "${currpack}::$1";
118 } else { # sub name with package
119 0 $name = $1;
120 0 $name =~ m/^(.*)::/;
121 0 ▶ if (defined(&{"${1}::AUTOLOAD"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"$1::AUTOLOAD";}) { } 0 0 0
122 0 ▶ \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
Branch: 1 branch
BranchTrueFalseTotal
\&{"$1::AUTOLOAD";} == \&SelfLoader::AUTOLOAD or die "SelfLoader Error: attempt to specify Selfloading", " sub $name in non-selfloading module $1" 0 0 0
123 die 'SelfLoader Error: attempt to specify Selfloading',
124 " sub $name in non-selfloading module $1";
125 } else {
126 0 $self->export($1,'AUTOLOAD');
127 }
128 }
129 } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared
130 1 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
131 1 $self->_package_defined($line);
132 1 $name = '';
133 1 @lines = ();
134 1 $currpack = $1;
135 1 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
136 1 ▶ if (defined(&{"${1}::AUTOLOAD"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{"$1::AUTOLOAD";}) { } 0 1 1
137 0 ▶ \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
Branch: 1 branch
BranchTrueFalseTotal
\&{"$1::AUTOLOAD";} == \&SelfLoader::AUTOLOAD or die "SelfLoader Error: attempt to specify Selfloading", " package $currpack which already has AUTOLOAD" 0 0 0
138 die 'SelfLoader Error: attempt to specify Selfloading',
139 " package $currpack which already has AUTOLOAD";
140 } else {
141 1 $self->export($currpack,'AUTOLOAD');
142 }
143 } else {
144 34 push(@lines,$line);
145 }
146 }
147 3 ▶ if (defined($line) && $line =~ /^__END__/) { # __END__
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line and $line =~ /^__END__/) 2 1 3
Condition66%2/3
defined $line and $line =~ /^__END__/
!ll&&!rl&&r
102
MC/DC50%1/2
defined $line and $line =~ /^__END__/
defined $line $line =~ /^__END__/
Truth table66%2/3
A and B
A= defined $lineB= $line =~ /^__END__/
ABresult
0X0
100
111
148 2 ▶ unless ($line =~ /^__END__\s*DATA/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^__END__\s*DATA/) 1 1 2
149 1 ▶ if ($endlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endlines) 0 1 1
150 # Devel::SelfStubber would like us to capture the lines after
151 # __END__ so it can write out the entire file
152 0 @$endlines = <$fh>;
153 }
154 1 close($fh);
155 }
156 }
157 3 push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype));
158 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 3 ▶ eval join('', @stubs) if @stubs;
Branch: 1 branch
BranchTrueFalseTotal
if @stubs 3 0 3
160 }
161  
162  
163 sub _add_to_cache {
164 12 ▶ my($self,$fullname,$pack,$lines, $protoype) = @_;
Subroutine _add_to_cache: called CC=4 SCAR=14.0
165 12 ▶ return () unless $fullname;
Branch: 1 branch
BranchTrueFalseTotal
unless $fullname 4 8 12
166 carp("Redefining sub $fullname")
167 8 ▶ if exists $Cache{$fullname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Cache{$fullname} 0 8 8
168 8 $Cache{$fullname} = join('', "\n\#line 1 \"sub $fullname\"\npackage $pack; ", @$lines);
169 #$Cache{$fullname} = join('', "package $pack; ",@$lines);
170 8 print STDERR "SelfLoader cached $fullname: $Cache{$fullname}" if DEBUG;
171 # return stub to be eval'd
172 8 ▶ defined($protoype) ? "sub $fullname $protoype;" : "sub $fullname;"
Branch: 1 branch
BranchTrueFalseTotal
defined $protoype ? : 8 0 8
173 }
174  
175 ▶sub _package_defined {}
Subroutine _package_defined: called CC=1 SCAR=0.0
176  
177 2 1;
178 __END__
↑ Summary