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.23
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 ME, 98, 95, 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. Similar to the "where" csh built-in
121 command or "which -a" command for platforms that support the "-a"
122 option. Will return an array containing all the path names matching
123 $short_exe_name.
124
126 $IMPLICIT_CURRENT_DIR
127 True if the current directory is included in the search implicitly on
128 whatever platform you are using. Normally the default is reasonable,
129 but on Windows the current directory is included implicitly for older
130 shells like "cmd.exe" and "command.com", but not for newer shells like
131 PowerShell. If you overrule this default, you should ALWAYS localize
132 the variable to the tightest scope possible, since setting this
133 variable from a module can affect other modules. Thus on Windows you
134 can get the correct result if the user is running either "cmd.exe" or
135 PowerShell on Windows you can do this:
136
137 use File::Which qw( which );
138 use Shell::Guess;
139
140 my $path = do {
141 my $is_power = Shell::Guess->running_shell->is_power;
142 local $File::Which::IMPLICIT_CURRENT_DIR = !$is_power;
143 which 'foo';
144 };
145
146 For a variety of reasons it is difficult to accurately compute the
147 shell that a user is using, but Shell::Guess makes a reasonable effort.
148
150 This module has no non-core requirements for Perl 5.6.2 and better.
151
152 This module is fully supported back to Perl 5.8.1. It may work on
153 5.8.0. It should work on Perl 5.6.x and I may even test on 5.6.2. I
154 will accept patches to maintain compatibility for such older Perls, but
155 you may need to fix it on 5.6.x / 5.8.0 and send me a patch.
156
157 Not tested on VMS although there is platform specific code for those.
158 Anyone who haves a second would be very kind to send me a report of how
159 it went.
160
162 Bugs should be reported via the GitHub issue tracker
163
164 <https://github.com/plicease/File-Which/issues>
165
166 For other issues, contact the maintainer.
167
169 pwhich, App::pwhich
170 Command line interface to this module.
171
172 IPC::Cmd
173 This module provides (among other things) a "can_run" function,
174 which is similar to "which". It is a much heavier module since it
175 does a lot more, and if you use "can_run" it pulls in
176 ExtUtils::MakeMaker. This combination may be overkill for
177 applications which do not need IPC::Cmd's complicated interface for
178 running programs, or do not need the memory overhead required for
179 installing Perl modules.
180
181 At least some older versions will find executables in the current
182 directory, even if the current directory is not in the search path
183 (which is the default on modern Unix).
184
185 "can_run" converts directory path name to the 8.3 version on
186 Windows using "Win32::GetShortPathName" in some cases. This is
187 frequently useful for tools that just need to run something using
188 "system" in scalar mode, but may be inconvenient for tools like
189 App::pwhich where user readability is a premium. Relying on
190 "Win32::GetShortPathName" to produce filenames without spaces is
191 problematic, as 8.3 filenames can be turned off with tweaks to the
192 registry (see
193 <https://technet.microsoft.com/en-us/library/cc959352.aspx>).
194
195 Devel::CheckBin
196 This module purports to "check that a command is available", but
197 does not provide any documentation on how you might use it.
198
200 · Per Einar Ellefsen <pereinar@cpan.org>
201
202 · Adam Kennedy <adamk@cpan.org>
203
204 · Graham Ollis <plicease@cpan.org>
205
207 This software is copyright (c) 2002 by Per Einar Ellefsen
208 <pereinar@cpan.org>.
209
210 This is free software; you can redistribute it and/or modify it under
211 the same terms as the Perl 5 programming language system itself.
212
213
214
215perl v5.30.1 2020-01-30 File::Which(3)