I have something like this:
if(! -e $filename) {
# do something
}
but I need to alter it so that it looks for file even on my PATH. Is there any way to achieve this without analysing PATH?
File::Which
How can you see if a file is in one of the directories specified in $ENV{PATH}
without looking at $ENV{PATH}
? … That's a rhetorical question.
Here is a short script I wrote some time ago. You should be able to adapt it for your needs:
#!/usr/bin/perl
use strict; use warnings;
use File::Basename;
use File::Spec::Functions qw( catfile path );
my $myname = fileparse $0;
die "Usage: $myname program_name\n" unless @ARGV;
my @path = path;
my @pathext = ( q{} );
if ( $^O eq 'MSWin32' ) {
push @pathext, map { lc } split /;/, $ENV{PATHEXT};
}
PROGRAM: for my $progname ( @ARGV ) {
unless ( $progname eq fileparse $progname ) {
warn "Not processed: $progname\n\tArgument is not a plain file name\n";
next PROGRAM;
}
my @results;
for my $dir ( @path ) {
for my $ext ( @pathext ) {
my $f = catfile $dir, "$progname$ext";
push @results, $f if -x $f;
}
}
print "$progname:\n";
print "\t$_\n" for @results;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With