filename.pm

filter: Statementstmt 96.3% 104 / 108 Branchbran 74.3% 58 / 78 Conditioncond 61.9% 13 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 13 / 13 Podpod 100% 1 / 1 totaltotal 81.2% 191 / 235 CC 48 SCAR 27.8
CC
48
Cov
85%
require
29.6
__ANON__
19.2
__ANON__
7.2
CRAP 16.1
↑ Summary pm.pm »
1  
2 2 ▶use v5.12.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use locale; # localize $!
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 package filename;
10 # ABSTRACT: Perl module to load files at compile-time, without BEGIN blocks.
11  
12  
13 2 ▶use Array::RefElem (); #qw( hv_store );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Carp 1.50 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Scalar::Util (); #qw( blessed refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 our $VERSION = 'v0.20.060'; # VERSION
20  
21  
22  
23 2 my ( $do, $eval, $check_inc, $error ) = (); # Private subs
24  
25 # Modified version of the code as specified in `perldoc -f require`
26 2 *import = \&require;
27 248 ▶sub require {
Subroutine require: called CC=17 SCAR=33.9
Pod documented
28 248 ▶ eval { $_[0]->isa(__PACKAGE__) } && shift
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_[0]->isa(__PACKAGE__) } } and shift() or Carp::croak($_[0], " is not a ", __PACKAGE__) 248 0 248
Condition33%1/3
eval { do { $_[0]->isa(__PACKAGE__) } } and shift()
!ll&&!rl&&r
00248
MC/DC0%0/2
eval { do { $_[0]->isa(__PACKAGE__) } } and shift()
eval { do { $_[0]->isa(__PACKAGE__) } } shift()
Truth table33%1/3
A and B
A= eval { do { $_[0]->isa(__PACKAGE__) } }B= shift()
ABresult
0X0
100
111
29 || Carp::croak( $_[0], " is not a ", __PACKAGE__ );
30 248 ▶ my $filename = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 248 248
31 248 ▶ Carp::croak("Null filename used") unless length($filename);
Branch: 1 branch
BranchTrueFalseTotal
unless length $filename 0 248 248
32  
33 248 ▶ if ( exists $INC{$filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $INC{$filename}) 18 230 248
34 18 ▶ return 1 if $INC{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$filename} 4 14 18
35 14 Carp::croak(
36 "Attempt to reload $filename aborted.\n"
37 . "Compilation failed in require"
38 );
39 }
40  
41 230 ▶ if ( File::Spec->file_name_is_absolute($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Spec"->file_name_is_absolute($filename)) 19 211 230
42 19 ▶ goto NOT_INC if $^V < v5.17.0 && !-r $filename;
Branch: 1 branch
BranchTrueFalseTotal
if $^V < v5.17.0 and not -r $filename 0 19 19
Condition33%1/3
$^V < v5.17.0 and not -r $filename
!ll&&!rl&&r
1900
MC/DC0%0/2
$^V < v5.17.0 and not -r $filename
$^V < v5.17.0 not -r $filename
Truth table33%1/3
A and not(B)
A= $^V < v5.17.0B= not -r $filename
ABresult
0X0
100
111
43 19 return $do->($filename);
44 }
45 211 foreach my $inc (@INC) {
46 211 my $fullpath = $check_inc->( $inc, $filename );
47 207 ▶ next unless length($fullpath);
Branch: 1 branch
BranchTrueFalseTotal
unless length $fullpath 8 199 207
48 199 ▶ return ref($fullpath)
Branch: 1 branch
BranchTrueFalseTotal
ref $fullpath ? : 180 19 199
49 ? $eval->( $$fullpath => $filename, $inc )
50 : $do->( $fullpath => $filename );
51 }
52 NOT_INC:
53 8 my $module = "";
54 8 ▶ if ( v5.18.0 <= $^V && $^V < v5.26.0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (v5.18.0 <= $^V and $^V < v5.26.0) 0 8 8
Condition33%1/3
v5.18.0 <= $^V and $^V < v5.26.0
!ll&&!rl&&r
080
MC/DC0%0/2
v5.18.0 <= $^V and $^V < v5.26.0
v5.18.0 <= $^V $^V < v5.26.0
Truth table33%1/3
A and B
A= v5.18.0 <= $^VB= $^V < v5.26.0
ABresult
0X0
100
111
55 0 ▶ if ( ( my $pm = $filename ) =~ s/\.pm\z// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $pm = $filename) =~ s/\.pm\z//l) 0 0 0
56 0 $pm =~ s!/!::!g;
57 0 $module = $pm;
58 }
59 0 ▶ $module = "(you may need to install the $module module) "
Branch: 1 branch
BranchTrueFalseTotal
if $module 0 0 0
60 if $module;
61 }
62 Carp::croak(
63 8 "Can't locate $filename in \@INC $module(\@INC contains: @INC)");
64 }
65  
66 ### Private subs ###
67  
68 211 ▶$check_inc = sub {
Subroutine __ANON__: called CC=19 SCAR=29.5
69 211 my ( $inc, $filename ) = @_;
70  
71 211 ▶ if ( my $ref = ref($inc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $inc) 192 19 211
72 192 my $subref = undef;
73 192 ▶ if ( defined( my $pkg = Scalar::Util::blessed($inc) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $pkg = builtin::blessed($inc))) { } 19 173 192
74 19 ▶ $subref = $inc->can("INC")
Branch: 1 branch
BranchTrueFalseTotal
$subref = $inc->can("INC") or Carp::croak(qq[Can't locate object method "INC" via package "$pkg"]) 15 4 19
75 or Carp::croak(
76 qq!Can't locate object method "INC" via package "$pkg"!
77 );
78 } else {
79 173 ▶ $subref
Branch: 2 branches
BranchTrueFalseTotal
$ref eq 'ARRAY' ? : 15 158 173
$ref eq 'CODE' ? : 158 0 158
80 = $ref eq "ARRAY" ? $inc->[0]
81 : $ref eq "CODE" ? $inc
82 : return;
83 }
84 188 my @elems = $subref->( $inc, $filename );
85 188 ▶ return unless @elems && ( my $elem_ref = ref( $elems[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless @elems and my $elem_ref = ref $elems[0] 8 180 188
Condition100%3/3
@elems and my $elem_ref = ref $elems[0]
!ll&&!rl&&r
44180
MC/DC100%2/2
@elems and my $elem_ref = ref $elems[0]
@elems my $elem_ref = ref $elems[0]
Truth table100%3/3
A and B
A= @elemsB= my $elem_ref = ref $elems[0]
ABresult
0X0
100
111
86 # Possible elements of @elems:
87 # SCALAR - Prepended code
88 # IO - Filehandle to read
89 # CODE - Code to execute with IO
90 # REF - State for CODE
91  
92 180 my $code = "";
93 180 ▶ if ( $elem_ref eq "SCALAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_ref eq "SCALAR") 105 75 180
94 105 $code = ${ shift @elems };
95 105 $elem_ref = ref( $elems[0] );
96 }
97  
98 180 my $fh = undef;
99 180 ▶ if ( $elem_ref eq "GLOB" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elem_ref eq "GLOB") 75 105 180
100 75 $fh = shift @elems;
101 75 $fh = *{$fh}{IO};
102 75 $elem_ref = ref( $elems[0] );
103 }
104  
105 180 my $sub = undef;
106 180 my $state = undef;
107 180 ▶ if ( $elem_ref eq "CODE" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elem_ref eq 'CODE') { } 120 60 180
elsif ($fh) { } 30 30 60
108 120 ( $sub, $state ) = @elems;
109  
110 120 local $_;
111 $code .= $_
112 120 ▶ while do { $_ = <$fh> if $fh; $sub->( 0, $state ); };
Branch: 2 branches
BranchTrueFalseTotal
do { $_ = readline $fh if $fh; &$sub(0, $state) } and do { $code .= $_ } 240 120 360
if $fh 135 225 360
113 } elsif ($fh) {
114 30 local $_;
115 30 ▶ $code .= $_ while (<$fh>);
Branch: 1 branch
BranchTrueFalseTotal
defined($_ = readline $fh) and do { $code .= $_ } 60 30 90
116 }
117  
118 180 return \$code;
119 }
120  
121 19 ▶ return unless length($inc);
Branch: 1 branch
BranchTrueFalseTotal
unless length $inc 0 19 19
122 19 my $fullpath = "$inc/$filename";
123 19 ▶ return unless -f $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fullpath 0 19 19
124 19 ▶ return if $^V < v5.17.0 && !-r _;
Branch: 1 branch
BranchTrueFalseTotal
if $^V < v5.17.0 and not -r _ 0 19 19
Condition33%1/3
$^V < v5.17.0 and not -r _
!ll&&!rl&&r
1900
MC/DC0%0/2
$^V < v5.17.0 and not -r _
$^V < v5.17.0 not -r _
Truth table33%1/3
A and not(B)
A= $^V < v5.17.0B= not -r _
ABresult
0X0
100
111
125 19 return $fullpath;
126 2 };
127  
128 2 my $do_text = <<'END';
129 package $pkg;
130 my $result = do($fullpath);
131 $INC{$filename} = delete $INC{$fullpath}
132 if $filename ne $fullpath && exists $INC{$fullpath};
133 if ($@) {
134 $INC{$filename} && Array::RefElem::hv_store( %INC, $filename, undef );
135 die $@;
136 }
137 $result;
138 END
139 38 ▶$do = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
140 38 ▶ my $fullpath = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 38 0 38
141 38 ▶ my $filename = @_ ? shift : $fullpath;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 19 19 38
142 38 my ($pkg) = caller(2);
143  
144 38 ( my $do_eval = $do_text ) =~ s/\$pkg/$pkg/;
145 38 ▶ return eval $do_eval || $error->( $filename => $fullpath );
Condition100%3/3
eval $do_eval || &$error($filename, $fullpath)
l!l&&r!l&&!r
41915
MC/DC0%0/2
eval $do_eval || &$error($filename, $fullpath)
eval $do_eval &$error($filename, $fullpath)
Truth table33%1/3
A || B
A= eval $do_evalB= &$error($filename, $fullpath)
ABresult
1X1
011
000
146 2 };
147  
148 2 my $eval_text = <<'END';
149 package $pkg;
150 my $result = eval $code;
151 Array::RefElem::hv_store( %INC, $filename, $@ ? undef : $inc );
152 die $@ if $@;
153 $result;
154 END
155 180 ▶$eval = sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
156 180 ▶ my $code = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 180 0 180
157 180 ▶ my $filename = @_ ? shift : $code;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 180 0 180
158 180 ▶ my $inc = @_ ? shift : $filename;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 180 0 180
159 180 my ($pkg) = caller(2);
160  
161 180 my $tmpfile = sprintf( "/loader/0x%x/%s",
162 Scalar::Util::refaddr( \$code ),
163 $filename
164 );
165 180 $code = "#line 0 $tmpfile\n" . $code;
166  
167 180 ( my $eval_eval = $eval_text ) =~ s/\$pkg/$pkg/;
168 180 ▶ return eval $eval_eval || $error->( $filename => $inc );
Condition100%3/3
eval $eval_eval || &$error($filename, $inc)
l!l&&r!l&&!r
2414313
MC/DC0%0/2
eval $eval_eval || &$error($filename, $inc)
eval $eval_eval &$error($filename, $inc)
Truth table33%1/3
A || B
A= eval $eval_evalB= &$error($filename, $inc)
ABresult
1X1
011
000
169 2 };
170  
171 190 ▶$error = sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
172 190 ▶ my $filename = @_ ? shift : $_;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 190 0 190
173 190 ▶ my $fullpath = @_ ? shift : $filename;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 190 0 190
174  
175 190 ▶ $@ && Carp::croak( $@, "Compilation failed in require" );
Branch: 1 branch
BranchTrueFalseTotal
$@ and Carp::croak($@, "Compilation failed in require") 14 176 190
176  
177 # $INC{$filename} shouldn't be set if we've gotten here,
178 # and $! is invalid if $INC{$filename} is true.
179 176 ▶ delete $INC{$filename} && undef($!);
Branch: 1 branch
BranchTrueFalseTotal
delete $INC{$filename} and undef $! 168 8 176
180  
181 176 ▶ $! && Carp::croak(
Branch: 2 branches
BranchTrueFalseTotal
$! and Carp::croak("Can't locate ${filename}: ", $^V >= v5.21.0 ? "${fullpath}: " : (), "$!") 8 168 176
$^V >= v5.21.0 ? : 8 0 8
182 "Can't locate $filename: ",
183 $^V >= v5.21.0 ? "$fullpath: " : (),
184 "$!"
185 );
186  
187 168 Carp::croak( $filename, " did not return a true value" );
188 2 };
189  
190 2 1;
191  
192 __END__
↑ Summary pm.pm »