1File::Which(3) User Contributed Perl Documentation File::Which(3)
2
3
4
6 File::Which - Perl implementation of the which utility as an API
7
9 version 1.22
10
12 use File::Which; # exports which()
13 use File::Which qw(which where); # exports which() and where()
14
15 my $exe_path = which 'perldoc';
16
17 my @paths = where 'perl';
18 # Or
19 my @paths = which 'perl'; # an array forces search for all of them
20
22 File::Which finds the full or relative paths to executable programs on
23 the system. This is normally the function of "which" utility. "which"
24 is typically implemented as either a program or a built in shell
25 command. On some platforms, such as Microsoft Windows it is not
26 provided as part of the core operating system. This module provides a
27 consistent API to this functionality regardless of the underlying
28 platform.
29
30 The focus of this module is correctness and portability. As a
31 consequence platforms where the current directory is implicitly part of
32 the search path such as Microsoft Windows will find executables in the
33 current directory, whereas on platforms such as UNIX where this is not
34 the case executables in the current directory will only be found if the
35 current directory is explicitly added to the path.
36
37 If you need a portable "which" on the command line in an environment
38 that does not provide it, install App::pwhich which provides a command
39 line interface to this API.
40
41 Implementations
42 File::Which searches the directories of the user's "PATH" (the current
43 implementation uses File::Spec#path to determine the correct "PATH"),
44 looking for executable files having the name specified as a parameter
45 to "which". Under Win32 systems, which do not have a notion of directly
46 executable files, but uses special extensions such as ".exe" and ".bat"
47 to identify them, "File::Which" takes extra steps to assure that you
48 will find the correct file (so for example, you might be searching for
49 "perl", it'll try perl.exe, perl.bat, etc.)
50
51 Linux, *BSD and other UNIXes
52
53 There should not be any surprises here. The current directory will not
54 be searched unless it is explicitly added to the path.
55
56 Modern Windows (including NT, XP, Vista, 7, 8, 10 etc)
57
58 Windows NT has a special environment variable called "PATHEXT", which
59 is used by the shell to look for executable files. Usually, it will
60 contain a list in the form ".EXE;.BAT;.COM;.JS;.VBS" etc. If
61 "File::Which" finds such an environment variable, it parses the list
62 and uses it as the different extensions.
63
64 Cygwin
65
66 Cygwin provides a Unix-like environment for Microsoft Windows users.
67 In most ways it works like other Unix and Unix-like environments, but
68 in a few key aspects it works like Windows. As with other Unix
69 environments, the current directory is not included in the search
70 unless it is explicitly included in the search path. Like on Windows,
71 files with ".EXE" or <.BAT> extensions will be discovered even if they
72 are not part of the query. ".COM" or extensions specified using the
73 "PATHEXT" environment variable will NOT be discovered without the fully
74 qualified name, however.
75
76 Windows 95, 98, ME, MS-DOS, OS/2
77
78 This set of operating systems don't have the "PATHEXT" variable, and
79 usually you will find executable files there with the extensions
80 ".exe", ".bat" and (less likely) ".com". "File::Which" uses this
81 hardcoded list if it's running under Win32 but does not find a
82 "PATHEXT" variable.
83
84 As of 2015 none of these platforms are tested frequently (or perhaps
85 ever), but the current maintainer is determined not to intentionally
86 remove support for older operating systems.
87
88 VMS
89
90 Same case as Windows 9x: uses ".exe" and ".com" (in that order).
91
92 As of 2015 the current maintainer does not test on VMS, and is in fact
93 not certain it has ever been tested on VMS. If this platform is
94 important to you and you can help me verify and or support it on that
95 platform please contact me.
96
98 which
99 my $path = which $short_exe_name;
100 my @paths = which $short_exe_name;
101
102 Exported by default.
103
104 $short_exe_name is the name used in the shell to call the program (for
105 example, "perl").
106
107 If it finds an executable with the name you specified, "which()" will
108 return the absolute path leading to this executable (for example,
109 /usr/bin/perl or C:\Perl\Bin\perl.exe).
110
111 If it does not find the executable, it returns "undef".
112
113 If "which()" is called in list context, it will return all the matches.
114
115 where
116 my @paths = where $short_exe_name;
117
118 Not exported by default.
119
120 Same as "which" in array context. Same as the "where" utility, will
121 return an array containing all the path names matching $short_exe_name.
122
124 This module has no non-core requirements for Perl 5.6.2 and better.
125
126 This module is fully supported back to Perl 5.8.1. It may work on
127 5.8.0. It should work on Perl 5.6.x and I may even test on 5.6.2. I
128 will accept patches to maintain compatibility for such older Perls, but
129 you may need to fix it on 5.6.x / 5.8.0 and send me a patch.
130
131 Not tested on VMS although there is platform specific code for those.
132 Anyone who haves a second would be very kind to send me a report of how
133 it went.
134
136 Bugs should be reported via the GitHub issue tracker
137
138 <https://github.com/plicease/File-Which/issues>
139
140 For other issues, contact the maintainer.
141
143 pwhich, App::pwhich
144 Command line interface to this module.
145
146 IPC::Cmd
147 Comes with a "can_run" function with slightly different semantics
148 that the traditional UNIX where. It will find executables in the
149 current directory, even though the current directory is not
150 searched for by default on Unix.
151
152 Devel::CheckBin
153 This module purports to "check that a command is available", but
154 does not provide any documentation on how you might use it.
155
157 · Per Einar Ellefsen <pereinar@cpan.org>
158
159 · Adam Kennedy <adamk@cpan.org>
160
161 · Graham Ollis <plicease@cpan.org>
162
164 This software is copyright (c) 2002 by Per Einar Ellefsen
165 <pereinar@cpan.org>.
166
167 This is free software; you can redistribute it and/or modify it under
168 the same terms as the Perl 5 programming language system itself.
169
170
171
172perl v5.26.3 2017-09-08 File::Which(3)