File::Which - Portable implementation of the `which' utility


File-Which documentation Contained in the File-Which distribution.

Index


Code Index:

NAME

Top

File::Which - Portable implementation of the `which' utility

SYNOPSIS

Top

  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'); # an array forces search for all of them

DESCRIPTION

Top

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 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.)

Steps Used on Win32, DOS, OS2 and VMS

Top

Windows NT

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.

Windows 9x and other ancient Win/DOS/OS2

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.

VMS

Same case as Windows 9x: uses .exe and .com (in that order).

Functions

Top

which($short_exe_name)

Exported by default.

$short_exe_name is the name used in the shell to call the program (for example, perl).

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).

If it does not find the executable, it returns undef.

If which() is called in list context, it will return all the matches.

where($short_exe_name)

Not exported by default.

Same as which($short_exe_name) in array context. Same as the `where' utility, will return an array containing all the path names matching $short_exe_name.

BUGS AND CAVEATS

Top

Not tested on VMS or MacOS, although there is platform specific code for those. Anyone who haves a second would be very kind to send me a report of how it went.

File::Spec adds the current directory to the front of PATH if on Win32, VMS or MacOS. I have no knowledge of those so don't know if the current directory is searced first or not. Could someone please tell me?

SUPPORT

Top

Bugs should be reported via the CPAN bug tracker at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Which

For other issues, contact the maintainer.

AUTHOR

Top

Adam Kennedy <adamk@cpan.org>

Per Einar Ellefsen <pereinar@cpan.org>

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.

Version 0.04 had some significant platform-related changes, taken from the Perl Power Tools `which' implementation by Abigail with enhancements from Peter Prymmer. See http://www.perl.com/language/ppt/src/which/index.html for more information.

COPYRIGHT

Top

SEE ALSO

Top

File::Spec, which(1), Perl Power Tools: http://www.perl.com/language/ppt/index.html.


File-Which documentation Contained in the File-Which distribution.

package File::Which;

use 5.004;
use strict;
use Exporter   ();
use File::Spec ();

use vars qw{$VERSION @ISA @EXPORT @EXPORT_OK};
BEGIN {
	$VERSION   = '1.09';
	@ISA       = 'Exporter';
	@EXPORT    = 'which';
	@EXPORT_OK = 'where';
}

use constant IS_VMS => ($^O eq 'VMS');
use constant IS_MAC => ($^O eq 'MacOS');
use constant IS_DOS => ($^O eq 'MSWin32' or $^O eq 'dos' or $^O eq 'os2');

# For Win32 systems, stores the extensions used for
# executable files
# For others, the empty string is used
# because 'perl' . '' eq 'perl' => easier
my @PATHEXT = ('');
if ( IS_DOS ) {
	# WinNT. PATHEXT might be set on Cygwin, but not used.
	if ( $ENV{PATHEXT} ) {
		push @PATHEXT, split ';', $ENV{PATHEXT};
	} else {
		# Win9X or other: doesn't have PATHEXT, so needs hardcoded.
		push @PATHEXT, qw{.com .exe .bat};
	}
} elsif ( IS_VMS ) {
	push @PATHEXT, qw{.exe .com};
}

sub which {
	my ($exec) = @_;

	return undef unless $exec;

	my $all = wantarray;
	my @results = ();

	# check for aliases first
	if ( IS_VMS ) {
		my $symbol = `SHOW SYMBOL $exec`;
		chomp($symbol);
		unless ( $? ) {
			return $symbol unless $all;
			push @results, $symbol;
		}
	}
	if ( IS_MAC ) {
		my @aliases = split /\,/, $ENV{Aliases};
		foreach my $alias ( @aliases ) {
			# This has not been tested!!
			# PPT which says MPW-Perl cannot resolve `Alias $alias`,
			# let's just hope it's fixed
			if ( lc($alias) eq lc($exec) ) {
				chomp(my $file = `Alias $alias`);
				last unless $file;  # if it failed, just go on the normal way
				return $file unless $all;
				push @results, $file;
				# we can stop this loop as if it finds more aliases matching,
				# it'll just be the same result anyway
				last;
			}
		}
	}

	my @path = File::Spec->path;
	if ( IS_DOS or IS_VMS or IS_MAC ) {
		unshift @path, File::Spec->curdir;
	}

	foreach my $base ( map { File::Spec->catfile($_, $exec) } @path ) {
		for my $ext ( @PATHEXT ) {
			my $file = $base.$ext;

			# We don't want dirs (as they are -x)
			next if -d $file;

			if (
				# Executable, normal case
				-x _
				or (
					# MacOS doesn't mark as executable so we check -e
					IS_MAC
					||
					(
						IS_DOS
						and
						grep {
							$file =~ /$_\z/i
						} @PATHEXT[1..$#PATHEXT]
					)
					# DOSish systems don't pass -x on
					# non-exe/bat/com files. so we check -e.
					# However, we don't want to pass -e on files
					# that aren't in PATHEXT, like README.
					and -e _
				)
			) {
				return $file unless $all;
				push @results, $file;
			}
		}
	}

	if ( $all ) {
		return @results;
	} else {
		return undef;
	}
}

sub where {
	# force wantarray
	my @res = which($_[0]);
	return @res;
}

1;

__END__