blib/lib/Module/FromPerlVer.pm

filter: Statementstmt 100% 169 / 169 Branchbran 65.8% 54 / 82 Conditioncond 80.0% 8 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 34 / 34 Podpod n/a totaltotal 88.7% 267 / 301 CC 50 SCAR 17.4
CC
50
Cov
89%
__ANON__
15.3
__ANON__
8.5
__ANON__
7.4
CRAP 5.7
↑ Summary
1 ########################################################################
2 # housekeeping
3 ########################################################################
4  
5 # Use the lowest-version of Perl that supports
6 # File::Copy::Recursive & friends -- yeah, ancient.
7  
8 package Module::FromPerlVer;
9 77 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 77 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 77 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 77 ▶use Cwd qw( getcwd );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 77 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 77 ▶use File::Find qw( find );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 77 ▶use File::Spec::Functions qw( &catpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 77 ▶use FindBin qw( $Bin );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 77 ▶use List::Util qw( first reduce );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 77 ▶use Symbol qw( qualify_to_ref );
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 77 ▶use File::Copy::Recursive::Reduced qw( dircopy );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 use File::Spec::Functions
24 77 ▶qw
Subroutine BEGIN: called CC=1 SCAR=0.0
25 77 (
26 &abs2rel
27 &rel2abs
28  
29 &catdir
30 &catpath
31 &splitpath
32 77 );
33  
34 ########################################################################
35 # package variables & sanity checks
36 ########################################################################
37  
38 77 our $VERSION = version->parse( 'v0.3.0' )->numify;
39  
40 77 my $default_d = 'version';
41  
42 77 my $cwd = getcwd;
43  
44 ########################################################################
45 # utility subs
46 ########################################################################
47  
48 my @handlerz =
49 (
50 sub
51 94 ▶ {
Subroutine __ANON__: called CC=14 SCAR=27.3
52 # set running perl version.
53  
54 94 my $argz = shift;
55 94 ▶ my $path = $argz->{ version_from } || '';
Condition100%2/2
$argz->{'version_from'} || ''
l!l
5539
MC/DC100%1/1
$argz->{'version_from'} || ''
$argz->{'version_from'}
Truth table100%2/2
A
A= $argz->{'version_from'}
Aresult
11
00
56 94 my $perl_v = '';
57  
58 94 ▶ if( my $value = $ENV{ PERL_VERSION } )
Branch: 2 branches
BranchTrueFalseTotal
if (my $value = $ENV{'PERL_VERSION'}) { } 37 57 94
elsif ($path) { } 55 2 57
59 {
60 37 ▶ warn "Overriding '$path' with PERL_VERSION='$value'.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $path 0 37 37
61 if $path;
62  
63 37 ▶ $perl_v = version->parse( $value )->numify
Branch: 1 branch
BranchTrueFalseTotal
$perl_v = "version"->parse($value)->numify or die "Botched version_from: invalid '${value}' from PERL_VERSION.\n" 37 0 37
64 or
65 die "Botched version_from: invalid '$value' from PERL_VERSION.\n";
66 }
67 elsif( $path )
68 {
69 55 ▶ -e $path or die "Bogus version_from: non-existant '$path'\n";
Branch: 1 branch
BranchTrueFalseTotal
-e $path or die "Bogus version_from: non-existant '${path}'\n" 55 0 55
70 55 ▶ -f _ or die "Bogus version_from: non-file '$path'\n";
Branch: 1 branch
BranchTrueFalseTotal
-f _ or die "Bogus version_from: non-file '${path}'\n" 55 0 55
71 55 ▶ -s _ or die "Bogus version_from: zero-size '$path'\n";
Branch: 1 branch
BranchTrueFalseTotal
-s _ or die "Bogus version_from: zero-size '${path}'\n" 55 0 55
72 55 ▶ -r _ or die "Bogus version_from: non-readable '$path'\n";
Branch: 1 branch
BranchTrueFalseTotal
-r _ or die "Bogus version_from: non-readable '${path}'\n" 55 0 55
73  
74 # at this point the version file seems minimally usable.
75  
76 my $found
77 = do
78 55 {
79 55 ▶ open my $fh, '<', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "Botched version_from: open '${path}', $!\n" 55 0 55
80 or
81 die "Botched version_from: open '$path', $!\n";
82  
83 first
84 55 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.3
85 55 ▶ if
Branch: 2 branches
BranchTrueFalseTotal
if (my($min_v) = / \b use \s+ (v?5[.][.\d_]*)/x) { } 37 18 55
elsif (my($max_v) = / \b no \s+ (v?5[.][.\d_]*)/x) { } 16 2 18
86 (
87 my ( $min_v )
88 = m{ \b use \s+ (v?5[.][.\d_]*)}x
89 )
90 {
91 37 ▶ $perl_v
Branch: 1 branch
BranchTrueFalseTotal
$perl_v = "version"->parse($min_v)->numify or die "Invalid version string: '${_}' ($path)" 37 0 37
92 = version->parse( $min_v )->numify
93 or
94 die "Invalid version string: '$_' ($path)"
95 }
96 elsif
97 (
98 my ( $max_v )
99 = m{ \b no \s+ (v?5[.][.\d_]*)}x
100 )
101 {
102 16 ▶ $perl_v
Branch: 1 branch
BranchTrueFalseTotal
$perl_v = "version"->parse($max_v)->numify - "1e-06" or die "Invalid version string: no '${_}' ($path)" 16 0 16
103 = version->parse( $max_v )->numify - 0.000001
104 or
105 die "Invalid version string: no '$_' ($path)"
106 }
107 else
108 {
109 2 ''
110 }
111 }
112 grep
113 {
114 55 ▶ /\S/
Condition100%2/2
&first(sub { if (my($min_v) = / \b use \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($min_v)->numify or die "Invalid version string: '${_}' ($path)"; } elsif (my($max_v) = / \b no \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($max_v)->numify - 1e-06 or die "Invalid version string: no '${_}' ($path)"; } else { ''; } ; } , grep({/\S/;} readline $fh)) || die("Bogus version_from: '${path}' lacks 'use version'.\n")
l!l
532
MC/DC100%1/1
&first(sub { if (my($min_v) = / \b use \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($min_v)->numify or die "Invalid version string: '${_}' ($path)"; } elsif (my($max_v) = / \b no \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($max_v)->numify - 1e-06 or die "Invalid version string: no '${_}' ($path)"; } else { ''; } ; } , grep({/\S/;} readline $fh)) || die("Bogus version_from: '${path}' lacks 'use version'.\n")
&first(sub { if (my($min_v) = / \b use \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($min_v)->numify or die "Invalid version string: '${_}' ($path)"; } elsif (my($max_v) = / \b no \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($max_v)->numify - 1e-06 or die "Invalid version string: no '${_}' ($path)"; } else { ''; } ; } , grep({/\S/;} readline $fh))
Truth table100%2/2
A
A= &first(sub { if (my($min_v) = / \b use \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($min_v)->numify or die "Invalid version string: '${_}' ($path)"; } elsif (my($max_v) = / \b no \s+ (v?5[.][.\d_]*)/x) { $perl_v = 'version'->parse($max_v)->numify - 1e-06 or die "Invalid version string: no '${_}' ($path)"; } else { ''; } ; } , grep({/\S/;} readline $fh))
Aresult
11
00
115 }
116 readline $fh
117 or
118 die "Bogus version_from: '$path' lacks 'use version'.\n";
119 };
120 }
121 else
122 {
123 # this *should* to be parseable.
124  
125 2 ▶ $perl_v = version->parse( $^V )->numify
Branch: 1 branch
BranchTrueFalseTotal
$perl_v = "version"->parse($^V)->numify or die "Severe weirdness: unparsable \$^V ($^V).\n" 2 0 2
126 or
127 die "Severe weirdness: unparsable \$^V ($^V).\n";
128  
129 }
130  
131 # belt & suspenders time
132  
133 92 ▶ $perl_v
Branch: 1 branch
BranchTrueFalseTotal
$perl_v or die "Broken perl_version: no version extracted." 92 0 92
134 or die "Broken perl_version: no version extracted.";
135  
136 92 *{ qualify_to_ref 'perl_version' }
137 = sub
138 92 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 92 $perl_v
140 92 };
141  
142 92 print "# Perl version: '$perl_v'\n";
143  
144 return
145 92 },
146  
147 sub
148 92 ▶ {
Subroutine __ANON__: called CC=8 SCAR=21.4
149 # exract version parent directory
150  
151 92 my $argz = shift;
152 92 ▶ my $vers_d = $argz->{ version_dir } || $default_d;
Condition66%2/3
$argz->{'version_dir'} || $default_d
l!l&&r!l&&!r
72200
MC/DC0%0/2
$argz->{'version_dir'} || $default_d
$argz->{'version_dir'} $default_d
Truth table66%2/3
A || B
A= $argz->{'version_dir'}B= $default_d
ABresult
1X1
011
000
153  
154 92 my ( $vol, $dir ) = splitpath $vers_d, 1;
155  
156 # $Bin/$dir simplifies testing by locating './t/version'
157 # for anything running in ./t using only the default.
158  
159 my $path
160 = first
161 204 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 -d
163 204 }
164 map
165 {
166 92 ▶ my $d = catdir $_, $dir;
Branch: 1 branch
BranchTrueFalseTotal
my $path = &first(sub { -d $_; } , map({my $d = catdir($_, $dir); catpath($vol, $d, "");} "", $cwd, $Module::FromPerlVer::Bin)) or die "Bogus version_dir: Non-existant: '${dir}'.\n" 92 0 92
167  
168 276 catpath $vol, $d, ''
169 }
170 (
171 '',
172 $cwd,
173 $Bin,
174 )
175 or
176 die "Bogus version_dir: Non-existant: '$dir'.\n";
177  
178 # first, above, guarantees that this is the last
179 # dir examined. make the final sanity checks.
180  
181 92 ▶ -r _ or die "Bogus version_dir: non-readable '$path'";
Branch: 1 branch
BranchTrueFalseTotal
-r _ or die "Bogus version_dir: non-readable '${path}'" 92 0 92
182 92 ▶ -w _ or die "Bogus version_dir: non-writeable '$path'";
Branch: 1 branch
BranchTrueFalseTotal
-w _ or die "Bogus version_dir: non-writeable '${path}'" 92 0 92
183 92 ▶ -x _ or die "Bogus version_dir: non-execable '$path'";
Branch: 1 branch
BranchTrueFalseTotal
-x _ or die "Bogus version_dir: non-execable '${path}'" 92 0 92
184  
185 92 ▶ index $path, $cwd
Branch: 1 branch
BranchTrueFalseTotal
index $path, $cwd or $path = abs2rel($path, $cwd) 0 92 92
186 or
187 $path = abs2rel $path, $cwd;
188  
189 92 ▶ my @found = glob "$path/*"
Branch: 1 branch
BranchTrueFalseTotal
my(@found) = glob("$path/*") or die "Botchd version_dir: '${path}' is empty directory.\n" 92 0 92
190 2 ▶ or die "Botchd version_dir: '$path' is empty directory.\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
191  
192 92 *{ qualify_to_ref 'version_dir' }
193 92 ▶ = sub { $path };
Subroutine __ANON__: called CC=1 SCAR=0.0
194  
195 92 print "# Version directory: '$path'\n";
196  
197 return
198 92 },
199  
200 sub
201 92 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
202 # locate source dir supporting perl version.
203  
204 92 my $perl_v = perl_version();
205 92 my $version_d = version_dir();
206  
207 # klutzy as this looks it beats a sort on $_->[0]
208 # followed by first.
209  
210  
211 my $found
212 = reduce
213 3312 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
214 # i.e., from any dir with version <= perl_v
215 # prefer the one with the highest verison.
216 # equality handles differences in version
217 # format colliding on a single numified value.
218  
219 3312 ▶ $perl_v < $b->[0]
Branch: 2 branches
BranchTrueFalseTotal
$perl_v < $Module::FromPerlVer::b->[0] ? : 1744 1568 3312
$Module::FromPerlVer::a->[0] >= $Module::FromPerlVer::b->[0] ? : 1332 236 1568
220 ? $a
221 : $a->[0] >= $b->[0]
222 ? $a
223 : $b
224 }
225 (
226 # sentinel allows evaluating all version dir's
227 # for <= $perl_v.
228  
229 [ 0, '' ],
230 map
231 {
232 92 my $dir_v
233 = eval
234 3312 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
my $dir_v = eval { do { "version"->parse(basename($_))->numify } } or warn "Unusable version dir: '${_}', not parsable.\n" 3312 0 3312
235 3312 version->parse( basename $_ )->numify
236 }
237 or warn "Unusable version dir: '$_', not parsable.\n";
238  
239 3312 ▶ $dir_v
Branch: 1 branch
BranchTrueFalseTotal
$dir_v ? : 3312 0 3312
240 ? [ $dir_v, $_ ]
241 : ()
242 }
243 glob "$version_d/*"
244 );
245  
246 92 ▶ $found->[0]
Branch: 1 branch
BranchTrueFalseTotal
$found->[0] or die "Failed version dir: all '${version_d}' > $perl_v\n" 76 16 92
247 or
248 die "Failed version dir: all '$version_d' > $perl_v\n";
249  
250 76 my $source_d = $found->[ -1 ];
251  
252 76 *{ qualify_to_ref 'source_dir' }
253 = sub
254 152 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
255 152 $source_d
256 76 };
257  
258 76 print "# Source directory: '$source_d'\n";
259  
260 return
261 76 },
262  
263 sub
264 76 ▶ {
Subroutine __ANON__: called CC=7 SCAR=20.0
265 # set the destination directory.
266  
267 76 my $argz = shift;
268 76 ▶ my $dest_d = $argz->{ dest_dir } || $cwd;
Condition66%2/3
$argz->{'dest_dir'} || $cwd
l!l&&r!l&&!r
7510
MC/DC0%0/2
$argz->{'dest_dir'} || $cwd
$argz->{'dest_dir'} $cwd
Truth table66%2/3
A || B
A= $argz->{'dest_dir'}B= $cwd
ABresult
1X1
011
000
269  
270 76 ▶ -e $dest_d or die "Botched dest_dir: non-existant '$dest_d'";
Branch: 1 branch
BranchTrueFalseTotal
-e $dest_d or die "Botched dest_dir: non-existant '${dest_d}'" 76 0 76
271 76 ▶ -d _ or die "Botched dest_dir: non-directory '$dest_d'";
Branch: 1 branch
BranchTrueFalseTotal
-d _ or die "Botched dest_dir: non-directory '${dest_d}'" 76 0 76
272 76 ▶ -r _ or die "Botched dest_dir: non-readable '$dest_d'";
Branch: 1 branch
BranchTrueFalseTotal
-r _ or die "Botched dest_dir: non-readable '${dest_d}'" 76 0 76
273 76 ▶ -w _ or die "Botched dest_dir: non-writeable '$dest_d'";
Branch: 1 branch
BranchTrueFalseTotal
-w _ or die "Botched dest_dir: non-writeable '${dest_d}'" 76 0 76
274  
275 76 ▶ index $dest_d, $cwd
Branch: 1 branch
BranchTrueFalseTotal
index $dest_d, $cwd or $dest_d = abs2rel($dest_d, $cwd) 75 1 76
276 or
277 $dest_d = abs2rel $dest_d, $cwd;
278  
279 # looks minimally usable...
280  
281 76 *{ qualify_to_ref 'dest_dir' }
282 = sub
283 228 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
284 228 $dest_d
285 76 };
286  
287 76 print "# Dest dir: '$dest_d'.\n";
288  
289 return
290 76 },
291  
292 sub
293 76 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
294 # locate files to copy
295 #
296 # pre-assigning the hashref's simplifies dealing
297 # with a collection of empty dir's.
298  
299 76 my $source_d = source_dir();
300 76 my @pathz = ( [], [] );
301  
302 76 my $n = length $source_d;
303  
304 find
305 sub
306 760 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
307 760 my $path = $File::Find::name;
308  
309 760 ▶ $path eq $source_d
Branch: 1 branch
BranchTrueFalseTotal
$path eq $source_d and return 76 684 760
310 and return;
311  
312 684 ▶ my $is_dir
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 228 456 684
313 = -d
314 ? 1
315 : 0
316 ;
317  
318 684 my ( $vol, $dir, $base )
319 = splitpath $path, $is_dir;
320  
321 684 my $sub_d = abs2rel $dir, $source_d;
322  
323 684 push @{ $pathz[ $is_dir ] }, catpath $vol, $sub_d, $base;
324 },
325 76 $source_d;
326  
327 # deal with a set of empty dirs.
328  
329 76 ▶ @{ $pathz[0] }
Branch: 1 branch
BranchTrueFalseTotal
@{$pathz[0];} or warn "No input files found: '${source_d}'" 76 0 76
330 or warn "No input files found: '$source_d'";
331  
332 76 *{ qualify_to_ref 'source_paths' }
333 = sub
334 188 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
335 wantarray
336 ? @pathz
337 188 ▶ : $pathz[0]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 152 36 188
338 76 };
339  
340 76 local $, = "\n#\t";
341 76 print '# Source files:', @{ $pathz[0] }, "\n";
342  
343 return
344 76 },
345  
346 sub
347 76 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
348 76 my ( $dst_v, $dst_d ) = splitpath dest_dir(), 1;
349 76 my ( $filz, $dirz ) = source_paths();
350  
351 my @dst_filz
352 = map
353 {
354 76 my ( undef, $src_d, $base ) = splitpath $_;
355  
356 456 catpath $dst_v, catdir( $dst_d, $src_d ), $base;
357 }
358 @$filz;
359  
360 my @dst_dirz
361 = map
362 {
363 76 my ( undef, $dir, undef ) = splitpath $_, 1;
364  
365 228 catpath $dst_v, catdir( $dst_d, $dir ), '';
366 }
367 @$dirz;
368  
369 76 for( @dst_filz, @dst_dirz )
370 {
371 684 ▶ index $_, $cwd
Branch: 1 branch
BranchTrueFalseTotal
index $_, $cwd and $_ = abs2rel($_, $cwd) 684 0 684
372 and
373 $_ = abs2rel $_, $cwd
374 }
375  
376 76 *{ qualify_to_ref 'dest_paths' }
377 = sub
378 153 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
379  
380 wantarray
381 153 ▶ ? ( \@dst_filz, \@dst_dirz )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 81 72 153
382 : \@dst_filz
383 76 };
384 },
385  
386 sub
387 76 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
388 76 *{ qualify_to_ref 'cleanup' }
389 = sub
390 78 ▶ {
Subroutine __ANON__: called CC=5 SCAR=16.6
391 78 my ( $filz, $dirz ) = dest_paths();
392  
393 78 unlink @$filz;
394  
395 -e $_ && warn "Failed removal: '$_'"
396 78 ▶ for @$filz;
Branch: 1 branch
BranchTrueFalseTotal
-e $_ and warn "Failed removal: '${_}'" 0 468 468
397  
398 # i.e., don't clobber dir's which have any
399 # files we didn't copy in them. this is most
400 # likely for ./t which can have common tests.
401  
402 78 for my $dir ( @$dirz )
403 {
404 234 ▶ ( my @a = glob "$dir/*" )
Branch: 1 branch
BranchTrueFalseTotal
my(@a) = glob("$dir/*") or rmdir $dir 0 234 234
405 or
406 rmdir $dir;
407 }
408 76 };
409  
410 return
411 76 },
412  
413 sub
414 76 ▶ {
Subroutine __ANON__: called CC=1 SCAR=0.0
415 76 my ( $filz, $dirz ) = source_paths();
416 76 my $dest_d = dest_dir();
417 76 my $expect = 1 + @$filz + @$dirz;
418  
419 76 *{ qualify_to_ref 'copy_source_dir' }
420 = sub
421 76 ▶ {
Subroutine __ANON__: called CC=2 SCAR=7.0
422 76 my $src = source_dir();
423 76 my $dst = dest_dir();
424 76 my $found = dircopy $src, $dst;
425
426 76 print "# Copied: $found files from '$src'\n";
427  
428 76 ▶ $found != $expect
Branch: 1 branch
BranchTrueFalseTotal
$found != $expect and warn "# Oddity: mismatched count $found != $expect.\n" 0 76 76
429 and
430 warn "# Oddity: mismatched count $found != $expect.\n";
431  
432 76 $found
433 76 };
434  
435 76 print "# Expect: $expect files to be copied.\n";
436  
437 return
438 76 }
439 77 );
440  
441 ########################################################################
442 # import is where it all happens
443 ########################################################################
444  
445 sub import
446 94 ▶{
Subroutine import: called CC=3 SCAR=11.0
447 94 my ( undef, %argz ) = @_;
448  
449 94 for my $sub ( @handlerz )
450 {
451 658 $sub->( \%argz );
452 }
453  
454 # at this point the args are consumed and if we are
455 # still alive the copy function has been installed.
456 #
457 # caller can use cleanup() to remove files copied or
458 # source_paths() to get a list of them.
459 #
460 # this returns the count from File::Copy::Recursive,
461 # which should be true if anything was found to copy.
462  
463 $argz{ no_copy }
464 or
465 76 ▶ copy_source_dir()
Branch: 1 branch
BranchTrueFalseTotal
$argz{"no_copy"} or copy_source_dir() 3 73 76
466 }
467  
468 # keep require happy
469 77 1
470 __END__
↑ Summary