-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquale.pl
100 lines (77 loc) · 2.14 KB
/
quale.pl
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
#######################################################################################
# Name: quale.pl
# Description: Simple Perl version of standard Linux which utility
# Author: Cesare Guardino
# Last modified: 17 August 2022
#######################################################################################
use strict;
use warnings;
use constant NAME => "quale";
use constant VERSION => "0.4.4";
#use File::Which; # exports which()
use File::Which qw(which where); # exports which() and where()
use Getopt::Long;
use Pod::Usage;
# POD {{{1
=head1 NAME
quale.pl
=head1 SYNOPSIS
quale.pl [options] <program_name>
Options:
-a, --all Prints all occurrences found
-h, --help Help usage message
Optional arguments:
<program_name> Program name to find
=head1 DESCRIPTION
B<quale.pl> Simple Perl version of standard Linux which utility.
=cut
# POD }}}1
my ($opt_all, $opt_help) = undef;
GetOptions(
"all|a" => \$opt_all,
'help|?' => \$opt_help,
) or banner(2);
banner(1) if $opt_help;
# Set defaults:
$opt_all = 0 if not defined $opt_all;
if ($opt_all)
{
my @paths = where $ARGV[0];
# Or
#my @paths = which 'gmake'; # an array forces search for all of them
foreach my $exe_path (@paths)
{
print_path($exe_path);
}
}
else
{
my $exe_path = which $ARGV[0];
print_path($exe_path);
}
sub banner
{
my ($id) = @_;
my $message = NAME . " " . VERSION . ", Copyright (c) 2016-2022 Cesare Guardino";
print "\n$message\n\n";
pod2usage($id);
}
sub print_path
{
my ($exe_path) = @_;
return if not defined $exe_path;
if (posix_shell())
{
$exe_path =~ s/\\/\//g;
}
else
{
$exe_path =~ s/\//\\/g;
}
print "$exe_path\n";
}
sub posix_shell
{
return ($^O ne "MSWin32" or defined $ENV{'TERM'} and $ENV{'TERM'} =~ /cygwin|xterm/ or defined $ENV{'MSYSCON'} and $ENV{'MSYSCON'} =~ /sh/) ? 1 : 0;
}