cosmicsoup has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

How would I be able to find out whether or not a file is in my path without having to call the system 'which' command. Is there anything inherent to perl that would provide this functionality. The system calls slow down the perl script dramatically, and I would like to have a work around.

Thanks
Louis

Replies are listed 'Best First'.
Re: Inherent 'which' command for perl
by broquaint (Abbot) on Mar 05, 2004 at 15:04 UTC
    Something like this should work on most *nix platforms
    use List::Util 'first'; use File::Spec::Functions 'catfile'; sub which { my $path = first { -x } map { catfile $_ => $_[0] } split ':' => $ENV{PATH}; return $path || "$_ not found in $ENV{PATH}"; } print "path to $_ is: ", which($_), "\n" for qw/ find perl here /; __output__ path to find is: /usr/bin/find path to perl is: /usr/bin/perl path to here is: here not found in /usr/kerberos/bin:/usr/local/bin:/u +sr/bin:/bin:/usr/X11R6/bin
    HTH

    _________
    broquaint

Re: Inherent 'which' command for perl
by grinder (Bishop) on Mar 05, 2004 at 14:56 UTC
    <merlyn> I wrote a snippet once that you should be able to adapt to the task at hand </merlyn>
Re: Inherent 'which' command for perl
by Roy Johnson (Monsignor) on Mar 05, 2004 at 17:25 UTC
    There is a File::Which module on CPAN.

    The PerlMonk tr/// Advocate
Re: Inherent 'which' command for perl
by slloyd (Hermit) on Mar 05, 2004 at 20:07 UTC
    if(-s $filename){print "file exists and is non-zero size";} elsif(-e $filename){print "file exists and is zero size";}