From: Paul Hovnanian P.E. on 15 Mar 2010 15:00 cerr wrote: > Hi There, > > My perl script depends on a binary that may be in $PATH and i would > like to check for that file. How can I verify if the file is present > in $PATH without hard coding a full path? > > Thanks, > Ron No need to hardcode $PATH in your app. Just get it from %ENV as $ENV{PATH}. So sub fnd { my ($f) = @_; foreach my $p (split /:/, $ENV{PATH}) { return "$p/$f" if( -x "$p/$f" ); } return undef; } will return the full executable path for $f, or undef. You might want to clean this up a bit. -- Paul Hovnanian paul(a)hovnanian.com ---------------------------------------------------------------------- Have gnu, will travel.
From: Ben Morrow on 15 Mar 2010 17:46 Quoth "Paul Hovnanian P.E." <paul(a)hovnanian.com>: > cerr wrote: > > > My perl script depends on a binary that may be in $PATH and i would > > like to check for that file. How can I verify if the file is present > > in $PATH without hard coding a full path? > > No need to hardcode $PATH in your app. Just get it from %ENV as $ENV{PATH}. > > So > > sub fnd { > my ($f) = @_; > foreach my $p (split /:/, $ENV{PATH}) { Use File::Spec->path here, or some wrapper. Not all platforms use a :-separated PATH, and not all platforms call the pertinant env var 'PATH'. > return "$p/$f" if( -x "$p/$f" ); Ditto: File::Spec->catfile, or a wrapper like Path::Class (highly recommended). Ben
From: m!thun on 15 Mar 2010 22:32
On Mar 15, 3:46 pm, Ben Morrow <b...(a)morrow.me.uk> wrote: > Quoth "Paul Hovnanian P.E." <p...(a)hovnanian.com>: > > > cerr wrote: > > > > My perl script depends on a binary that may be in $PATH and i would > > > like to check for that file. How can I verify if the file is present > > > in $PATH without hard coding a full path? > > > No need to hardcode $PATH in your app. Just get it from %ENV as $ENV{PATH}. > > > So > > > sub fnd { > > my ($f) = @_; > > foreach my $p (split /:/, $ENV{PATH}) { > > Use File::Spec->path here, or some wrapper. Not all platforms use a > :-separated PATH, and not all platforms call the pertinant env var 'PATH'.. > > > return "$p/$f" if( -x "$p/$f" ); > > Ditto: File::Spec->catfile, or a wrapper like Path::Class (highly > recommended). > > Ben I've been using File::Which. I think this is more portable and usable than the other modules mentioned here. |