| 1 | #!/usr/bin/perl | ||||||||||||||||||
| 2 | 1 | ▶ | use v5.10; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | |||||||||||||||||||
| 4 | package App::rhich; | ||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 1 | our $VERSION = '1.007'; | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | =encoding utf8 | ||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | App::rhich - which(1) with a Perl regex | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | Run this program like you would which(1), but give is a Perl regex. Even | ||||||||||||||||||
| 20 | a sequence is a regex. | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | % rhich perl | ||||||||||||||||||
| 23 | % rhich 'p.*rl' | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | rhich(1) goes through the directories listed in PATH and lists files | ||||||||||||||||||
| 28 | that match the regular expression given as the argument. This module file | ||||||||||||||||||
| 29 | is a modulino that can act as both a script and a module. | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =head2 Funtions | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | =over 4 | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | =item * run() | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | Takes no arguments but does all the work. | ||||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | =back | ||||||||||||||||||
| 40 | |||||||||||||||||||
| 41 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | Copyright © 2013-2025, brian d foy <briandfoy@pobox.com>. All rights reserved. | ||||||||||||||||||
| 44 | |||||||||||||||||||
| 45 | You may use this under the terms of the Artistic License 2.0. | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | =head1 AUTHOR | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | brian d foy, C<< <briandfoy@pobox.com> >> | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | =cut | ||||||||||||||||||
| 52 | |||||||||||||||||||
| 53 | 1 | ▶ | use File::Spec; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 54 | |||||||||||||||||||
| 55 | 1 | ▶ | run() unless caller; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 0 | ▶ | sub run { | ||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||
| 58 | 0 | ▶ | unless( defined $ARGV[0] ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 59 | 0 | warn "Need a pattern to search!\n"; | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | 0 | my $regex = eval { qr/$ARGV[0]/ }; | |||||||||||||||||
| 62 | 0 | ▶ | unless( defined $regex ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | 0 | die "Could not compile regex! $@\n"; | |||||||||||||||||
| 64 | } | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | # XXX: do some regex cleaning here | ||||||||||||||||||
| 67 | # take out (?{}) and (?{{}}) | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 0 | my @paths = _get_path_components(); | |||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 0 | foreach my $path ( @paths ) { | |||||||||||||||||
| 73 | 0 | ▶ | if( ! -e $path ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 74 | 0 | warn "$0: path $path does not exist\n"; | |||||||||||||||||
| 75 | 0 | next; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | elsif( ! -d $path ) { | ||||||||||||||||||
| 78 | 0 | warn "$0: path $path is not a directory\n"; | |||||||||||||||||
| 79 | 0 | next; | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | elsif( opendir my $dh, $path ) { | ||||||||||||||||||
| 82 | my @commands = | ||||||||||||||||||
| 83 | map { | ||||||||||||||||||
| 84 | 0 | ▶ | if( -l ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 85 | 0 | my $target = readlink; | |||||||||||||||||
| 86 | 0 | "$_ -> $target"; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | 0 | else { $_ } | |||||||||||||||||
| 89 | } | ||||||||||||||||||
| 90 | 0 | grep { -x } | |||||||||||||||||
| 91 | 0 | map { File::Spec->catfile( $path, $_ ) } | |||||||||||||||||
| 92 | 0 | grep { /$regex/ } | |||||||||||||||||
| 93 | readdir $dh; | ||||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 0 | ▶ | next unless @commands; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 0 | print join "\n", @commands, ''; | |||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | else { | ||||||||||||||||||
| 100 | 0 | warn "$0: could not read directory for $path: $!\n"; | |||||||||||||||||
| 101 | } | ||||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | } | ||||||||||||||||||
| 104 | |||||||||||||||||||
| 105 | 0 | ▶ | sub _get_path_components { | ||||||||||||||||
|
Subroutine
_get_path_components: not called CC=2 SCAR=10.1
| |||||||||||||||||||
| 106 | 1 | ▶ | use Config; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 107 | 0 | ▶ | my $separator = $Config{path_sep} // ':'; | ||||||||||||||||
|
Condition0%0/2
$App::rhich::Config{"path_sep"} // ":"
MC/DC0%0/1
$App::rhich::Config{"path_sep"} // ":"
$App::rhich::Config{"path_sep"}
Truth table0%0/2
A
A= $App::rhich::Config{"path_sep"}
| |||||||||||||||||||
| 108 | 0 | my @parts = split /$separator/, $ENV{PATH}; | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 1 | 1; | |||||||||||||||||