File Coverage

blib/lib/App/rhich.pm
Criterion Covered Total %
statement 14 40 35.0
branch 0 14 0.0
condition 0 2 0.0
subroutine 5 7 71.4
pod 1 1 100.0
total 20 64 31.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 1     1   302738 use v5.10;
  1         4  
3              
4             package App::rhich;
5 1     1   5 use strict;
  1         1  
  1         47  
6 1     1   4 use warnings;
  1         2  
  1         95  
7              
8              
9             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 . 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<< >>
50              
51             =cut
52              
53 1     1   5 use File::Spec;
  1         4  
  1         435  
54              
55             run() unless caller;
56              
57             sub run {
58 0 0   0 1   unless( defined $ARGV[0] ) {
59 0           warn "Need a pattern to search!\n";
60             }
61 0           my $regex = eval { qr/$ARGV[0]/ };
  0            
62 0 0         unless( defined $regex ) {
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 0         if( ! -e $path ) {
    0          
    0          
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 0         if( -l ) {
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/ }
  0            
93             readdir $dh;
94              
95 0 0         next unless @commands;
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             sub _get_path_components {
106 1     1   7 use Config;
  1         2  
  1         117  
107 0   0 0     my $separator = $Config{path_sep} // ':';
108 0           my @parts = split /$separator/, $ENV{PATH};
109             }
110              
111             1;