File::Which - Portable implementation of the `which' utility
use File::Which; # exports which() use File::Which qw(which where); # exports which() and where() my $exe_path = which('perldoc'); my @paths = where('perl'); - Or - my @paths = which('perl', {all => 1 });
File::Which was created to be able to get the paths to executable programs on systems under which the `which' program wasn't implemented in the shell.
File::Which
File::Which searches the directories of the user's PATH (as returned by File::Spec->path()), looking for executable files having the name specified as a parameter to which(). Under Win32 systems, which do not have a notion of directly executable files, but uses special extensions such as .exe and .bat to identify them, File::Which takes extra steps to assure that you will find the correct file (so for example, you might be searching for perl, it'll try perl.exe, perl.bat, etc.)
PATH
File::Spec->path()
which()
.exe
.bat
perl
perl.exe
perl.bat
Windows NT has a special environment variable called PATHEXT, which is used by the shell to look for executable files. Usually, it will contain a list in the form .EXE;.BAT;.COM;.JS;.VBS etc. If File::Which finds such an environment variable, it parses the list and uses it as the different extensions.
PATHEXT
.EXE;.BAT;.COM;.JS;.VBS
This set of operating systems don't have the PATHEXT variable, and usually you will find executable files there with the extensions .exe, .bat and (less likely) .com. File::Which uses this hardcoded list if it's running under Win32 but does not find a PATHEXT variable.
.com
Exported by default.
$short_exe_name is the name used in the shell to call the program (for example, perl).
$short_exe_name
If it finds an executable with the name you specified, which() will return the absolute path leading to this executable (for example, /usr/bin/perl or C:\Perl\Bin\perl.exe).
/usr/bin/perl
C:\Perl\Bin\perl.exe
if it does not find the executable, it returns the empty string.
which() also accepts a hash reference with options:
all: if set to 1, which() will return a list of all the executable paths it finds, and not just the first match. See where().
where()
Not exported by default.
Same as which($short_exe_name, { all => 1 }). Same as the `where' utility, will return an array containing all the path names matching $short_exe_name.
which($short_exe_name, { all => 1 })
`where'
Has not been tested under MacOS. If anyone could give me the information needed for it to work on the Mac (how it searches the path, etc... although MacOs < X don't have a shell, so this might not really apply).
Per Einar Ellefsen, <per.einar (at) skynet.be>
Originated in modperl-2.0/lib/Apache/Build.pm. Changed for use in DocSet (for the mod_perl site) and Win32-awareness by me, with slight modifications by Stas Bekman, then extracted to create File::Which.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
File::Spec, File::HomeDir.
To install File::Which, copy and paste the appropriate command in to your terminal.
cpanm
cpanm File::Which
CPAN shell
perl -MCPAN -e shell install File::Which
For more information on module installation, please visit the detailed CPAN module installation guide.