#!perl -w
use strict;
use English;
# Shift a command entry from @ARGV
my $regexp = shift || die "usage: $0 regexp\n";
# Create a hash of directories searched
my %seen = (); # avoid duplicate directory searches
# distinguish Windows and Unix ... need to add more for Mac
my $path_sep = ($OSNAME eq 'MSWin32') ? ';' : ':';
my $file_sep = ($OSNAME eq 'MSWin32') ? '\\' : '/';
# Search through each Environment Path directory for this $regexp
# that is, a Perl regular expression
foreach my $dir (split(/$path_sep/,$ENV{PATH})) {
# Avoid any duplicate path searches
next if $seen{$dir}++;
# Avoid any directories that do not exist or can not be read
opendir(DOT, $dir) || next;
# Search through the entire directory
while ($_ = readdir(DOT)) {
# Skip any non-match
next unless /$regexp/o;
# Show any matches
print "$dir$file_sep$_\n";
}
}
__END__
=head1 NAME
pathgrep.pl
=head1 DESCRIPTION
Look for regular expression within the PATH environment variable. The
PATH environment variable defines where the system looks for commands.
=head1 SYNOPSIS
perl pathgrep.pl ...
=head1 EXAMPLES
perl pathgrep.pl .exe
=head1 TODO
Add MAC separators
Eliminate double display of separators
Add a print statement at the end to show any duplicate directories
Allow quotes to pass in special characters
=head1 AUTHOR
Extended pathgrep.pl in early Perl scripts written by
Tom Christiansen. Adapted for Perl for Bioinformatics
by David Scott.
=cut