blib/lib/Unix/Whereis.pm

filter: Statementstmt 100% 70 / 70 Branchbran 97.6% 41 / 42 Conditioncond 83.3% 45 / 54 MC/DCmcdc 69.7% 23 / 33 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 90.5% 191 / 211 CC 42 SCAR 23.9
CC
42
Cov
94%
whereis
16.3
whereis_everyone
13.0
_build_env_path
8.0
CRAP 10.9
↑ Summary
1 package Unix::Whereis;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $Unix::Whereis::VERSION = '0.1';
7  
8 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $sep;
11 2 our $cache;
12  
13 3 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
14 3 my $caller = caller();
15 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 *{ $caller . '::whereis' } = \&whereis;
17  
18 3 ▶ if ( defined $_[1] && $_[1] eq 'whereis_everyone' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and $_[1] eq "whereis_everyone") 1 2 3
Condition66%2/3
defined $_[1] and $_[1] eq "whereis_everyone"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $_[1] and $_[1] eq "whereis_everyone"
defined $_[1] $_[1] eq "whereis_everyone"
Truth table66%2/3
A and B
A= defined $_[1]B= $_[1] eq "whereis_everyone"
ABresult
0X0
100
111
19 1 *{ $caller . '::whereis_everyone' } = \&whereis_everyone;
20 }
21 }
22  
23 35 ▶sub pathsep {
Subroutine pathsep: called CC=6 SCAR=17.9
Pod documented
24 35 ▶ $sep = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 5 30 35
25  
26 35 ▶ if ( !defined $sep ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Unix::Whereis::sep) 6 29 35
27  
28 # only use %Config::Config if loaded already (keys() is the key!):
29 # pkit -e 'd(scalar keys %Config::Config);require Config;d(scalar keys %Config::Config)'
30 # pkit -e 'd(scalar keys %Config::Config)'
31 # pkit -MConfig -e 'd(scalar keys %Config::Config)'
32 6 ▶ if ( keys %Config::Config && exists $Config::Config{'path_sep'} && length $Config::Config{'path_sep'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %Config::Config and exists $Config::Config{'path_sep'} and length $Config::Config{'path_sep'}) { } 1 5 6
Condition83%5/6
keys %Config::Config and exists $Config::Config{"path_sep"} and length $Config::Config{"path_sep"}
!ll&&!rl&&r
321
keys %Config::Config and exists $Config::Config{"path_sep"}
!ll&&!rl&&r
303
MC/DC66%2/3
keys %Config::Config and exists $Config::Config{"path_sep"} and length $Config::Config{"path_sep"}
keys %Config::Config exists $Config::Config{"path_sep"} length $Config::Config{"path_sep"}
Truth table75%3/4
A and B and C
A= keys %Config::ConfigB= exists $Config::Config{"path_sep"}C= length $Config::Config{"path_sep"}
ABCresult
0XX0
10X0
1100
1111
33 1 $sep = $Config::Config{'path_sep'};
34 }
35 else {
36 5 $sep = ':';
37 }
38 }
39  
40 35 return $sep;
41 }
42  
43 11 ▶sub whereis {
Subroutine whereis: called CC=16 SCAR=27.9
Pod documented
44 11 my ( $name, $conf_hr ) = @_;
45 11 ▶ return if !defined $name || $name eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $name or $name eq "" 0 11 11
Condition33%1/3
not defined $name or $name eq ""
l!l&&r!l&&!r
0011
MC/DC0%0/2
not defined $name or $name eq ""
not defined $name $name eq ""
Truth table33%1/3
not(A) or B
A= not defined $nameB= $name eq ""
ABresult
1X1
011
000
46  
47 11 ▶ $conf_hr = {} if !defined $conf_hr || ref $conf_hr ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $conf_hr or ref $conf_hr ne "HASH" 6 5 11
Condition66%2/3
not defined $conf_hr or ref $conf_hr ne "HASH"
l!l&&r!l&&!r
605
MC/DC50%1/2
not defined $conf_hr or ref $conf_hr ne "HASH"
not defined $conf_hr ref $conf_hr ne "HASH"
Truth table66%2/3
not(A) or B
A= not defined $conf_hrB= ref $conf_hr ne "HASH"
ABresult
1X1
011
000
48  
49 11 ▶ if ( $conf_hr->{'cache'} && $cache->{'whereis'}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis"}{$name}) 2 9 11
Condition100%3/3
$conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis"}{$name}
!ll&&!rl&&r
812
MC/DC100%2/2
$conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis"}{$name}
$conf_hr->{"cache"} $Unix::Whereis::cache->{"whereis"}{$name}
Truth table100%3/3
A and B
A= $conf_hr->{"cache"}B= $Unix::Whereis::cache->{"whereis"}{$name}
ABresult
0X0
100
111
50 2 ▶ delete $cache->{'whereis'}{$name} if $conf_hr->{'cache'} eq 'clear';
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} eq "clear" 1 1 2
51 2 ▶ return $cache->{'whereis'}{$name} if $conf_hr->{'cache'} && exists $cache->{'whereis'}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis"}{$name} 1 1 2
Condition66%2/3
$conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis"}{$name}
!ll&&!rl&&r
011
MC/DC50%1/2
$conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis"}{$name}
$conf_hr->{"cache"} exists $Unix::Whereis::cache->{"whereis"}{$name}
Truth table66%2/3
A and B
A= $conf_hr->{"cache"}B= exists $Unix::Whereis::cache->{"whereis"}{$name}
ABresult
0X0
100
111
52 }
53  
54 10 local $ENV{'PATH'} = _build_env_path($conf_hr);
55  
56 10 for my $PATH ( split( pathsep(), $ENV{'PATH'} ) ) {
57 31 my $bin = File::Spec->catfile( $PATH, $name );
58 31 ▶ if ( -f $bin && -x _ && -s _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $bin and -x _ and -s _) 5 26 31
Condition100%6/6
-f $bin and -x _ and -s _
!ll&&!rl&&r
2515
-f $bin and -x _
!ll&&!rl&&r
2326
MC/DC100%3/3
-f $bin and -x _ and -s _
-f $bin -x _ -s _
Truth table100%4/4
A and B and C
A= -f $binB= -x _C= -s _
ABCresult
0XX0
10X0
1100
1111
59 5 ▶ $cache->{'whereis'}{$name} = $bin if $conf_hr->{'cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} 2 3 5
60 5 return $bin;
61 }
62 }
63  
64 5 ▶ return $name if $conf_hr->{'fallback'};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"fallback"} 1 4 5
65 4 return;
66 }
67  
68 11 ▶sub whereis_everyone {
Subroutine whereis_everyone: called CC=13 SCAR=25.7
Pod documented
69 11 my ( $name, $conf_hr ) = @_;
70 11 ▶ $conf_hr = {} if !defined $conf_hr || ref $conf_hr ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $conf_hr or ref $conf_hr ne "HASH" 6 5 11
Condition66%2/3
not defined $conf_hr or ref $conf_hr ne "HASH"
l!l&&r!l&&!r
605
MC/DC50%1/2
not defined $conf_hr or ref $conf_hr ne "HASH"
not defined $conf_hr ref $conf_hr ne "HASH"
Truth table66%2/3
not(A) or B
A= not defined $conf_hrB= ref $conf_hr ne "HASH"
ABresult
1X1
011
000
71  
72 11 ▶ if ( $conf_hr->{'cache'} && $cache->{'whereis_everyone'}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis_everyone"}{$name}) 2 9 11
Condition100%3/3
$conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis_everyone"}{$name}
!ll&&!rl&&r
812
MC/DC100%2/2
$conf_hr->{"cache"} and $Unix::Whereis::cache->{"whereis_everyone"}{$name}
$conf_hr->{"cache"} $Unix::Whereis::cache->{"whereis_everyone"}{$name}
Truth table100%3/3
A and B
A= $conf_hr->{"cache"}B= $Unix::Whereis::cache->{"whereis_everyone"}{$name}
ABresult
0X0
100
111
73 2 ▶ delete $cache->{'whereis_everyone'}{$name} if $conf_hr->{'cache'} eq 'clear';
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} eq "clear" 1 1 2
74 2 ▶ return @{ $cache->{'whereis_everyone'}{$name} } if $conf_hr->{'cache'} && exists $cache->{'whereis_everyone'}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis_everyone"}{$name} 1 1 2
Condition66%2/3
$conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis_everyone"}{$name}
!ll&&!rl&&r
011
MC/DC50%1/2
$conf_hr->{"cache"} and exists $Unix::Whereis::cache->{"whereis_everyone"}{$name}
$conf_hr->{"cache"} exists $Unix::Whereis::cache->{"whereis_everyone"}{$name}
Truth table66%2/3
A and B
A= $conf_hr->{"cache"}B= exists $Unix::Whereis::cache->{"whereis_everyone"}{$name}
ABresult
0X0
100
111
75 }
76  
77 10 my @found;
78  
79 10 local $ENV{'PATH'} = _build_env_path($conf_hr);
80  
81 10 for my $PATH ( split( pathsep(), $ENV{'PATH'} ) ) {
82 37 my $bin = File::Spec->catfile( $PATH, $name );
83 37 ▶ push( @found, $bin ) if -f $bin && -x _ && -s _;
Branch: 1 branch
BranchTrueFalseTotal
if -f $bin and -x _ and -s _ 11 26 37
Condition100%6/6
-f $bin and -x _ and -s _
!ll&&!rl&&r
25111
-f $bin and -x _
!ll&&!rl&&r
23212
MC/DC100%3/3
-f $bin and -x _ and -s _
-f $bin -x _ -s _
Truth table100%4/4
A and B and C
A= -f $binB= -x _C= -s _
ABCresult
0XX0
10X0
1100
1111
84 }
85  
86 10 ▶ $cache->{'whereis_everyone'}{$name} = \@found if $conf_hr->{'cache'};
Branch: 1 branch
BranchTrueFalseTotal
if $conf_hr->{"cache"} 2 8 10
87 10 return @found;
88 }
89  
90 73 ▶sub _build_env_path {
Subroutine _build_env_path: called CC=8 SCAR=20.8
91 73 my ($conf_hr) = @_;
92  
93 73 ▶ $sep ||= pathsep();
Condition66%2/3
$Unix::Whereis::sep ||= pathsep()
l!l&&r!l&&!r
7210
MC/DC0%0/2
$Unix::Whereis::sep ||= pathsep()
$Unix::Whereis::sep pathsep()
Truth table66%2/3
A ||= B
A= $Unix::Whereis::sepB= pathsep()
ABresult
1X1
011
000
94  
95 73 my $path = $ENV{'PATH'};
96 73 ▶ $path = $conf_hr->{'mypath'} if exists $conf_hr->{'mypath'} && length $conf_hr->{'mypath'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf_hr->{"mypath"} and length $conf_hr->{"mypath"} 22 51 73
Condition100%3/3
exists $conf_hr->{"mypath"} and length $conf_hr->{"mypath"}
!ll&&!rl&&r
43822
MC/DC100%2/2
exists $conf_hr->{"mypath"} and length $conf_hr->{"mypath"}
exists $conf_hr->{"mypath"} length $conf_hr->{"mypath"}
Truth table100%3/3
A and B
A= exists $conf_hr->{"mypath"}B= length $conf_hr->{"mypath"}
ABresult
0X0
100
111
97 73 $path =~ s/\Q$sep\E+$//;
98 73 $path =~ s/^\Q$sep\E+//;
99  
100 73 ▶ if ( exists $conf_hr->{'prepend'} && length $conf_hr->{'prepend'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf_hr->{"prepend"} and length $conf_hr->{"prepend"}) 24 49 73
Condition100%3/3
exists $conf_hr->{"prepend"} and length $conf_hr->{"prepend"}
!ll&&!rl&&r
41824
MC/DC100%2/2
exists $conf_hr->{"prepend"} and length $conf_hr->{"prepend"}
exists $conf_hr->{"prepend"} length $conf_hr->{"prepend"}
Truth table100%3/3
A and B
A= exists $conf_hr->{"prepend"}B= length $conf_hr->{"prepend"}
ABresult
0X0
100
111
101 24 $conf_hr->{'prepend'} =~ s/\Q$sep\E+$//;
102 24 $conf_hr->{'prepend'} =~ s/^\Q$sep\E+//;
103 24 $path = "$conf_hr->{'prepend'}$sep$path";
104 }
105  
106 73 ▶ if ( exists $conf_hr->{'append'} && length $conf_hr->{'append'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf_hr->{"append"} and length $conf_hr->{"append"}) 24 49 73
Condition100%3/3
exists $conf_hr->{"append"} and length $conf_hr->{"append"}
!ll&&!rl&&r
41824
MC/DC100%2/2
exists $conf_hr->{"append"} and length $conf_hr->{"append"}
exists $conf_hr->{"append"} length $conf_hr->{"append"}
Truth table100%3/3
A and B
A= exists $conf_hr->{"append"}B= length $conf_hr->{"append"}
ABresult
0X0
100
111
107 24 $conf_hr->{'append'} =~ s/\Q$sep\E+$//;
108 24 $conf_hr->{'append'} =~ s/^\Q$sep\E+//;
109 24 $path .= $sep . $conf_hr->{'append'};
110 }
111  
112 73 return $path;
113 }
114  
115 2 1;
116  
117 __END__
↑ Summary