(view source code of which.pl as plain text)
#! perl# Check command line argument(s)if ( !@ARGV[0] or @ARGV[1] or !( @ARGV[0] =~ m/^[-\w]+(\.\w+)?$/ ) ) {
print "\nWhich.pl, Version 2.00\n";
print "Locate the specified program file\n";
print "Port of Unix' WHICH command\n\n";
print "Usage: WHICH.PL filename[.ext]\n\n";
print "Where: \"filename\" is a file name only: no path, no wildcards\n\n";
print "Note: This script ignores DOSKEY macros and internal commands, and\n";
print " returns external commands (executable or script file names) only.\n\n";
print "Written by Rob van der Woude\n";
print "http://www.robvanderwoude.com\n";
exit(2);
}# Store PATH and PATHEXT in arrayswhile ( ( $key, $value ) = each %ENV ) {
if ( uc( $key ) eq "PATH" ) {
# Current directory must be searched before the PATH@path = ( ".", split( /;+/,$value ) );
} elsif ( uc( $key ) eq "PATHEXT" ) {
# Search for "non-executable" program files too@pathext = ( split( /;+/,$value ) );
}}# Search code in labelled block, to allow quitting the loopSEARCH: {
# Search each directory specified in PATHforeach ( @path ) {
$d = $_;
# Remove trailing backslash or backslash plus dot$d =~ s/^(.*)\\\.?$/\1/;
# Read the directory (files only)opendir( SEARCHDIR, $d ) or die "Cannot open directory $d:\n$!";
readdir ( SEARCHDIR );
@files = grep { -f "$d\\$_" } readdir( SEARCHDIR );
foreach $file ( @files ) {
# Check for specified file name both with # and without each extension from PATHEXTif ( @ARGV[0] =~ m/^[^.]=\.[^.]+$/ ) {
if ( $file =~ m/^@ARGV[0]$/i ) {
# Display resultprint "\n$d\\$file\n";
# Close directory handleclosedir( SEARCHDIR );
# Abort search at first successful findlast SEARCH;
}} else {
foreach $ext ( @pathext ) {
if ( $file =~ m/^@ARGV[0]($ext)$/i ) {
# Display resultprint "\n$d\\$file\n";
# Close directory handleclosedir( SEARCHDIR );
# Abort search at first successful findlast SEARCH;
} } } } # Close directory handleclosedir( SEARCHDIR );
} # If you arrived here, the search was unsuccessfulprint "\n-None-\n";
exit(1);
}page last modified: 2025-10-11; loaded in 0.0081 seconds