| 1 |
NAME |
| 2 |
|
| 3 |
File::Which - Perl implementation of the which utility as an API |
| 4 |
|
| 5 |
VERSION |
| 6 |
|
| 7 |
version 1.27 |
| 8 |
|
| 9 |
SYNOPSIS |
| 10 |
|
| 11 |
use File::Which; # exports which() |
| 12 |
use File::Which qw(which where); # exports which() and where() |
| 13 |
|
| 14 |
my $exe_path = which 'perldoc'; |
| 15 |
|
| 16 |
my @paths = where 'perl'; |
| 17 |
# Or |
| 18 |
my @paths = which 'perl'; # an array forces search for all of them |
| 19 |
|
| 20 |
DESCRIPTION |
| 21 |
|
| 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 is |
| 24 |
typically implemented as either a program or a built in shell command. |
| 25 |
On some platforms, such as Microsoft Windows it is not provided as part |
| 26 |
of the core operating system. This module provides a consistent API to |
| 27 |
this functionality regardless of the underlying platform. |
| 28 |
|
| 29 |
The focus of this module is correctness and portability. As a |
| 30 |
consequence platforms where the current directory is implicitly part of |
| 31 |
the search path such as Microsoft Windows will find executables in the |
| 32 |
current directory, whereas on platforms such as UNIX where this is not |
| 33 |
the case executables in the current directory will only be found if the |
| 34 |
current directory is explicitly added to the path. |
| 35 |
|
| 36 |
If you need a portable which on the command line in an environment that |
| 37 |
does not provide it, install App::pwhich which provides a command line |
| 38 |
interface to this API. |
| 39 |
|
| 40 |
Implementations |
| 41 |
|
| 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 to |
| 47 |
identify them, File::Which takes extra steps to assure that you will |
| 48 |
find the correct file (so for example, you might be searching for perl, |
| 49 |
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 is |
| 59 |
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 File::Which |
| 61 |
finds such an environment variable, it parses the list and uses it as |
| 62 |
the different extensions. |
| 63 |
|
| 64 |
Cygwin |
| 65 |
|
| 66 |
Cygwin provides a Unix-like environment for Microsoft Windows users. In |
| 67 |
most ways it works like other Unix and Unix-like environments, but in a |
| 68 |
few key aspects it works like Windows. As with other Unix environments, |
| 69 |
the current directory is not included in the search unless it is |
| 70 |
explicitly included in the search path. Like on Windows, files with |
| 71 |
.EXE or <.BAT> extensions will be discovered even if they are not part |
| 72 |
of the query. .COM or extensions specified using the PATHEXT |
| 73 |
environment variable will NOT be discovered without the fully qualified |
| 74 |
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 .exe, |
| 80 |
.bat and (less likely) .com. File::Which uses this hardcoded list if |
| 81 |
it's running under Win32 but does not find a PATHEXT variable. |
| 82 |
|
| 83 |
As of 2015 none of these platforms are tested frequently (or perhaps |
| 84 |
ever), but the current maintainer is determined not to intentionally |
| 85 |
remove support for older operating systems. |
| 86 |
|
| 87 |
VMS |
| 88 |
|
| 89 |
Same case as Windows 9x: uses .exe and .com (in that order). |
| 90 |
|
| 91 |
As of 2015 the current maintainer does not test on VMS, and is in fact |
| 92 |
not certain it has ever been tested on VMS. If this platform is |
| 93 |
important to you and you can help me verify and or support it on that |
| 94 |
platform please contact me. |
| 95 |
|
| 96 |
FUNCTIONS |
| 97 |
|
| 98 |
which |
| 99 |
|
| 100 |
my $path = which $short_exe_name; |
| 101 |
my @paths = which $short_exe_name; |
| 102 |
|
| 103 |
Exported by default. |
| 104 |
|
| 105 |
$short_exe_name is the name used in the shell to call the program (for |
| 106 |
example, perl). |
| 107 |
|
| 108 |
If it finds an executable with the name you specified, which() will |
| 109 |
return the absolute path leading to this executable (for example, |
| 110 |
/usr/bin/perl or C:\Perl\Bin\perl.exe). |
| 111 |
|
| 112 |
If it does not find the executable, it returns undef. |
| 113 |
|
| 114 |
If which() is called in list context, it will return all the matches. |
| 115 |
|
| 116 |
where |
| 117 |
|
| 118 |
my @paths = where $short_exe_name; |
| 119 |
|
| 120 |
Not exported by default. |
| 121 |
|
| 122 |
Same as "which" in array context. Similar to the where csh built-in |
| 123 |
command or which -a command for platforms that support the -a option. |
| 124 |
Will return an array containing all the path names matching |
| 125 |
$short_exe_name. |
| 126 |
|
| 127 |
GLOBALS |
| 128 |
|
| 129 |
$IMPLICIT_CURRENT_DIR |
| 130 |
|
| 131 |
True if the current directory is included in the search implicitly on |
| 132 |
whatever platform you are using. Normally the default is reasonable, |
| 133 |
but on Windows the current directory is included implicitly for older |
| 134 |
shells like cmd.exe and command.com, but not for newer shells like |
| 135 |
PowerShell. If you overrule this default, you should ALWAYS localize |
| 136 |
the variable to the tightest scope possible, since setting this |
| 137 |
variable from a module can affect other modules. Thus on Windows you |
| 138 |
can get the correct result if the user is running either cmd.exe or |
| 139 |
PowerShell on Windows you can do this: |
| 140 |
|
| 141 |
use File::Which qw( which ); |
| 142 |
use Shell::Guess; |
| 143 |
|
| 144 |
my $path = do { |
| 145 |
my $is_power = Shell::Guess->running_shell->is_power; |
| 146 |
local $File::Which::IMPLICIT_CURRENT_DIR = !$is_power; |
| 147 |
which 'foo'; |
| 148 |
}; |
| 149 |
|
| 150 |
For a variety of reasons it is difficult to accurately compute the |
| 151 |
shell that a user is using, but Shell::Guess makes a reasonable effort. |
| 152 |
|
| 153 |
CAVEATS |
| 154 |
|
| 155 |
This module has no non-core requirements for Perl 5.6.2 and better. |
| 156 |
|
| 157 |
This module is fully supported back to Perl 5.8.1. It may work on |
| 158 |
5.8.0. It should work on Perl 5.6.x and I may even test on 5.6.2. I |
| 159 |
will accept patches to maintain compatibility for such older Perls, but |
| 160 |
you may need to fix it on 5.6.x / 5.8.0 and send me a patch. |
| 161 |
|
| 162 |
Not tested on VMS although there is platform specific code for those. |
| 163 |
Anyone who haves a second would be very kind to send me a report of how |
| 164 |
it went. |
| 165 |
|
| 166 |
SUPPORT |
| 167 |
|
| 168 |
Bugs should be reported via the GitHub issue tracker |
| 169 |
|
| 170 |
https://github.com/uperl/File-Which/issues |
| 171 |
|
| 172 |
For other issues, contact the maintainer. |
| 173 |
|
| 174 |
SEE ALSO |
| 175 |
|
| 176 |
pwhich, App::pwhich |
| 177 |
|
| 178 |
Command line interface to this module. |
| 179 |
|
| 180 |
IPC::Cmd |
| 181 |
|
| 182 |
Requires Perl 5.8.3. Included as part of the Perl core as of 5.9.5. |
| 183 |
|
| 184 |
This module provides (among other things) a can_run function, which |
| 185 |
is similar to which. It is a much heavier module since it does a lot |
| 186 |
more, and if you use can_run it pulls in ExtUtils::MakeMaker. This |
| 187 |
combination may be overkill for applications which do not need |
| 188 |
IPC::Cmd's complicated interface for running programs, or do not need |
| 189 |
the memory overhead required for installing Perl modules. |
| 190 |
|
| 191 |
At least some older versions will find executables in the current |
| 192 |
directory, even if the current directory is not in the search path |
| 193 |
(which is the default on modern Unix). |
| 194 |
|
| 195 |
can_run converts directory path name to the 8.3 version on Windows |
| 196 |
using Win32::GetShortPathName in some cases. This is frequently |
| 197 |
useful for tools that just need to run something using system in |
| 198 |
scalar mode, but may be inconvenient for tools like App::pwhich where |
| 199 |
user readability is a premium. Relying on Win32::GetShortPathName to |
| 200 |
produce filenames without spaces is problematic, as 8.3 filenames can |
| 201 |
be turned off with tweaks to the registry (see |
| 202 |
https://technet.microsoft.com/en-us/library/cc959352.aspx). |
| 203 |
|
| 204 |
Devel::CheckBin |
| 205 |
|
| 206 |
Requires Perl 5.8.1. |
| 207 |
|
| 208 |
This module purports to "check that a command is available", but does |
| 209 |
not provide any documentation on how you might use it. |
| 210 |
|
| 211 |
This module also relies on ExtUtils::MakeMaker so has the same |
| 212 |
overhead burdens as IPC::Cmd. |
| 213 |
|
| 214 |
AUTHORS |
| 215 |
|
| 216 |
* Per Einar Ellefsen <[email protected]> |
| 217 |
|
| 218 |
* Adam Kennedy <[email protected]> |
| 219 |
|
| 220 |
* Graham Ollis <[email protected]> |
| 221 |
|
| 222 |
COPYRIGHT AND LICENSE |
| 223 |
|
| 224 |
This software is copyright (c) 2002 by Per Einar Ellefsen |
| 225 |
<[email protected]>. |
| 226 |
|
| 227 |
This is free software; you can redistribute it and/or modify it under |
| 228 |
the same terms as the Perl 5 programming language system itself. |
| 229 |
|