| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Unix::Whereis; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 76640 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 4 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 84 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | $Unix::Whereis::VERSION = '0.1'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 19 | use File::Spec; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 99 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $sep; | 
| 11 |  |  |  |  |  |  | our $cache; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub import { | 
| 14 | 3 |  |  | 3 |  | 2060 | my $caller = caller(); | 
| 15 | 2 |  |  | 2 |  | 8 | no strict 'refs';    ## no critic | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1779 |  | 
| 16 | 3 |  |  |  |  | 6 | *{ $caller . '::whereis' } = \&whereis; | 
|  | 3 |  |  |  |  | 13 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 3 | 100 | 66 |  |  | 1799 | if ( defined $_[1] && $_[1] eq 'whereis_everyone' ) { | 
| 19 | 1 |  |  |  |  | 3 | *{ $caller . '::whereis_everyone' } = \&whereis_everyone; | 
|  | 1 |  |  |  |  | 5 |  | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub pathsep { | 
| 24 | 35 | 100 |  | 35 | 1 | 58175 | $sep = $_[0] if @_; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 35 | 100 |  |  |  | 81 | if ( !defined $sep ) { | 
| 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 | 100 | 66 |  |  | 43 | if ( keys %Config::Config && exists $Config::Config{'path_sep'} && length $Config::Config{'path_sep'} ) { | 
|  |  |  | 100 |  |  |  |  | 
| 33 | 1 |  |  |  |  | 2 | $sep = $Config::Config{'path_sep'}; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  | else { | 
| 36 | 5 |  |  |  |  | 12 | $sep = ':'; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 35 |  |  |  |  | 172 | return $sep; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub whereis { | 
| 44 | 11 |  |  | 11 | 1 | 6600 | my ( $name, $conf_hr ) = @_; | 
| 45 | 11 | 50 | 33 |  |  | 74 | return if !defined $name || $name eq ''; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 11 | 100 | 66 |  |  | 53 | $conf_hr = {} if !defined $conf_hr || ref $conf_hr ne 'HASH'; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 11 | 100 | 100 |  |  | 45 | if ( $conf_hr->{'cache'} && $cache->{'whereis'}{$name} ) { | 
| 50 | 2 | 100 |  |  |  | 11 | delete $cache->{'whereis'}{$name} if $conf_hr->{'cache'} eq 'clear'; | 
| 51 | 2 | 100 | 66 |  |  | 21 | return $cache->{'whereis'}{$name} if $conf_hr->{'cache'} && exists $cache->{'whereis'}{$name}; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 10 |  |  |  |  | 24 | local $ENV{'PATH'} = _build_env_path($conf_hr); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 10 |  |  |  |  | 31 | for my $PATH ( split( pathsep(), $ENV{'PATH'} ) ) { | 
| 57 | 31 |  |  |  |  | 324 | my $bin = File::Spec->catfile( $PATH, $name ); | 
| 58 | 31 | 100 | 100 |  |  | 693 | if ( -f $bin && -x _ && -s _ ) { | 
|  |  |  | 100 |  |  |  |  | 
| 59 | 5 | 100 |  |  |  | 18 | $cache->{'whereis'}{$name} = $bin if $conf_hr->{'cache'}; | 
| 60 | 5 |  |  |  |  | 99 | return $bin; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 5 | 100 |  |  |  | 23 | return $name if $conf_hr->{'fallback'}; | 
| 65 | 4 |  |  |  |  | 28 | return; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub whereis_everyone { | 
| 69 | 11 |  |  | 11 | 1 | 8832 | my ( $name, $conf_hr ) = @_; | 
| 70 | 11 | 100 | 66 |  |  | 65 | $conf_hr = {} if !defined $conf_hr || ref $conf_hr ne 'HASH'; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 11 | 100 | 100 |  |  | 53 | if ( $conf_hr->{'cache'} && $cache->{'whereis_everyone'}{$name} ) { | 
| 73 | 2 | 100 |  |  |  | 11 | delete $cache->{'whereis_everyone'}{$name} if $conf_hr->{'cache'} eq 'clear'; | 
| 74 | 2 | 100 | 66 |  |  | 16 | return @{ $cache->{'whereis_everyone'}{$name} } if $conf_hr->{'cache'} && exists $cache->{'whereis_everyone'}{$name}; | 
|  | 1 |  |  |  |  | 8 |  | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 10 |  |  |  |  | 14 | my @found; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 10 |  |  |  |  | 25 | local $ENV{'PATH'} = _build_env_path($conf_hr); | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 10 |  |  |  |  | 31 | for my $PATH ( split( pathsep(), $ENV{'PATH'} ) ) { | 
| 82 | 37 |  |  |  |  | 482 | my $bin = File::Spec->catfile( $PATH, $name ); | 
| 83 | 37 | 100 | 100 |  |  | 1170 | push( @found, $bin ) if -f $bin && -x _ && -s _; | 
|  |  |  | 100 |  |  |  |  | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 10 | 100 |  |  |  | 40 | $cache->{'whereis_everyone'}{$name} = \@found if $conf_hr->{'cache'}; | 
| 87 | 10 |  |  |  |  | 134 | return @found; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub _build_env_path { | 
| 91 | 73 |  |  | 73 |  | 121 | my ($conf_hr) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 73 |  | 66 |  |  | 245 | $sep ||= pathsep(); | 
| 94 |  |  |  |  |  |  |  | 
| 95 | 73 |  |  |  |  | 139 | my $path = $ENV{'PATH'}; | 
| 96 | 73 | 100 | 100 |  |  | 300 | $path = $conf_hr->{'mypath'} if exists $conf_hr->{'mypath'} && length $conf_hr->{'mypath'}; | 
| 97 | 73 |  |  |  |  | 440 | $path =~ s/\Q$sep\E+$//; | 
| 98 | 73 |  |  |  |  | 215 | $path =~ s/^\Q$sep\E+//; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 73 | 100 | 100 |  |  | 276 | if ( exists $conf_hr->{'prepend'} && length $conf_hr->{'prepend'} ) { | 
| 101 | 24 |  |  |  |  | 103 | $conf_hr->{'prepend'} =~ s/\Q$sep\E+$//; | 
| 102 | 24 |  |  |  |  | 85 | $conf_hr->{'prepend'} =~ s/^\Q$sep\E+//; | 
| 103 | 24 |  |  |  |  | 66 | $path = "$conf_hr->{'prepend'}$sep$path"; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 73 | 100 | 100 |  |  | 303 | if ( exists $conf_hr->{'append'} && length $conf_hr->{'append'} ) { | 
| 107 | 24 |  |  |  |  | 96 | $conf_hr->{'append'} =~ s/\Q$sep\E+$//; | 
| 108 | 24 |  |  |  |  | 80 | $conf_hr->{'append'} =~ s/^\Q$sep\E+//; | 
| 109 | 24 |  |  |  |  | 70 | $path .= $sep . $conf_hr->{'append'}; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 73 |  |  |  |  | 417 | return $path; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | 1; | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | __END__ |