check for file in $PATH

Discussion in 'Perl Misc' started by cerr, Mar 15, 2010.

  1. cerr

    cerr Guest

    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
    cerr, Mar 15, 2010
    #1
    1. Advertising

  2. cerr

    Frank Seitz Guest

    Frank Seitz, Mar 15, 2010
    #2
    1. Advertising

  3. 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?


    I suppose you could generalize FindBin to search for arbitrary files,
    not just the perl binary.

    Or simply use the external "which" utility.

    Or just call it, maybe with innocent arguments, and analyse the return
    value for the error code.

    jue
    Jürgen Exner, Mar 15, 2010
    #3
  4. cerr

    cerr Guest

    On Mar 15, 10:42 am, Frank Seitz <> wrote:
    > 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?

    >
    > http://search.cpan.org/~tjenness/File-SearchPath/SearchPath.pm


    Great! this is doing the job, thanks!
    --
    Ron
    cerr, Mar 15, 2010
    #4
  5. Frank Seitz <> wrote:
    >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?

    >
    >http://search.cpan.org/~tjenness/File-SearchPath/SearchPath.pm


    Much better than my idea ;-)

    jue
    Jürgen Exner, Mar 15, 2010
    #5
  6. cerr

    m!thun Guest

    On Mar 15, 3:46 pm, Ben Morrow <> wrote:
    > Quoth "Paul Hovnanian P.E." <>:
    >
    > > 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.
    m!thun, Mar 16, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    2
    Views:
    2,333
    brucie
    Oct 16, 2004
  2. Maciej Sobczak
    Replies:
    9
    Views:
    537
    Roger Binns
    Apr 25, 2004
  3. Alessandro
    Replies:
    5
    Views:
    557
    Alessandro
    Sep 27, 2010
  4. Matt
    Replies:
    3
    Views:
    134
    Grant Wagner
    Sep 17, 2004
  5. Matt
    Replies:
    3
    Views:
    91
    bruce
    Sep 22, 2004
Loading...

Share This Page