Devel/InnerPackage.pm

filter: Statementstmt 100% 43 / 43 Branchbran 92.8% 13 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 95.7% 67 / 70 CC 11 SCAR 15.4
CC
11
Cov
97%
_loaded
7.0
list_packages
5.0
BEGIN
1.0
CRAP 4.7
« Module/Pluggable.pm ↑ Summary
1 package Devel::InnerPackage;
2  
3 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 46 ▶use if $] > 5.017, 'deprecate';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 46 our $VERSION = '0.4';
9 46 our @EXPORT_OK = qw(list_packages);
10  
11 =pod
12  
13 =head1 NAME
14  
15 Devel::InnerPackage - find all the inner packages of a package
16  
17 =head1 SYNOPSIS
18  
19 use Foo::Bar;
20 use Devel::InnerPackage qw(list_packages);
21  
22 my @inner_packages = list_packages('Foo::Bar');
23  
24  
25 =head1 DESCRIPTION
26  
27  
28 Given a file like this
29  
30  
31 package Foo::Bar;
32  
33 sub foo {}
34  
35  
36 package Foo::Bar::Quux;
37  
38 sub quux {}
39  
40 package Foo::Bar::Quirka;
41  
42 sub quirka {}
43  
44 1;
45  
46 then
47  
48 list_packages('Foo::Bar');
49  
50 will return
51  
52 Foo::Bar::Quux
53 Foo::Bar::Quirka
54  
55 =head1 METHODS
56  
57 =head2 list_packages <package name>
58  
59 Return a list of all inner packages of that package.
60  
61 =cut
62  
63 256 ▶sub list_packages {
Subroutine list_packages: called CC=5 SCAR=16.1
Pod documented
64 256 ▶ my $pack = shift; $pack .= "::" unless $pack =~ m!::$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $pack =~ /::$/ 256 0 256
65  
66 46 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 256 my @packs;
68 256 my @stuff = grep !/^(main|)::$/, keys %{$pack};
69 256 for my $cand (grep /::$/, @stuff)
70 {
71 66 $cand =~ s!::$!!;
72 66 my @children = list_packages($pack.$cand);
73  
74 66 ▶ push @packs, "$pack$cand" unless $cand =~ /^::/ ||
Branch: 1 branch
BranchTrueFalseTotal
unless $cand =~ /^::/ or not __PACKAGE__->_loaded($pack . $cand) 61 5 66
Condition66%2/3
$cand =~ /^::/ or not __PACKAGE__->_loaded($pack . $cand)
l!l&&r!l&&!r
0561
MC/DC50%1/2
$cand =~ /^::/ or not __PACKAGE__->_loaded($pack . $cand)
$cand =~ /^::/ not __PACKAGE__->_loaded($pack . $cand)
Truth table66%2/3
A or not(B)
A= $cand =~ /^::/B= not __PACKAGE__->_loaded($pack . $cand)
ABresult
1X1
011
000
75 !__PACKAGE__->_loaded($pack.$cand); # or @children;
76 66 push @packs, @children;
77 }
78 256 return grep {$_ !~ /::(::ISA::CACHE|SUPER)/} @packs;
79 }
80  
81 ### XXX this is an inlining of the Class-Inspector->loaded()
82 ### method, but inlined to remove the dependency.
83 66 ▶sub _loaded {
Subroutine _loaded: called CC=7 SCAR=19.5
84 66 my ($class, $name) = @_;
85  
86 46 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # Handle by far the two most common cases
89 # This is very fast and handles 99% of cases.
90 66 ▶ return 1 if defined ${"${name}::VERSION"};
Branch: 1 branch
BranchTrueFalseTotal
if defined ${"${name}::VERSION";} 8 58 66
91 58 ▶ return 1 if @{"${name}::ISA"};
Branch: 1 branch
BranchTrueFalseTotal
if @{"${name}::ISA";} 6 52 58
92  
93 # Are there any symbol table entries other than other namespaces
94 52 foreach ( keys %{"${name}::"} ) {
95 107 ▶ next if substr($_, -2, 2) eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, -2, 2) eq "::" 4 103 107
96 103 ▶ return 1 if defined &{"${name}::$_"};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{"${name}::$_";} 33 70 103
97 }
98  
99 # No functions, and it doesn't have a version, and isn't anything.
100 # As an absolute last resort, check for an entry in %INC
101 19 my $filename = join( '/', split /(?:'|::)/, $name ) . '.pm';
102 19 ▶ return 1 if defined $INC{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $INC{$filename} 14 5 19
103  
104 5 '';
105 }
106  
107  
108 =head1 AUTHOR
109  
110 Simon Wistow <simon@thegestalt.org>
111  
112 =head1 COPYING
113  
114 Copyright, 2005 Simon Wistow
115  
116 Distributed under the same terms as Perl itself.
117  
118 =head1 BUGS
119  
120 None known.
121  
122 =cut
123  
124  
125  
126  
127  
128 46 1;
« Module/Pluggable.pm ↑ Summary