lib/ExtUtils/PL2Bat.pm

filter: Statementstmt 37.1% 26 / 70 Branchbran 2.9% 1 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 27.3% 35 / 128 CC 23 SCAR 57.8
CC
23
Cov
24%
pl2bat
462.0
import
3.0
BEGIN
1.0
CRAP 323.9
↑ Summary script/pl2bat.pl »
1 package ExtUtils::PL2Bat;
2 2 $ExtUtils::PL2Bat::VERSION = '0.005';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # In core, we can't use any other modules except those that already live in
12 # lib/, so Exporter is not available to us.
13 sub import {
14 1 ▶ my ($self, @functions) = @_;
Subroutine import: called CC=3 SCAR=11.0
15 1 ▶ @functions = 'pl2bat' if not @functions;
Branch: 1 branch
BranchTrueFalseTotal
unless @functions 1 0 1
16 1 my $caller = caller;
17 1 for my $function (@functions) {
18 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 *{"$caller\::$function"} = \&{$function};
20 }
21 }
22  
23 sub pl2bat {
24 0 ▶ my %opts = @_;
Subroutine pl2bat: not called CC=21 SCAR=61.4
Pod documented
25  
26 # NOTE: %0 is already enclosed in doublequotes by cmd.exe, as appropriate
27 0 ▶ $opts{ntargs} = '-x -S %0 %*' unless exists $opts{ntargs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"ntargs"} 0 0 0
28 0 ▶ $opts{otherargs} = '-x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9' unless exists $opts{otherargs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"otherargs"} 0 0 0
29  
30 0 ▶ $opts{stripsuffix} = qr/\.plx?/ unless exists $opts{stripsuffix};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"stripsuffix"} 0 0 0
31  
32 0 ▶ if (not exists $opts{out}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts{"out"}) 0 0 0
33 0 $opts{out} = $opts{in};
34 0 $opts{out} =~ s/$opts{stripsuffix}$//i;
35 0 ▶ $opts{out} .= '.bat' unless $opts{in} =~ /\.bat$/i or $opts{in} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"in"} =~ /\.bat$/i or $opts{"in"} eq "-" 0 0 0
Condition0%0/3
$opts{"in"} =~ /\.bat$/i or $opts{"in"} eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"in"} =~ /\.bat$/i or $opts{"in"} eq "-"
$opts{"in"} =~ /\.bat$/i $opts{"in"} eq "-"
Truth table0%0/3
A or B
A= $opts{"in"} =~ /\.bat$/iB= $opts{"in"} eq "-"
ABresult
1X1
011
000
36 }
37  
38 0 my $head = <<"EOT";
39 \@rem = '--*-Perl-*--
40 \@set "ErrorLevel="
41 \@if "%OS%" == "Windows_NT" \@goto WinNT
42 \@perl $opts{otherargs}
43 \@set ErrorLevel=%ErrorLevel%
44 \@goto endofperl
45 :WinNT
46 \@perl $opts{ntargs}
47 \@set ErrorLevel=%ErrorLevel%
48 \@if NOT "%COMSPEC%" == "%SystemRoot%\\system32\\cmd.exe" \@goto endofperl
49 \@if %ErrorLevel% == 9009 \@echo You do not have Perl in your PATH.
50 \@goto endofperl
51 \@rem ';
52 EOT
53  
54 0 $head =~ s/^\s+//gm;
55 0 my $headlines = 2 + ($head =~ tr/\n/\n/);
56 0 my $tail = <<'EOT';
57 __END__
58 :endofperl
59 @set "ErrorLevel=" & @goto _undefined_label_ 2>NUL || @"%COMSPEC%" /d/c @exit %ErrorLevel%
60 EOT
61 0 $tail =~ s/^\s+//gm;
62  
63 0 my $linedone = 0;
64 0 my $taildone = 0;
65 0 my $linenum = 0;
66 0 my $skiplines = 0;
67  
68 0 my $start = $Config{startperl};
69 0 ▶ $start = '#!perl' unless $start =~ /^#!.*perl/;
Branch: 1 branch
BranchTrueFalseTotal
unless $start =~ /^#!.*perl/ 0 0 0
70  
71 0 ▶ open my $in, '<', $opts{in} or croak "Can't open $opts{in}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $opts{"in"} or croak(qq[Can't open $opts{"in"}: $!]) 0 0 0
72 0 my @file = <$in>;
73 0 close $in;
74  
75 0 foreach my $line ( @file ) {
76 0 $linenum++;
77 0 ▶ if ( $line =~ /^:endofperl\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^:endofperl\b/) 0 0 0
78 0 ▶ if (!exists $opts{update}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts{"update"}) 0 0 0
79 0 warn "$opts{in} has already been converted to a batch file!\n";
80 0 return;
81 }
82 0 $taildone++;
83 }
84 0 ▶ if ( not $linedone and $line =~ /^#!.*perl/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $linedone and $line =~ /^#!.*perl/) 0 0 0
Condition0%0/3
not $linedone and $line =~ /^#!.*perl/
!ll&&!rl&&r
000
MC/DC0%0/2
not $linedone and $line =~ /^#!.*perl/
not $linedone $line =~ /^#!.*perl/
Truth table0%0/3
not(A) and B
A= not $linedoneB= $line =~ /^#!.*perl/
ABresult
0X0
100
111
85 0 ▶ if (exists $opts{update}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{'update'}) { } 0 0 0
86 0 $skiplines = $linenum - 1;
87 0 $line .= '#line '.(1+$headlines)."\n";
88 } else {
89 0 $line .= '#line '.($linenum+$headlines)."\n";
90 }
91 0 $linedone++;
92 }
93 0 ▶ if ( $line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines) 0 0 0
Condition0%0/3
$line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^#\s*line\b/ and $linenum == 2 + $skiplines
$line =~ /^#\s*line\b/ $linenum == 2 + $skiplines
Truth table0%0/3
A and B
A= $line =~ /^#\s*line\b/B= $linenum == 2 + $skiplines
ABresult
0X0
100
111
94 0 $line = '';
95 }
96 }
97  
98 0 ▶ open my $out, '>', $opts{out} or croak "Can't open $opts{out}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", $opts{"out"} or croak(qq[Can't open $opts{"out"}: $!]) 0 0 0
99 0 print $out $head;
100 0 ▶ print $out $start, ( $opts{usewarnings} ? ' -w' : '' ),
Branch: 2 branches
BranchTrueFalseTotal
unless $linedone 0 0 0
$opts{'usewarnings'} ? : 0 0 0
101 "\n#line ", ($headlines+1), "\n" unless $linedone;
102 0 print $out @file[$skiplines..$#file];
103 0 ▶ print $out $tail unless $taildone;
Branch: 1 branch
BranchTrueFalseTotal
unless $taildone 0 0 0
104 0 close $out;
105  
106 0 return $opts{out};
107 }
108  
109 2 1;
110  
111 # ABSTRACT: Batch file creation to run perl scripts on Windows
112  
113 __END__
↑ Summary script/pl2bat.pl »