auto/share/dist/Build-PPK/stub.pl untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 102 CC 7 SCAR 22.9
CC
7
Cov
0%
run
20.0
extract_to
12.0
exec_tar
6.0
CRAP 9.9
1 use strict;
2 use warnings;
3  
4 use POSIX ();
5 use File::Temp ();
6 use MIME::Base64 ();
7  
8 sub exec_tar {
9 my (@args) = @_;
10  
11 my @PATH = qw(/usr/local/bin /usr/bin /bin);
12 my @COMMANDS = qw(gtar tar bsdtar);
13  
14 foreach my $command (@COMMANDS) {
15 foreach my $dir (@PATH) {
16 my $file = "$dir/$command";
17  
18 if ( -x $file ) {
19 exec( $file, @args ) or die("Unable to exec() $command: $!");
20 }
21 }
22 }
23  
24 die( 'Could not locate tar binary in ' . join( ':', @PATH ) );
25 }
26  
27 sub extract_to {
28 my ($tmpdir) = @_;
29  
30 pipe my ( $out, $in ) or die("Unable to pipe(): $!");
31  
32 my $pid = fork();
33  
34 if ( !defined $pid ) {
35 die("Unable to fork(): $!");
36 }
37 elsif ( $pid == 0 ) {
38 close $in;
39  
40 POSIX::dup2( fileno($out), fileno(STDIN) ) or die("Unable to dup2(): $!");
41  
42 chdir $tmpdir or die("Unable to chdir() to $tmpdir: $!");
43  
44 exec_tar( 'mpzxf', '-' );
45 }
46  
47 close $out;
48  
49 while ( my $len = read( DATA, my $buf, 4081 ) ) {
50 my $decoded = MIME::Base64::decode_base64($buf);
51  
52 syswrite( $in, $decoded ) or die("Failed to syswrite(): $!");
53 }
54  
55 close $in;
56  
57 waitpid( $pid, 0 );
58  
59 my $status = $? >> 8;
60  
61 return $status == 0;
62 }
63  
64 sub run {
65 my ( $tmpdir, @args ) = @_;
66 my $main = "$tmpdir/scripts/main.pl";
67 my $lib = "$tmpdir/lib";
68  
69 my $pid = fork();
70  
71 if ( $pid == 0 ) {
72 $ENV{'PERL5LIB'} = defined $ENV{'PERL5LIB'}?
73 "$lib:$ENV{'PERL5LIB'}":
74 $lib;
75  
76 exec( $^X, $main, @args ) or die("Unable to exec() $main: $!");
77 }
78 elsif ( !defined($pid) ) {
79 die("Unable to fork(): $!");
80 }
81  
82 waitpid( $pid, 0 );
83  
84 return $? >> 8;
85 }
86  
87 my $tmpdir = File::Temp::tempdir( 'CLEANUP' => 1 ) or die("Cannot create temporary directory: $!");
88  
89 extract_to($tmpdir) or die("Unable to extract to $tmpdir");
90  
91 exit run( $tmpdir, @ARGV );
92  
93 __DATA__