Filename | /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Devel/InnerPackage.pm |
Statements | Executed 15 statements in 597µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 178µs | 662µs | BEGIN@7 | Devel::InnerPackage::
1 | 1 | 1 | 11µs | 22µs | BEGIN@3 | Devel::InnerPackage::
1 | 1 | 1 | 8µs | 18µs | BEGIN@67 | Devel::InnerPackage::
1 | 1 | 1 | 7µs | 17µs | BEGIN@87 | Devel::InnerPackage::
1 | 1 | 1 | 7µs | 58µs | BEGIN@4 | Devel::InnerPackage::
1 | 1 | 1 | 6µs | 34µs | BEGIN@5 | Devel::InnerPackage::
0 | 0 | 0 | 0s | 0s | _loaded | Devel::InnerPackage::
0 | 0 | 0 | 0s | 0s | list_packages | Devel::InnerPackage::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Devel::InnerPackage; | ||||
2 | |||||
3 | 2 | 20µs | 2 | 33µs | # spent 22µs (11+11) within Devel::InnerPackage::BEGIN@3 which was called:
# once (11µs+11µs) by Module::Pluggable::Object::BEGIN@8 at line 3 # spent 22µs making 1 call to Devel::InnerPackage::BEGIN@3
# spent 11µs making 1 call to strict::import |
4 | 2 | 22µs | 2 | 110µs | # spent 58µs (7+51) within Devel::InnerPackage::BEGIN@4 which was called:
# once (7µs+51µs) by Module::Pluggable::Object::BEGIN@8 at line 4 # spent 58µs making 1 call to Devel::InnerPackage::BEGIN@4
# spent 51µs making 1 call to base::import |
5 | 2 | 24µs | 2 | 61µs | # spent 34µs (6+27) within Devel::InnerPackage::BEGIN@5 which was called:
# once (6µs+27µs) by Module::Pluggable::Object::BEGIN@8 at line 5 # spent 34µs making 1 call to Devel::InnerPackage::BEGIN@5
# spent 28µs making 1 call to vars::import |
6 | |||||
7 | 2 | 233µs | 2 | 664µs | # spent 662µs (178+484) within Devel::InnerPackage::BEGIN@7 which was called:
# once (178µs+484µs) by Module::Pluggable::Object::BEGIN@8 at line 7 # spent 662µs making 1 call to Devel::InnerPackage::BEGIN@7
# spent 3µs making 1 call to if::import |
8 | |||||
9 | 1 | 700ns | $VERSION = '0.4'; | ||
10 | 1 | 700ns | @EXPORT_OK = qw(list_packages); | ||
11 | |||||
12 | =pod | ||||
13 | |||||
14 | =head1 NAME | ||||
15 | |||||
16 | Devel::InnerPackage - find all the inner packages of a package | ||||
17 | |||||
18 | =head1 SYNOPSIS | ||||
19 | |||||
20 | use Foo::Bar; | ||||
21 | use Devel::InnerPackage qw(list_packages); | ||||
22 | |||||
23 | my @inner_packages = list_packages('Foo::Bar'); | ||||
24 | |||||
25 | |||||
26 | =head1 DESCRIPTION | ||||
27 | |||||
28 | |||||
29 | Given a file like this | ||||
30 | |||||
31 | |||||
32 | package Foo::Bar; | ||||
33 | |||||
34 | sub foo {} | ||||
35 | |||||
36 | |||||
37 | package Foo::Bar::Quux; | ||||
38 | |||||
39 | sub quux {} | ||||
40 | |||||
41 | package Foo::Bar::Quirka; | ||||
42 | |||||
43 | sub quirka {} | ||||
44 | |||||
45 | 1; | ||||
46 | |||||
47 | then | ||||
48 | |||||
49 | list_packages('Foo::Bar'); | ||||
50 | |||||
51 | will return | ||||
52 | |||||
53 | Foo::Bar::Quux | ||||
54 | Foo::Bar::Quirka | ||||
55 | |||||
56 | =head1 METHODS | ||||
57 | |||||
58 | =head2 list_packages <package name> | ||||
59 | |||||
60 | Return a list of all inner packages of that package. | ||||
61 | |||||
62 | =cut | ||||
63 | |||||
64 | sub list_packages { | ||||
65 | my $pack = shift; $pack .= "::" unless $pack =~ m!::$!; | ||||
66 | |||||
67 | 2 | 163µs | 2 | 28µs | # spent 18µs (8+10) within Devel::InnerPackage::BEGIN@67 which was called:
# once (8µs+10µs) by Module::Pluggable::Object::BEGIN@8 at line 67 # spent 18µs making 1 call to Devel::InnerPackage::BEGIN@67
# spent 10µs making 1 call to strict::unimport |
68 | my @packs; | ||||
69 | my @stuff = grep !/^(main|)::$/, keys %{$pack}; | ||||
70 | for my $cand (grep /::$/, @stuff) | ||||
71 | { | ||||
72 | $cand =~ s!::$!!; | ||||
73 | my @children = list_packages($pack.$cand); | ||||
74 | |||||
75 | push @packs, "$pack$cand" unless $cand =~ /^::/ || | ||||
76 | !__PACKAGE__->_loaded($pack.$cand); # or @children; | ||||
77 | push @packs, @children; | ||||
78 | } | ||||
79 | return grep {$_ !~ /::(::ISA::CACHE|SUPER)/} @packs; | ||||
80 | } | ||||
81 | |||||
82 | ### XXX this is an inlining of the Class-Inspector->loaded() | ||||
83 | ### method, but inlined to remove the dependency. | ||||
84 | sub _loaded { | ||||
85 | my ($class, $name) = @_; | ||||
86 | |||||
87 | 2 | 130µs | 2 | 27µs | # spent 17µs (7+10) within Devel::InnerPackage::BEGIN@87 which was called:
# once (7µs+10µs) by Module::Pluggable::Object::BEGIN@8 at line 87 # spent 17µs making 1 call to Devel::InnerPackage::BEGIN@87
# spent 10µs making 1 call to strict::unimport |
88 | |||||
89 | # Handle by far the two most common cases | ||||
90 | # This is very fast and handles 99% of cases. | ||||
91 | return 1 if defined ${"${name}::VERSION"}; | ||||
92 | return 1 if @{"${name}::ISA"}; | ||||
93 | |||||
94 | # Are there any symbol table entries other than other namespaces | ||||
95 | foreach ( keys %{"${name}::"} ) { | ||||
96 | next if substr($_, -2, 2) eq '::'; | ||||
97 | return 1 if defined &{"${name}::$_"}; | ||||
98 | } | ||||
99 | |||||
100 | # No functions, and it doesn't have a version, and isn't anything. | ||||
101 | # As an absolute last resort, check for an entry in %INC | ||||
102 | my $filename = join( '/', split /(?:'|::)/, $name ) . '.pm'; | ||||
103 | return 1 if defined $INC{$filename}; | ||||
104 | |||||
105 | ''; | ||||
106 | } | ||||
107 | |||||
108 | |||||
109 | =head1 AUTHOR | ||||
110 | |||||
111 | Simon Wistow <simon@thegestalt.org> | ||||
112 | |||||
113 | =head1 COPYING | ||||
114 | |||||
115 | Copyright, 2005 Simon Wistow | ||||
116 | |||||
117 | Distributed under the same terms as Perl itself. | ||||
118 | |||||
119 | =head1 BUGS | ||||
120 | |||||
121 | None known. | ||||
122 | |||||
123 | =cut | ||||
124 | |||||
- - | |||||
129 | 1 | 2µs | 1; |