App-pwhich

 view release on metacpan or  search on metacpan

lib/App/pwhich.pm  view on Meta::CPAN

package App::pwhich;

use strict;
use warnings;
use 5.008001;
use File::Which qw( which );
use Getopt::Std qw( getopts );   ## no critic (Community::PreferredAlternatives)

# ABSTRACT: Perl-only `which`
our $VERSION = '1.17'; # VERSION

sub main
{
  local @ARGV;
  my $prog;
  ($prog, @ARGV) = @_;

  my %opts;

  if($prog eq 'which')
  {
    getopts('avs', \%opts) || return _usage();
    return _version() if $opts{v};
    return _usage() unless @ARGV;
  }
  else
  {
    $opts{a} = 1;
  }

  foreach my $file (@ARGV)
  {
    local $File::Which::IMPLICIT_CURRENT_DIR = $File::Which::IMPLICIT_CURRENT_DIR;
    if($^O eq 'MSWin32' && eval { require Shell::Guess; 1 })
    {
      $File::Which::IMPLICIT_CURRENT_DIR = !Shell::Guess->running_shell->is_power;
    }
    my @result = $opts{a}
    ? which($file)
    : scalar which($file);

    # We might end up with @result = (undef) -> 1 elem
    @result = () unless defined $result[0];
    unless($opts{s})
    {
      print "$_\n" for grep { defined } @result;
    }

    unless (@result)
    {
      print STDERR "$0: no $file in PATH\n" unless $opts{s};
      return 1;
    }
  }

  return 0;
}

sub _version
{
  my $my_version = $App::pwhich::VERSION || 'dev';
  print <<"END_TEXT";
This is pwhich running File::Which version $File::Which::VERSION
                       App::pwhich version $my_version

Copyright 2002 Per Einar Ellefsen

Some parts Copyright 2009 Adam Kennedy

Other parts Copyright 2015 Graham Ollis

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
END_TEXT



( run in 0.875 second using v1.01-cache-2.11-cpan-5a3173703d6 )