1 package Devel::InnerPackage;
5 use vars qw($VERSION @EXPORT_OK);
8 @EXPORT_OK = qw(list_packages);
15 Devel::InnerPackage - find all the inner packages of a package
20 use Devel::innerPackage qw(list_packages);
22 my @inner_packages = list_packages('Foo::Bar');
28 Given a file like this
36 package Foo::Bar::Quux;
40 package Foo::Bar::Quirka;
48 list_packages('Foo::Bar');
57 =head2 list_packages <package name>
59 Return a list of all inner packages of that package.
64 my $pack = shift; $pack .= "::" unless $pack =~ m!::$!;
68 my @stuff = grep !/^(main|)::$/, keys %{$pack};
69 for my $cand (grep /::$/, @stuff)
72 my @children = list_packages($pack.$cand);
74 push @packs, "$pack$cand" unless $cand =~ /^::/ ||
75 !__PACKAGE__->_loaded($pack.$cand); # or @children;
76 push @packs, @children;
78 return grep {$_ !~ /::::ISA::CACHE/} @packs;
81 ### XXX this is an inlining of the Class-Inspector->loaded()
82 ### method, but inlined to remove the dependency.
84 my ($class, $name) = @_;
88 # Handle by far the two most common cases
89 # This is very fast and handles 99% of cases.
90 return 1 if defined ${"${name}::VERSION"};
91 return 1 if defined @{"${name}::ISA"};
93 # Are there any symbol table entries other than other namespaces
94 foreach ( keys %{"${name}::"} ) {
95 next if substr($_, -2, 2) eq '::';
96 return 1 if defined &{"${name}::$_"};
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 my $filename = join( '/', split /(?:'|::)/, $name ) . '.pm';
102 return 1 if defined $INC{$filename};
110 Simon Wistow <simon@thegestalt.org>
114 Copyright, 2005 Simon Wistow
116 Distributed under the same terms as Perl itself.