1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
use v6;
unit class File::Which::Unix;
method which(Str $exec, Bool :$all = False) {
return Any unless $exec;
my @results;
return $exec if $exec ~~ /\// && $exec.IO ~~ :f && $exec.IO ~~ :x;
my @path = flat( $*SPEC.path );
my @PATHEXT = '';
for @path.map({ $*SPEC.catfile($_, $exec) }) -> $file {
# Ignore possibly -x directories
next if $file.IO ~~ :d;
# Executable, normal case
if $file.IO ~~ :x {
if $all {
@results.push( $file );
} else {
return $file;
}
}
}
return @results.unique if $all;
return Any;
}
=begin pod
=head1 NAME
File::Which::Unix - Linux/Unix which implementation
=head1 SYNOPSIS
use File::Which::Unix;
my $o = File::Which::Unix.new;
say $o.which('raku');
=head1 DESCRIPTION
Implements the which method under UNIX-based platforms
=head1 AUTHOR
Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright 2016 Ahmad M. Zawawi
This library is free software; you can redistribute it and/or modify it under
the MIT License
=end pod
|