-
Notifications
You must be signed in to change notification settings - Fork 33
/
locatepm
executable file
·79 lines (67 loc) · 1.64 KB
/
locatepm
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 18 February 2012
# Edit: 08 August 2012
# https://github.com/trizen
# Find installed Perl modules matching a regular expression
use 5.014;
use File::Find qw(find);
use Getopt::Std qw(getopts);
sub usage {
die <<"HELP";
usage: perl $0 [options] 'REGEX'\n
options:
-p : print full path
-b : both: path + name
-i : case insensitive\n
example:
perl $0 -b ^File:: ^Term
HELP
}
my %opts;
getopts('pbih', \%opts);
(!@ARGV || $opts{h}) && usage();
sub reduce_dirs {
my %substring_count;
@substring_count{@_} = ();
for my $x (@_) {
for my $y (@_) {
next if $x eq $y;
if (index($x, $y) == 0) {
$substring_count{$x}++;
}
}
}
grep { !$substring_count{$_} } keys %substring_count;
}
my @dirs;
for my $dirname (@INC) {
if (-d $dirname) {
next if chr ord $dirname eq q{.};
$dirname =~ tr{/}{/}s;
chop $dirname if substr($dirname, -1) eq '/';
push @dirs, $dirname;
}
}
@dirs = reduce_dirs(@dirs);
my $inc_re = do {
local $" = q{|};
qr{^(?>@{[map { quotemeta(s{/}{::}gr) } @dirs]})::};
};
foreach my $arg (@ARGV) {
my $regex = $opts{i} ? qr{$arg}i : qr{$arg};
find {
wanted => sub {
my $name = $_;
say $opts{b} ? "$name\n$_\n"
: $opts{p} ? $_
: $name
if substr($name, -3, 3, '') eq '.pm'
and $name =~ s{/}{::}g
and $name =~ s{$inc_re}{}o
and $name =~ /$regex/;
},
no_chdir => 1,
} => @dirs;
}