blib/lib/Namespace/Subroutines.pm

filter: Statementstmt 100% 63 / 63 Branchbran 72.7% 16 / 22 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 82.5% 90 / 109 CC 17 SCAR 23.6
CC
17
Cov
85%
find
16.7
__ANON__
3.0
BEGIN
1.0
CRAP 10.6
↑ Summary Build »
1 package Namespace::Subroutines;
2 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 2 ▶use attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.01';
11  
12 2 my %skip = (
13 AUTOLOAD => 1,
14 BEGIN => 1,
15 MODIFY_CODE_ATTRIBUTES => 1,
16 FETCH_CODE_ATTRIBUTES => 1,
17 );
18  
19 sub find {
20 1 ▶ my ( $ns, $cb ) = @_;
Subroutine find: called CC=15 SCAR=28.1
Pod undocumented
21  
22 # 'My::App::Controller' -> 'My/App/Controller'
23 1 my $ns2 = $ns =~ s{::}{/}gr;
24  
25 1 my @modules;
26 1 foreach my $path (@INC) {
27 7 ▶ next unless -d $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 7 7
28 File::Find::find(
29 sub {
30 21501 ▶ return unless /\.pm$/;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless /\.pm$/ 19532 1969 21501
31 1969 my $name = $File::Find::name =~ s{$path/}{}r;
32 1969 ▶ return unless $name =~ /^$ns2/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$ns2/ 1968 1 1969
33 1 push @modules, [ $name, $File::Find::name ];
34 },
35 7 $path
36 );
37 }
38  
39 1 foreach my $m (@modules) {
40 1 my ( $modname, $path ) = @$m;
41  
42 # 'Data/Dumper.pm' -> qw(Data Dumper.pm)
43 1 my @a = split( m{/}, $modname );
44 1 pop @a; # qw(Data)
45 1 my $namespace = join( '/', @a ); # 'Data'
46  
47 # 'My/App/Controller/Users.pm', 'My/App/Controller/Inventory.pm', etc.
48 1 ▶ next unless $namespace =~ /^$ns2/;
Branch: 1 branch
BranchTrueFalseTotal
unless $namespace =~ /^$ns2/ 0 1 1
49 1 ▶ require $modname unless defined $INC{$modname};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $INC{$modname} 1 0 1
50  
51 1 my $module = $modname; # 'My/App/Controller/Users.pm'
52 1 $module =~ s{\.pm$}{}; # 'My/App/Controller/Users'
53 1 $module =~ s{/}{::}g; # 'My::App::Controller::Users'
54 1 $module .= '::'; # 'My::App::Controller::Users::'
55 1 my $table = '%' . $module; # '%My::App::Controller::Users::'
56  
57 ## no critic (BuiltinFunctions::ProhibitStringyEval)
58 1 my @symbols = split( m{\|}, eval "join('|', keys $table)" );
59 1 my @subroutines = grep { defined &{ $module . $_ } } @symbols;
60 1 my %subroutines;
61  
62 1 ▶ open my $fh, '<', $path or ( carp "unable to open $!" and next );
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or carp("unable to open $!") and next 0 1 1
Condition20%1/5
open(my $fh, '<', $path) || carp("unable to open $!") && (next)
l!l&&r!l&&!r
100
carp("unable to open $!") and next
!ll
00
MC/DC0%0/2
open(my $fh, '<', $path) || carp("unable to open $!") && (next)
open(my $fh, '<', $path) carp("unable to open $!")
Truth table33%1/3
A || B
A= open(my $fh, '<', $path)B= carp("unable to open $!")
ABresult
1X1
011
000
63 1 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 10 1 11
64 10 ▶ next unless $line =~ /^sub\s+(\w+)[\:\(\s]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^sub\s+(\w+)[\:\(\s]/ 8 2 10
65 2 $subroutines{$1} = 1;
66 }
67 1 ▶ close $fh or ( carp "error closing $!" and next );
Branch: 1 branch
BranchTrueFalseTotal
close $fh or carp("error closing $!") and next 0 1 1
Condition20%1/5
close $fh || carp("error closing $!") && (next)
l!l&&r!l&&!r
100
carp("error closing $!") and next
!ll
00
MC/DC0%0/2
close $fh || carp("error closing $!") && (next)
close $fh carp("error closing $!")
Truth table33%1/3
A || B
A= close $fhB= carp("error closing $!")
ABresult
1X1
011
000
68  
69 # 'My::App::Controller::Users::' -> 'Users'
70 1 $module =~ s{^$ns\::(.+)::$}{$1};
71  
72 1 foreach my $sub (@subroutines) {
73 4 ▶ next if $skip{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if $skip{$sub} 0 4 4
74 4 ▶ next unless $subroutines{$sub};
Branch: 1 branch
BranchTrueFalseTotal
unless $subroutines{$sub} 2 2 4
75 2 my $name = join( '::', $ns, $module, $sub );
76 2 my $ref = \&$name;
77 2 my @attrs = attributes::get( \&$name );
78 2 $cb->( [ split( /::/, $module ) ], $sub, $ref, \@attrs );
79 }
80 }
81 }
82  
83 2 1;
84 __END__
↑ Summary Build »