blib/lib/Module/Want.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 84.3% 27 / 32 Conditioncond 82.8% 29 / 35 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 14 / 14 Podpod 100% 10 / 10 totaltotal 85.6% 155 / 181 CC 31 SCAR 20.6
CC
31
Cov
91%
import
14.6
search_inc_paths
9.1
have_mod
6.0
CRAP 7.9
↑ Summary
1 package Module::Want;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 *normalize_ns = \&get_clean_ns; # do before warnings to prevent 'only used once' warning
5 1 *get_relative_path_of_ns = \&get_inc_key; # do before warnings to prevent 'only used once' warning
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 $Module::Want::VERSION = '0.6';
9  
10 1 my %lookup;
11  
12 # Uncomment these 3 lines and ' # $tries{$ns}++;' in have_mod() for dev testing
13 # $Module::Want::DevTesting = 1;
14 # my %tries;
15 # sub _get_debugs_refs { return \%lookup, \%tries }
16  
17 1 my $ns_regexp = qr/[A-Za-z_][A-Za-z0-9_]*(?:(?:\:\:|\')[A-Za-z0-9_]+)*/;
18  
19 1 ▶sub get_ns_regexp { return $ns_regexp }
Subroutine get_ns_regexp: called CC=1 SCAR=0.0
Pod documented
20  
21 42 ▶sub is_ns { $_[0] =~ m/\A$ns_regexp\z/ }
Subroutine is_ns: called CC=1 SCAR=0.0
Pod documented
22  
23 6 ▶sub get_all_use_require_in_text {
Subroutine get_all_use_require_in_text: called CC=1 SCAR=0.0
Pod documented
24 6 return $_[0] =~ m/(?:^\s*|\;\s*|eval[^;]+)(?:use|require)\s+($ns_regexp)/g;
25 }
26  
27 14 ▶sub get_inc_key {
Subroutine get_inc_key: called CC=2 SCAR=7.0
Pod documented
28 14 ▶ return if !is_ns( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_ns($_[0]) 0 14 14
29  
30 # %INC keys are always unix format so no need for File::Spec
31 # if I've been misinformed of that fact then please let me know, thanks
32 14 my $key = $_[0] . '.pm';
33 14 $key =~ s{(?:\:\:|\')}{/}g;
34 14 return $key;
35 }
36  
37 3 ▶sub distname2ns {
Subroutine distname2ns: called CC=2 SCAR=6.9
Pod documented
38 3 my ($node) = @_;
39 3 $node =~ s/-/::/g;
40 3 my $ns = get_clean_ns($node);
41 3 ▶ return $ns if is_ns($ns);
Branch: 1 branch
BranchTrueFalseTotal
if is_ns($ns) 2 1 3
42 1 return;
43 }
44  
45 3 ▶sub ns2distname {
Subroutine ns2distname: called CC=2 SCAR=6.9
Pod documented
46 3 my $node = get_clean_ns( $_[0] );
47 3 ▶ return if !is_ns($node);
Branch: 1 branch
BranchTrueFalseTotal
unless is_ns($node) 1 2 3
48 2 $node =~ s/::/-/g;
49 2 return $node;
50 }
51  
52 7 ▶sub get_clean_ns {
Subroutine get_clean_ns: called CC=1 SCAR=0.0
Pod documented
53 7 my $dirty = $_[0];
54 7 $dirty =~ s{^\s+}{};
55 7 $dirty =~ s{\s+$}{};
56 7 $dirty =~ s{\'}{::}g;
57 7 return $dirty;
58 }
59  
60 11 ▶sub have_mod {
Subroutine have_mod: called CC=6 SCAR=18.0
Pod documented
61 11 my ( $ns, $skip_cache ) = @_;
62 11 ▶ $skip_cache ||= 0;
Condition50%1/2
$skip_cache ||= 0
l!l
011
MC/DC0%0/1
$skip_cache ||= 0
$skip_cache
Truth table50%1/2
A
A= $skip_cache
Aresult
11
00
63  
64 11 ▶ if ( !is_ns($ns) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_ns($ns)) 1 10 11
65 1 require Carp;
66 1 Carp::carp('Invalid Namespace');
67 1 return;
68 }
69  
70 10 ▶ if ( $skip_cache || !exists $lookup{$ns} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_cache or not exists $lookup{$ns}) 4 6 10
Condition66%2/3
$skip_cache or not exists $lookup{$ns}
l!l&&r!l&&!r
046
MC/DC50%1/2
$skip_cache or not exists $lookup{$ns}
$skip_cache not exists $lookup{$ns}
Truth table66%2/3
A or not(B)
A= $skip_cacheB= not exists $lookup{$ns}
ABresult
1X1
011
000
71  
72 4 $lookup{$ns} = 0;
73  
74 # $tries{$ns}++;
75 4 local $SIG{__DIE__}; # prevent benign eval from tripping potentially fatal sig handler
76 4 eval qq{require $ns;\$lookup{\$ns}++;}; ## no critic
77 }
78  
79 10 ▶ return $lookup{$ns} if $lookup{$ns};
Branch: 1 branch
BranchTrueFalseTotal
if $lookup{$ns} 7 3 10
80 3 return;
81 }
82  
83 2 ▶sub get_inc_path_via_have_mod {
Subroutine get_inc_path_via_have_mod: called CC=2 SCAR=6.9
Pod documented
84 2 my ( $ns, $skip_cache ) = @_;
85 2 ▶ return unless have_mod( $ns, $skip_cache );
Branch: 1 branch
BranchTrueFalseTotal
unless have_mod($ns, $skip_cache) 1 1 2
86 1 return $INC{ get_inc_key($ns) };
87 }
88  
89 4 ▶sub search_inc_paths {
Subroutine search_inc_paths: called CC=9 SCAR=22.1
Pod documented
90 4 my ( $ns, $want_abs ) = @_;
91  
92 4 ▶ have_mod('File::Spec') || return;
Branch: 1 branch
BranchTrueFalseTotal
have_mod("File::Spec") or return 4 0 4
93  
94 4 my $rel_path = File::Spec->catfile( split( m{/}, get_relative_path_of_ns($ns) ) );
95 4 ▶ my $return_first = wantarray ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
96 4 my @result;
97  
98 4 for my $path (@INC) {
99 12 my $abspath = File::Spec->rel2abs( $rel_path, $path );
100 12 ▶ if ( -f $abspath ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $abspath) 6 6 12
101 6 ▶ push @result, ( $want_abs ? $abspath : $path );
Branch: 1 branch
BranchTrueFalseTotal
$want_abs ? : 3 3 6
102 6 ▶ last if $return_first;
Branch: 1 branch
BranchTrueFalseTotal
if $return_first 2 4 6
103 }
104 }
105  
106 4 ▶ if (@result) {
Branch: 1 branch
BranchTrueFalseTotal
if (@result) 4 0 4
107 4 ▶ return $result[0] if $return_first;
Branch: 1 branch
BranchTrueFalseTotal
if $return_first 2 2 4
108 2 return @result;
109 }
110 0 return;
111 }
112  
113 6 ▶sub import {
Subroutine import: called CC=14 SCAR=26.8
114 6 shift;
115  
116 6 my $caller = caller();
117  
118 1 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
119 6 *{ $caller . '::have_mod' } = \&have_mod;
120  
121 6 for my $ns (@_) {
122 8 ▶ next if $ns eq 'have_mod';
Branch: 1 branch
BranchTrueFalseTotal
if $ns eq "have_mod" 0 8 8
123  
124 8 ▶ if ( $ns eq 'is_ns' || $ns eq 'get_inc_key' || $ns eq 'get_clean_ns' || $ns eq 'get_ns_regexp' || $ns eq 'get_all_use_require_in_text' || $ns eq 'get_relative_path_of_ns' || $ns eq 'normalize_ns' || $ns eq 'get_inc_path_via_have_mod' || $ns eq 'search_inc_paths' || $ns eq 'distname2ns' || $ns eq 'ns2distname' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns eq 'is_ns' or $ns eq 'get_inc_key' or $ns eq 'get_clean_ns' or $ns eq 'get_ns_regexp' or $ns eq 'get_all_use_require_in_text' or $ns eq 'get_relative_path_of_ns' or $ns eq 'normalize_ns' or $ns eq 'get_inc_path_via_have_mod' or $ns eq 'search_inc_paths' or $ns eq 'distname2ns' or $ns eq 'ns2distname') { } 8 0 8
Condition86%26/30
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns" or $ns eq "get_inc_path_via_have_mod" or $ns eq "search_inc_paths" or $ns eq "distname2ns" or $ns eq "ns2distname"
l!l&&r!l&&!r
710
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns" or $ns eq "get_inc_path_via_have_mod" or $ns eq "search_inc_paths" or $ns eq "distname2ns"
l!l&&r!l&&!r
611
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns" or $ns eq "get_inc_path_via_have_mod" or $ns eq "search_inc_paths"
l!l&&r!l&&!r
512
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns" or $ns eq "get_inc_path_via_have_mod"
l!l&&r!l&&!r
413
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns"
l!l&&r!l&&!r
314
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns"
l!l&&r!l&&!r
215
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text"
l!l&&r!l&&!r
206
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp"
l!l&&r!l&&!r
206
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns"
l!l&&r!l&&!r
206
$ns eq "is_ns" or $ns eq "get_inc_key"
l!l&&r!l&&!r
116
MC/DC0%0/11
$ns eq "is_ns" or $ns eq "get_inc_key" or $ns eq "get_clean_ns" or $ns eq "get_ns_regexp" or $ns eq "get_all_use_require_in_text" or $ns eq "get_relative_path_of_ns" or $ns eq "normalize_ns" or $ns eq "get_inc_path_via_have_mod" or $ns eq "search_inc_paths" or $ns eq "distname2ns" or $ns eq "ns2distname"
$ns eq "is_ns" $ns eq "get_inc_key" $ns eq "get_clean_ns" $ns eq "get_ns_regexp" $ns eq "get_all_use_require_in_text" $ns eq "get_relative_path_of_ns" $ns eq "normalize_ns" $ns eq "get_inc_path_via_have_mod" $ns eq "search_inc_paths" $ns eq "distname2ns" $ns eq "ns2distname"
Truth table66%8/12
A or B or C or D or E or F or G or H or I or J or K
A= $ns eq "is_ns"B= $ns eq "get_inc_key"C= $ns eq "get_clean_ns"D= $ns eq "get_ns_regexp"E= $ns eq "get_all_use_require_in_text"F= $ns eq "get_relative_path_of_ns"G= $ns eq "normalize_ns"H= $ns eq "get_inc_path_via_have_mod"I= $ns eq "search_inc_paths"J= $ns eq "distname2ns"K= $ns eq "ns2distname"
ABCDEFGHIJKresult
1XXXXXXXXXX1
01XXXXXXXXX1
001XXXXXXXX1
0001XXXXXXX1
00001XXXXXX1
000001XXXXX1
0000001XXXX1
00000001XXX1
000000001XX1
0000000001X1
000000000011
000000000000
125 8 *{ $caller . "::$ns" } = \&{$ns};
126 }
127 else {
128 0 have_mod($ns);
129 }
130 }
131 }
132  
133 1 1;
134  
135 __END__
↑ Summary