File Coverage

inc/File/Which.pm
Criterion Covered Total %
statement 28 49 57.1
branch 0 10 0.0
condition 1 5 20.0
subroutine 9 11 81.8
pod 2 2 100.0
total 40 77 51.9


line stmt bran cond sub pod time code
1             #line 1
2             package File::Which;
3 1     1   15  
  1         4  
  1         41  
4 1     1   6 use 5.004;
  1         2  
  1         27  
5 1     1   7 use strict;
  1         5  
  1         26  
6 1     1   5 use Exporter ();
  1         10  
  1         30  
7             use File::Spec ();
8 1     1   5  
  1         2  
  1         214  
9             use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK};
10 1     1   3 BEGIN {
11 1         18 $VERSION = '1.09';
12 1         3 @ISA = 'Exporter';
13 1         32 @EXPORT = 'which';
14             @EXPORT_OK = 'where';
15             }
16 1     1   6  
  1         2  
  1         79  
17 1     1   17 use constant IS_VMS => ($^O eq 'VMS');
  1         2  
  1         75  
18 1   33 1   5 use constant IS_MAC => ($^O eq 'MacOS');
  1         2  
  1         1018  
19             use constant IS_DOS => ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');
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             my @PATHEXT = ('');
26             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             } elsif ( IS_VMS ) {
35             push @PATHEXT, qw{.exe .com};
36             }
37              
38 0     0 1   sub which {
39             my ($exec) = @_;
40 0 0          
41             return undef unless $exec;
42 0            
43 0           my $all = wantarray;
44             my @results = ();
45              
46 0           # check for aliases first
47             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 0           }
55             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 0            
73 0           my @path = File::Spec->path;
74             if ( IS_DOS or IS_VMS or IS_MAC ) {
75             unshift @path, File::Spec->curdir;
76             }
77 0            
  0            
78 0           foreach my $base ( map { File::Spec->catfile($_, $exec) } @path ) {
79 0           for my $ext ( @PATHEXT ) {
80             my $file = $base.$ext;
81              
82 0 0         # We don't want dirs (as they are -x)
83             next if -d $file;
84 0 0 0        
85             if (
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 0 0         ) {
106 0           return $file unless $all;
107             push @results, $file;
108             }
109             }
110             }
111 0 0          
112 0           if ( $all ) {
113             return @results;
114 0           } else {
115             return undef;
116             }
117             }
118              
119             sub where {
120 0     0 1   # force wantarray
121 0           my @res = which($_[0]);
122             return @res;
123             }
124              
125             1;
126              
127             __END__