blib/lib/ExtUtils/InferConfig.pm

filter: Statementstmt 90.2% 93 / 103 Branchbran 50.0% 24 / 48 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 72.5% 135 / 186 CC 30 SCAR 24.1
CC
30
Cov
74%
_perl_to_file
30.7
_infer_config
9.3
_infer_inc
8.9
CRAP 11.2
↑ Summary
1 package ExtUtils::InferConfig;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use IPC::Cmd qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 $VERSION = '1.04';
11 }
12  
13 #use constant ISWIN32 => ($^O =~ /win32/i ? 1 : 0);
14  
15 =head1 NAME
16  
17 ExtUtils::InferConfig - Infer Perl Configuration for non-running interpreters
18  
19 =head1 SYNOPSIS
20  
21 use ExtUtils::InferConfig;
22 my $eic = ExtUtils::InferConfig->new(
23 perl => '/path/to/a/perl'
24 );
25
26 # Get that interpreters %Config as hash ref
27 my $Config = $eic->get_config;
28
29 # Get that interpreters @INC as array ref
30 my $INC = $eic->get_inc;
31  
32 =head1 DESCRIPTION
33  
34 This module can determine the configuration and C<@INC> of a perl
35 interpreter given its path and that it is runnable by the current
36 user.
37  
38 It runs the interpreter with a one-liner and grabs the C<%Config>
39 hash via STDOUT capturing. Getting the module load paths, C<@INC>,
40 works the same way for C<@INC> entries that are plain paths.
41  
42 =head1 METHODS
43  
44 =head2 new
45  
46 Requires one named parameter: C<perl>, the path to the perl
47 interpreter to query for information.
48  
49 Optional parameter: C<debug =E<gt> 1> enables the debugging mode.
50  
51 =cut
52  
53 2 ▶sub new {
Subroutine new: called CC=6 SCAR=19.9
Pod documented
54 2 my $class = shift;
55 2 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
56  
57 2 my %args = @_;
58  
59  
60 my $self = {
61 perl => undef,
62 config => undef,
63 inc => undef,
64 2 ▶ ($args{debug} ? (debug => 1) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$args{'debug'} ? : 0 2 2
65 };
66 2 bless $self => $class;
67  
68 # get interpreter, check that we have access
69 2 ▶ my $perl = $args{perl} || $^X;
Condition33%1/3
$args{'perl'} || $^X
l!l&&r!l&&!r
200
MC/DC0%0/2
$args{'perl'} || $^X
$args{'perl'} $^X
Truth table33%1/3
A || B
A= $args{'perl'}B= $^X
ABresult
1X1
011
000
70 2 $perl = $self->_perl_to_file($perl);
71  
72 2 ▶ if (not defined $perl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $perl) 0 2 2
73 0 croak(
74 "Invalid perl interpreter specified. "
75 ."It was either not found or it is not executable."
76 );
77 }
78  
79 2 ▶ warn "Using perl '$perl'" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
80  
81 2 $self->{perl} = $perl;
82  
83 2 return $self;
84 }
85  
86 2 ▶sub _perl_to_file {
Subroutine _perl_to_file: called CC=8 SCAR=34.3
87 # see perldoc perlvar about this. Look for $^X
88 2 my $self = shift;
89 2 my $perl = shift;
90  
91 2 ▶ return() if not defined $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $perl 0 2 2
92 2 ▶ return $perl if -f $perl and -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $perl and -x _ 2 0 2
Condition33%1/3
-f $perl and -x _
!ll&&!rl&&r
002
MC/DC0%0/2
-f $perl and -x _
-f $perl -x _
Truth table33%1/3
A and B
A= -f $perlB= -x _
ABresult
0X0
100
111
93  
94 # Build up a set of file names (not command names).
95 0 ▶ if ($^O ne 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "VMS") 0 0 0
96 $perl .= $Config{_exe}
97 0 ▶ unless $perl =~ m/\Q$Config{_exe}$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $perl =~ /\Q$ExtUtils::InferConfig::Config{"_exe"}$\E/i 0 0 0
98 }
99  
100 0 ▶ return $perl if -f $perl and -x _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $perl and -x _ 0 0 0
Condition0%0/3
-f $perl and -x _
!ll&&!rl&&r
000
MC/DC0%0/2
-f $perl and -x _
-f $perl -x _
Truth table0%0/3
A and B
A= -f $perlB= -x _
ABresult
0X0
100
111
101 0 return();
102 }
103  
104  
105 =head2 get_config
106  
107 Returns a copy of the C<%Config::Config> hash of the
108 intepreter which was specified as a parameter to the
109 constructor.
110  
111 The first time this method (or the get_inc method below)
112 is called, the perl binary is run. For subsequent calls
113 of this method, the information is cached.
114  
115 =cut
116  
117 1 ▶sub get_config {
Subroutine get_config: called CC=2 SCAR=7.0
Pod documented
118 1 my $self = shift;
119 1 ▶ return $self->{config} if defined $self->{config};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"config"} 0 1 1
120  
121 1 $self->{config} = $self->_infer_config($self->{perl});
122  
123 1 return $self->{config};
124 }
125  
126 1 ▶sub _infer_config {
Subroutine _infer_config: called CC=9 SCAR=22.2
127 1 my $self = shift;
128 1 my $perl = shift;
129 1 my $code = <<'HERE';
130 use Config;
131 foreach my $k (keys %Config) {
132 my $ek = $k;
133 $ek =~ s/([\n\t\r%])/q{%}.ord($1).q{;}/ge;
134 my $ev = $Config{$k};
135 if (defined $ev) {
136 $ev =~ s/([\n\t\r%])/q{%}.ord($1).q{;}/ge;
137 } else {
138 $ev = q{%-1;};
139 }
140 print qq{$ek\n$ev\n};
141 }
142 HERE
143  
144 1 ▶ warn "Running the following code:\n---$code\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
145  
146 1 $code =~ s/\s+$//;
147 1 $code =~ s/\n/ /g;
148  
149 1 my @command = (
150 $perl, '-e', $code
151 );
152 1 ▶ warn "Running the following command: '@command'" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
153  
154 1 my $old_use_run = $IPC::Cmd::USE_IPC_RUN;
155 1 $IPC::Cmd::USE_IPC_RUN = 1;
156 1 my ($success, $error_code, undef, $buffer, $error) = IPC::Cmd::run(
157 command => \@command,
158 );
159 1 $IPC::Cmd::USE_IPC_RUN = $old_use_run;
160
161  
162 1 ▶ warn "Returned buffer is:\n---\n".join("\n",@$buffer)."\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
163 1 ▶ warn "Returned error buffer is:\n---\n".join("\n",@$error)."\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
164  
165 1 ▶ if (not $success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 1 1
166 0 croak(
167 "Could not run the specified perl interpreter to determine \%Config. Error code (if any) was: $error_code. STDERR was (if any): ".join('', @$error)
168 );
169 }
170  
171 1 my %Config;
172 1 my @data = split /\n/, join '', @$buffer;
173 1 ▶ while (@data) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data) 1256 1 1257
174 1256 my $key = shift(@data);
175 1256 chomp $key;
176 1256 my $value = shift(@data);
177 1256 ▶ $value = '' if !defined $value; #in case of last value
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 1255 1256
178 1256 chomp $value;
179 0 $key =~ s/%(\d+);/chr($1)/eg;
180 1256 ▶ if ($value eq '%-1;') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '%-1;') { } 216 1040 1256
181 216 $value = undef;
182 }
183 else {
184 1040 $value =~ s/%(\d+);/chr($1)/eg;
185 }
186 1256 $Config{$key} = $value;
187 }
188  
189 1 return \%Config;
190 }
191  
192  
193 =head2 get_inc
194  
195 Returns a copy of the C<@INC> array of the
196 intepreter which was specified as a parameter to the
197 constructor. B<Caveat:> This skips any references
198 (subroutines, C<ARRAY> refs, objects) in the C<@INC>
199 array because they cannot be reliably stringified!
200  
201 The first time this method (or the get_config method avove)
202 is called, the perl binary is run. For subsequent calls
203 of this method, the information is cached.
204  
205 =cut
206  
207 1 ▶sub get_inc {
Subroutine get_inc: called CC=2 SCAR=7.0
Pod documented
208 1 my $self = shift;
209 1 ▶ return $self->{config} if defined $self->{inc};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"inc"} 0 1 1
210  
211 1 $self->{inc} = $self->_infer_inc($self->{perl});
212  
213 1 return $self->{inc};
214 }
215  
216  
217 1 ▶sub _infer_inc {
Subroutine _infer_inc: called CC=8 SCAR=21.8
218 1 my $self = shift;
219 1 my $perl = shift;
220 1 my $code = <<'HERE';
221 foreach my $inc (@INC) {
222 my $i = $inc;
223 if (not ref($i)) {
224 $i =~ s/([\n\t\r%])/q{%}.ord($1).q{;}/ge;
225 }
226 print qq{$i\n};
227 }
228 HERE
229 1 ▶ warn "Running the following code:\n---$code\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
230  
231 1 $code =~ s/\s+$//;
232 1 $code =~ s/\n/ /g;
233  
234 1 my @command = (
235 $perl, '-e', $code
236 );
237 1 ▶ warn "Running the following command: '@command'" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
238  
239 1 my $old_use_run = $IPC::Cmd::USE_IPC_RUN;
240 1 $IPC::Cmd::USE_IPC_RUN = 1;
241 1 my ($success, $error_code, undef, $buffer, $error) = IPC::Cmd::run(
242 command => \@command,
243 );
244 1 $IPC::Cmd::USE_IPC_RUN = $old_use_run;
245  
246 1 ▶ warn "Returned buffer is:\n---\n".join("\n",@$buffer)."\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
247 1 ▶ warn "Returned error buffer is:\n---\n".join("\n",@$error)."\n---" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
248  
249 1 ▶ if (not $success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 1 1
250 0 croak(
251 "Could not run the specified perl interpreter to determine \@INC. Error code (if any) was: $error_code. STDERR was (if any): ".join('', @$error)
252 );
253 }
254  
255 1 my @inc;
256 1 my @data = split /\n/, join '', @$buffer;
257 1 foreach my $line (@data) {
258 8 chomp $line;
259 8 ▶ if ($line eq '%-1;') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq '%-1;') { } 0 8 8
260 0 $line = undef;
261 }
262 else {
263 0 $line =~ s/%(\d+);/chr($1)/eg;
264 }
265 8 push @inc, $line;
266 }
267  
268 1 return \@inc;
269 }
270  
271  
272 3 1;
273 __END__
↑ Summary