blib/lib/File/Which.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 90.0% 9 / 10 Conditioncond 50.0% 4 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 89.7% 79 / 88 CC 11 SCAR 15.7
CC
11
Cov
92%
which
9.0
BEGIN
3.8
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package File::Which;
2  
3 3 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 BEGIN {
10 3 ▶ $VERSION = '1.06_01';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 $VERSION = eval $VERSION; # Temporary
12 3 @ISA = 'Exporter';
13 3 @EXPORT = 'which';
14 3 @EXPORT_OK = 'where';
15 }
16  
17 3 ▶use constant IS_VMS => ($^O eq 'VMS');
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use constant IS_MAC => ($^O eq 'MacOS');
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use constant IS_DOS => ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');
Subroutine BEGIN: called CC=3 SCAR=13.3
Condition33%2/6
$^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'os2'
l!l&&r!l&&!r
003
$^O eq "MSWin32" or $^O eq "dos"
l!l&&r!l&&!r
003
MC/DC0%0/3
$^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'os2'
$^O eq "MSWin32" $^O eq "dos" $^O eq 'os2'
Truth table25%1/4
A or B || C
A= $^O eq "MSWin32"B= $^O eq "dos"C= $^O eq 'os2'
ABCresult
1XX1
01X1
0011
0000
20  
21 # For Win32 systems, stores the extensions used for
22 # executable files
23 # For others, the empty string is used
24 # because 'perl' . '' eq 'perl' => easier
25 3 my @PATHEXT = ('');
26 3 if ( IS_DOS ) {
27 # WinNT. PATHEXT might be set on Cygwin, but not used.
28 if ( $ENV{PATHEXT} ) {
29 push @PATHEXT, split ';', $ENV{PATHEXT};
30 } else {
31 # Win9X or other: doesn't have PATHEXT, so needs hardcoded.
32 push @PATHEXT, qw{.com .exe .bat};
33 }
34 0 } elsif ( IS_VMS ) {
35 push @PATHEXT, qw{.exe .com};
36 }
37  
38 sub which {
39 5 ▶ my ($exec) = @_;
Subroutine which: called CC=9 SCAR=22.0
Pod documented
40  
41 5 ▶ return undef unless $exec;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec 1 4 5
42  
43 4 my $all = wantarray;
44 4 my @results = ();
45  
46 # check for aliases first
47 4 if ( IS_VMS ) {
48 my $symbol = `SHOW SYMBOL $exec`;
49 chomp($symbol);
50 unless ( $? ) {
51 return $symbol unless $all;
52 push @results, $symbol;
53 }
54 }
55 4 if ( IS_MAC ) {
56 my @aliases = split /\,/, $ENV{Aliases};
57 foreach my $alias ( @aliases ) {
58 # This has not been tested!!
59 # PPT which says MPW-Perl cannot resolve `Alias $alias`,
60 # let's just hope it's fixed
61 if ( lc($alias) eq lc($exec) ) {
62 chomp(my $file = `Alias $alias`);
63 last unless $file; # if it failed, just go on the normal way
64 return $file unless $all;
65 push @results, $file;
66 # we can stop this loop as if it finds more aliases matching,
67 # it'll just be the same result anyway
68 last;
69 }
70 }
71 }
72  
73 4 my @path = File::Spec->path;
74 4 if ( IS_DOS or IS_VMS or IS_MAC ) {
75 unshift @path, File::Spec->curdir;
76 }
77  
78 4 foreach my $base ( map { File::Spec->catfile($_, $exec) } @path ) {
79 12 for my $ext ( @PATHEXT ) {
80 12 my $file = $base.$ext;
81  
82 # We don't want dirs (as they are -x)
83 12 ▶ next if -d $file;
Branch: 1 branch
BranchTrueFalseTotal
if -d $file 0 12 12
84  
85 12 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (-x _ or !!0) 3 9 12
Condition100%2/2
-x _ or !!0
l!l
39
MC/DC100%1/1
-x _ or !!0
-x _
Truth table100%2/2
A
A= -x _
Aresult
11
00
86 # Executable, normal case
87 -x _
88 or (
89 # MacOS doesn't mark as executable so we check -e
90 IS_MAC
91 ||
92 (
93 IS_DOS
94 and
95 grep {
96 $file =~ /$_\z/i
97 } @PATHEXT[1..$#PATHEXT]
98 )
99 # DOSish systems don't pass -x on
100 # non-exe/bat/com files. so we check -e.
101 # However, we don't want to pass -e on files
102 # that aren't in PATHEXT, like README.
103 and -e _
104 )
105 ) {
106 3 ▶ return $file unless $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 1 2 3
107 2 push @results, $file;
108 }
109 }
110 }
111  
112 3 ▶ if ( $all ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all) { } 2 1 3
113 2 return @results;
114 } else {
115 1 return undef;
116 }
117 }
118  
119 sub where {
120 # force wantarray
121 1 ▶ my @res = which($_[0]);
Subroutine where: called CC=1 SCAR=0.0
Pod documented
122 1 return @res;
123 }
124  
125 3 1;
126  
127 __END__
↑ Summary