Profile of Class/Inspector.pm

← Index
NYTProf Performance Profile «block view•line view•sub view»
For xt/tapper-mcp-scheduler-with-db-longrun.t
Run on Tue May 22 17:18:39 2012
Reported on Tue May 22 17:22:56 2012

Filename | /2home/ss5/perl5/perlbrew/perls/perl-5.12.3/lib/site_perl/5.12.3/Class/Inspector.pm
Statements | Executed 127 statements in 1.82ms

Subroutines
Calls | P | F | Exclusive Time | Inclusive Time | Subroutine
--- | --- | --- | --- | --- | ---
1 | 1 | 1 | 46µs | 56µs | Class::Inspector::::BEGIN@51Class::Inspector::BEGIN@51
8 | 3 | 1 | 46µs | 65µs | Class::Inspector::::_classClass::Inspector::_class
4 | 1 | 1 | 39µs | 39µs | Class::Inspector::::_loadedClass::Inspector::_loaded
4 | 1 | 1 | 29µs | 109µs | Class::Inspector::::loadedClass::Inspector::loaded
2 | 1 | 1 | 23µs | 102µs | Class::Inspector::::filenameClass::Inspector::filename
1 | 1 | 1 | 19µs | 19µs | Class::Inspector::::BEGIN@42Class::Inspector::BEGIN@42
8 | 1 | 1 | 15µs | 15µs | Class::Inspector::::CORE:matchClass::Inspector::CORE:match (opcode)
2 | 1 | 1 | 13µs | 25µs | Class::Inspector::::_inc_filenameClass::Inspector::_inc_filename
1 | 1 | 1 | 12µs | 29µs | Class::Inspector::::BEGIN@46Class::Inspector::BEGIN@46
2 | 1 | 1 | 12µs | 36µs | Class::Inspector::::loaded_filenameClass::Inspector::loaded_filename
1 | 1 | 1 | 11µs | 70µs | Class::Inspector::::BEGIN@50Class::Inspector::BEGIN@50
1 | 1 | 1 | 9µs | 23µs | Class::Inspector::::BEGIN@540Class::Inspector::BEGIN@540
1 | 1 | 1 | 7µs | 15µs | Class::Inspector::::BEGIN@553Class::Inspector::BEGIN@553
2 | 2 | 1 | 6µs | 6µs | Class::Inspector::::CORE:qrClass::Inspector::CORE:qr (opcode)
1 | 1 | 1 | 6µs | 18µs | Class::Inspector::::BEGIN@45Class::Inspector::BEGIN@45
1 | 1 | 1 | 3µs | 3µs | Class::Inspector::::BEGIN@47Class::Inspector::BEGIN@47
8 | 1 | 1 | 2µs | 2µs | Class::Inspector::::CORE:substClass::Inspector::CORE:subst (opcode)
1 | 1 | 1 | 1µs | 1µs | Class::Inspector::::CORE:regcompClass::Inspector::CORE:regcomp (opcode)
0 | 0 | 0 | 0s | 0s | Class::Inspector::::_inc_to_localClass::Inspector::_inc_to_local
0 | 0 | 0 | 0s | 0s | Class::Inspector::::_subnamesClass::Inspector::_subnames
0 | 0 | 0 | 0s | 0s | Class::Inspector::::childrenClass::Inspector::children
0 | 0 | 0 | 0s | 0s | Class::Inspector::::function_existsClass::Inspector::function_exists
0 | 0 | 0 | 0s | 0s | Class::Inspector::::function_refsClass::Inspector::function_refs
0 | 0 | 0 | 0s | 0s | Class::Inspector::::functionsClass::Inspector::functions
0 | 0 | 0 | 0s | 0s | Class::Inspector::::installedClass::Inspector::installed
0 | 0 | 0 | 0s | 0s | Class::Inspector::::methodsClass::Inspector::methods
0 | 0 | 0 | 0s | 0s | Class::Inspector::::recursive_childrenClass::Inspector::recursive_children
0 | 0 | 0 | 0s | 0s | Class::Inspector::::resolved_filenameClass::Inspector::resolved_filename
0 | 0 | 0 | 0s | 0s | Class::Inspector::::subclassesClass::Inspector::subclasses

Call graph for these subroutines as a Graphviz dot language file.

Line | State ments | Time on line | Calls | Time in subs | Code
--- | --- | --- | --- | --- | ---
1 |  |  |  |  | package Class::Inspector;
2 |  |  |  |  |
3 |  |  |  |  | =pod
4 |  |  |  |  |
5 |  |  |  |  | =head1 NAME
6 |  |  |  |  |
7 |  |  |  |  | Class::Inspector - Get information about a class and its structure
8 |  |  |  |  |
9 |  |  |  |  | =head1 SYNOPSIS
10 |  |  |  |  |
11 |  |  |  |  | use Class::Inspector;
12 |  |  |  |  |
13 |  |  |  |  | # Is a class installed and/or loaded
14 |  |  |  |  | Class::Inspector->installed( 'Foo::Class' );
15 |  |  |  |  | Class::Inspector->loaded( 'Foo::Class' );
16 |  |  |  |  |
17 |  |  |  |  | # Filename related information
18 |  |  |  |  | Class::Inspector->filename( 'Foo::Class' );
19 |  |  |  |  | Class::Inspector->resolved_filename( 'Foo::Class' );
20 |  |  |  |  |
21 |  |  |  |  | # Get subroutine related information
22 |  |  |  |  | Class::Inspector->functions( 'Foo::Class' );
23 |  |  |  |  | Class::Inspector->function_refs( 'Foo::Class' );
24 |  |  |  |  | Class::Inspector->function_exists( 'Foo::Class', 'bar' );
25 |  |  |  |  | Class::Inspector->methods( 'Foo::Class', 'full', 'public' );
26 |  |  |  |  |
27 |  |  |  |  | # Find all loaded subclasses or something
28 |  |  |  |  | Class::Inspector->subclasses( 'Foo::Class' );
29 |  |  |  |  |
30 |  |  |  |  | =head1 DESCRIPTION
31 |  |  |  |  |
32 |  |  |  |  | Class::Inspector allows you to get information about a loaded class. Most or
33 |  |  |  |  | all of this information can be found in other ways, but they aren't always
34 |  |  |  |  | very friendly, and usually involve a relatively high level of Perl wizardry,
35 |  |  |  |  | or strange and unusual looking code. Class::Inspector attempts to provide
36 |  |  |  |  | an easier, more friendly interface to this information.
37 |  |  |  |  |
38 |  |  |  |  | =head1 METHODS
39 |  |  |  |  |
40 |  |  |  |  | =cut
41 |  |  |  |  |
42 | 3 | 30µs | 1 | 19µs | # spent 19µs within Class::Inspector::BEGIN@42 which was called: # once (19µs+0s) by File::ShareDir::BEGIN@114 at line 42 use 5.006; # spent 19µs making 1 call to Class::Inspector::BEGIN@42
43 |  |  |  |  | # We don't want to use strict refs anywhere in this module, since we do a
44 |  |  |  |  | # lot of things in here that aren't strict refs friendly.
45 | 3 | 17µs | 2 | 31µs | # spent 18µs (6+13) within Class::Inspector::BEGIN@45 which was called: # once (6µs+13µs) by File::ShareDir::BEGIN@114 at line 45 use strict qw{vars subs}; # spent 18µs making 1 call to Class::Inspector::BEGIN@45 # spent 13µs making 1 call to strict::import
46 | 3 | 19µs | 2 | 45µs | # spent 29µs (12+16) within Class::Inspector::BEGIN@46 which was called: # once (12µs+16µs) by File::ShareDir::BEGIN@114 at line 46 use warnings; # spent 29µs making 1 call to Class::Inspector::BEGIN@46 # spent 16µs making 1 call to warnings::import
47 | 3 | 18µs | 1 | 3µs | # spent 3µs within Class::Inspector::BEGIN@47 which was called: # once (3µs+0s) by File::ShareDir::BEGIN@114 at line 47 use File::Spec (); # spent 3µs making 1 call to Class::Inspector::BEGIN@47
48 |  |  |  |  |
49 |  |  |  |  | # Globals
50 | 3 | 102µs | 2 | 128µs | # spent 70µs (11+58) within Class::Inspector::BEGIN@50 which was called: # once (11µs+58µs) by File::ShareDir::BEGIN@114 at line 50 use vars qw{$VERSION $RE_IDENTIFIER $RE_CLASS $UNIX}; # spent 70µs making 1 call to Class::Inspector::BEGIN@50 # spent 58µs making 1 call to vars::import
51 |  |  |  |  | # spent 56µs (46+10) within Class::Inspector::BEGIN@51 which was called: # once (46µs+10µs) by File::ShareDir::BEGIN@114 at line 68 BEGIN {
52 | 5 | 19µs |  |  | $VERSION = '1.27';
53 |  |  |  |  |
54 |  |  |  |  | # If Unicode is available, enable it so that the
55 |  |  |  |  | # pattern matches below match unicode method names.
56 |  |  |  |  | # We can safely ignore any failure here.
57 | 2 | 25µs |  |  | SCOPE: {
58 |  |  |  |  | local $@;
59 |  |  |  |  | eval "require utf8; utf8->import"; # spent 6µs executing statements in string eval
60 |  |  |  |  | }
61 |  |  |  |  |
62 |  |  |  |  | # Predefine some regexs
63 |  |  | 1 | 6µs | $RE_IDENTIFIER = qr/\A[^\W\d]\w*\z/s; # spent 6µs making 1 call to Class::Inspector::CORE:qr
64 |  |  | 1 | 1µs | $RE_CLASS = qr/\A[^\W\d]\w*(?:(?:\'|::)\w+)*\z/s; # spent 1µs making 1 call to Class::Inspector::CORE:qr
65 |  |  |  |  |
66 |  |  |  |  | # Are we on something Unix-like?
67 |  |  |  |  | $UNIX = !! ( $File::Spec::ISA[0] eq 'File::Spec::Unix' );
68 | 1 | 1.08ms | 1 | 56µs | } # spent 56µs making 1 call to Class::Inspector::BEGIN@51
69 |  |  |  |  |
- - |  |  |  |  |
74 |  |  |  |  | #####################################################################
75 |  |  |  |  | # Basic Methods
76 |  |  |  |  |
77 |  |  |  |  | =pod
78 |  |  |  |  |
79 |  |  |  |  | =head2 installed $class
80 |  |  |  |  |
81 |  |  |  |  | The C<installed> static method tries to determine if a class is installed
82 |  |  |  |  | on the machine, or at least available to Perl. It does this by wrapping
83 |  |  |  |  | around C<resolved_filename>.
84 |  |  |  |  |
85 |  |  |  |  | Returns true if installed/available, false if the class is not installed,
86 |  |  |  |  | or C<undef> if the class name is invalid.
87 |  |  |  |  |
88 |  |  |  |  | =cut
89 |  |  |  |  |
90 |  |  |  |  | sub installed {
91 |  |  |  |  | my $class = shift;
92 |  |  |  |  | !! ($class->loaded_filename($_[0]) or $class->resolved_filename($_[0]));
93 |  |  |  |  | }
94 |  |  |  |  |
95 |  |  |  |  | =pod
96 |  |  |  |  |
97 |  |  |  |  | =head2 loaded $class
98 |  |  |  |  |
99 |  |  |  |  | The C<loaded> static method tries to determine if a class is loaded by
100 |  |  |  |  | looking for symbol table entries.
101 |  |  |  |  |
102 |  |  |  |  | This method it uses to determine this will work even if the class does not
103 |  |  |  |  | have its own file, but is contained inside a single file with multiple
104 |  |  |  |  | classes in it. Even in the case of some sort of run-time loading class
105 |  |  |  |  | being used, these typically leave some trace in the symbol table, so an
106 |  |  |  |  | L<Autoload> or L<Class::Autouse>-based class should correctly appear
107 |  |  |  |  | loaded.
108 |  |  |  |  |
109 |  |  |  |  | Returns true if the class is loaded, false if not, or C<undef> if the
110 |  |  |  |  | class name is invalid.
111 |  |  |  |  |
112 |  |  |  |  | =cut
113 |  |  |  |  |
114 |  |  |  |  | # spent 109µs (29+80) within Class::Inspector::loaded which was called 4 times, avg 27µs/call: # 4 times (29µs+80µs) by File::ShareDir::_MODULE at line 526 of File/ShareDir.pm, avg 27µs/call sub loaded {
115 | 12 | 22µs |  |  | my $class = shift;
116 |  |  | 4 | 41µs | my $name = $class->_class(shift) or return undef; # spent 41µs making 4 calls to Class::Inspector::_class, avg 10µs/call
117 |  |  | 4 | 39µs | $class->_loaded($name); # spent 39µs making 4 calls to Class::Inspector::_loaded, avg 10µs/call
118 |  |  |  |  | }
119 |  |  |  |  |
120 |  |  |  |  | # spent 39µs within Class::Inspector::_loaded which was called 4 times, avg 10µs/call: # 4 times (39µs+0s) by Class::Inspector::loaded at line 117, avg 10µs/call sub _loaded {
121 | 20 | 26µs |  |  | my $class = shift;
122 |  |  |  |  | my $name = shift;
123 |  |  |  |  |
124 |  |  |  |  | # Handle by far the two most common cases
125 |  |  |  |  | # This is very fast and handles 99% of cases.
126 |  |  |  |  | return 1 if defined ${"${name}::VERSION"};
127 |  |  |  |  | return 1 if @{"${name}::ISA"};
128 |  |  |  |  |
129 |  |  |  |  | # Are there any symbol table entries other than other namespaces
130 |  |  |  |  | foreach ( keys %{"${name}::"} ) {
131 | 8 | 20µs |  |  | next if substr($_, -2, 2) eq '::';
132 |  |  |  |  | return 1 if defined &{"${name}::$_"};
133 |  |  |  |  | }
134 |  |  |  |  |
135 |  |  |  |  | # No functions, and it doesn't have a version, and isn't anything.
136 |  |  |  |  | # As an absolute last resort, check for an entry in %INC
137 |  |  |  |  | my $filename = $class->_inc_filename($name);
138 |  |  |  |  | return 1 if defined $INC{$filename};
139 |  |  |  |  |
140 |  |  |  |  | '';
141 |  |  |  |  | }
142 |  |  |  |  |
143 |  |  |  |  | =pod
144 |  |  |  |  |
145 |  |  |  |  | =head2 filename $class
146 |  |  |  |  |
147 |  |  |  |  | For a given class, returns the base filename for the class. This will NOT
148 |  |  |  |  | be a fully resolved filename, just the part of the filename BELOW the
149 |  |  |  |  | C<@INC> entry.
150 |  |  |  |  |
151 |  |  |  |  | print Class->filename( 'Foo::Bar' );
152 |  |  |  |  | > Foo/Bar.pm
153 |  |  |  |  |
154 |  |  |  |  | This filename will be returned with the right seperator for the local
155 |  |  |  |  | platform, and should work on all platforms.
156 |  |  |  |  |
157 |  |  |  |  | Returns the filename on success or C<undef> if the class name is invalid.
158 |  |  |  |  |
159 |  |  |  |  | =cut
160 |  |  |  |  |
161 |  |  |  |  | # spent 102µs (23+78) within Class::Inspector::filename which was called 2 times, avg 51µs/call: # 2 times (23µs+78µs) by File::ShareDir::_module_dir_old at line 274 of File/ShareDir.pm, avg 51µs/call sub filename {
162 | 6 | 21µs |  |  | my $class = shift;
163 |  |  | 2 | 12µs | my $name = $class->_class(shift) or return undef; # spent 12µs making 2 calls to Class::Inspector::_class, avg 6µs/call
164 |  |  | 2 | 66µs | File::Spec->catfile( split /(?:\'|::)/, $name ) . '.pm'; # spent 66µs making 2 calls to File::Spec::Unix::catfile, avg 33µs/call
165 |  |  |  |  | }
166 |  |  |  |  |
167 |  |  |  |  | =pod
168 |  |  |  |  |
169 |  |  |  |  | =head2 resolved_filename $class, @try_first
170 |  |  |  |  |
171 |  |  |  |  | For a given class, the C<resolved_filename> static method returns the fully
172 |  |  |  |  | resolved filename for a class. That is, the file that the class would be
173 |  |  |  |  | loaded from.
174 |  |  |  |  |
175 |  |  |  |  | This is not nescesarily the file that the class WAS loaded from, as the
176 |  |  |  |  | value returned is determined each time it runs, and the C<@INC> include
177 |  |  |  |  | path may change.
178 |  |  |  |  |
179 |  |  |  |  | To get the actual file for a loaded class, see the C<loaded_filename>
180 |  |  |  |  | method.
181 |  |  |  |  |
182 |  |  |  |  | Returns the filename for the class, or C<undef> if the class name is
183 |  |  |  |  | invalid.
184 |  |  |  |  |
185 |  |  |  |  | =cut
186 |  |  |  |  |
187 |  |  |  |  | sub resolved_filename {
188 |  |  |  |  | my $class = shift;
189 |  |  |  |  | my $filename = $class->_inc_filename(shift) or return undef;
190 |  |  |  |  | my @try_first = @_;
191 |  |  |  |  |
192 |  |  |  |  | # Look through the @INC path to find the file
193 |  |  |  |  | foreach ( @try_first, @INC ) {
194 |  |  |  |  | my $full = "$_/$filename";
195 |  |  |  |  | next unless -e $full;
196 |  |  |  |  | return $UNIX ? $full : $class->_inc_to_local($full);
197 |  |  |  |  | }
198 |  |  |  |  |
199 |  |  |  |  | # File not found
200 |  |  |  |  | '';
201 |  |  |  |  | }
202 |  |  |  |  |
203 |  |  |  |  | =pod
204 |  |  |  |  |
205 |  |  |  |  | =head2 loaded_filename $class
206 |  |  |  |  |
207 |  |  |  |  | For a given loaded class, the C<loaded_filename> static method determines
208 |  |  |  |  | (via the C<%INC> hash) the name of the file that it was originally loaded
209 |  |  |  |  | from.
210 |  |  |  |  |
211 |  |  |  |  | Returns a resolved file path, or false if the class did not have it's own
212 |  |  |  |  | file.
213 |  |  |  |  |
214 |  |  |  |  | =cut
215 |  |  |  |  |
216 |  |  |  |  | # spent 36µs (12+25) within Class::Inspector::loaded_filename which was called 2 times, avg 18µs/call: # 2 times (12µs+25µs) by File::ShareDir::_module_dir_old at line 275 of File/ShareDir.pm, avg 18µs/call sub loaded_filename {
217 | 6 | 11µs |  |  | my $class = shift;
218 |  |  | 2 | 25µs | my $filename = $class->_inc_filename(shift); # spent 25µs making 2 calls to Class::Inspector::_inc_filename, avg 12µs/call
219 |  |  |  |  | $UNIX ? $INC{$filename} : $class->_inc_to_local($INC{$filename});
220 |  |  |  |  | }
221 |  |  |  |  |
- - |  |  |  |  |
226 |  |  |  |  | #####################################################################
227 |  |  |  |  | # Sub Related Methods
228 |  |  |  |  |
229 |  |  |  |  | =pod
230 |  |  |  |  |
231 |  |  |  |  | =head2 functions $class
232 |  |  |  |  |
233 |  |  |  |  | For a loaded class, the C<functions> static method returns a list of the
234 |  |  |  |  | names of all the functions in the classes immediate namespace.
235 |  |  |  |  |
236 |  |  |  |  | Note that this is not the METHODS of the class, just the functions.
237 |  |  |  |  |
238 |  |  |  |  | Returns a reference to an array of the function names on success, or C<undef>
239 |  |  |  |  | if the class name is invalid or the class is not loaded.
240 |  |  |  |  |
241 |  |  |  |  | =cut
242 |  |  |  |  |
243 |  |  |  |  | sub functions {
244 |  |  |  |  | my $class = shift;
245 |  |  |  |  | my $name = $class->_class(shift) or return undef;
246 |  |  |  |  | return undef unless $class->loaded( $name );
247 |  |  |  |  |
248 |  |  |  |  | # Get all the CODE symbol table entries
249 |  |  |  |  | my @functions = sort grep { /$RE_IDENTIFIER/o }
250 |  |  |  |  | grep { defined &{"${name}::$_"} }
251 |  |  |  |  | keys %{"${name}::"};
252 |  |  |  |  | \@functions;
253 |  |  |  |  | }
254 |  |  |  |  |
255 |  |  |  |  | =pod
256 |  |  |  |  |
257 |  |  |  |  | =head2 function_refs $class
258 |  |  |  |  |
259 |  |  |  |  | For a loaded class, the C<function_refs> static method returns references to
260 |  |  |  |  | all the functions in the classes immediate namespace.
261 |  |  |  |  |
262 |  |  |  |  | Note that this is not the METHODS of the class, just the functions.
263 |  |  |  |  |
264 |  |  |  |  | Returns a reference to an array of C<CODE> refs of the functions on
265 |  |  |  |  | success, or C<undef> if the class is not loaded.
266 |  |  |  |  |
267 |  |  |  |  | =cut
268 |  |  |  |  |
269 |  |  |  |  | sub function_refs {
270 |  |  |  |  | my $class = shift;
271 |  |  |  |  | my $name = $class->_class(shift) or return undef;
272 |  |  |  |  | return undef unless $class->loaded( $name );
273 |  |  |  |  |
274 |  |  |  |  | # Get all the CODE symbol table entries, but return
275 |  |  |  |  | # the actual CODE refs this time.
276 |  |  |  |  | my @functions = map { \&{"${name}::$_"} }
277 |  |  |  |  | sort grep { /$RE_IDENTIFIER/o }
278 |  |  |  |  | grep { defined &{"${name}::$_"} }
279 |  |  |  |  | keys %{"${name}::"};
280 |  |  |  |  | \@functions;
281 |  |  |  |  | }
282 |  |  |  |  |
283 |  |  |  |  | =pod
284 |  |  |  |  |
285 |  |  |  |  | =head2 function_exists $class, $function
286 |  |  |  |  |
287 |  |  |  |  | Given a class and function name the C<function_exists> static method will
288 |  |  |  |  | check to see if the function exists in the class.
289 |  |  |  |  |
290 |  |  |  |  | Note that this is as a function, not as a method. To see if a method
291 |  |  |  |  | exists for a class, use the C<can> method for any class or object.
292 |  |  |  |  |
293 |  |  |  |  | Returns true if the function exists, false if not, or C<undef> if the
294 |  |  |  |  | class or function name are invalid, or the class is not loaded.
295 |  |  |  |  |
296 |  |  |  |  | =cut
297 |  |  |  |  |
298 |  |  |  |  | sub function_exists {
299 |  |  |  |  | my $class = shift;
300 |  |  |  |  | my $name = $class->_class( shift ) or return undef;
301 |  |  |  |  | my $function = shift or return undef;
302 |  |  |  |  |
303 |  |  |  |  | # Only works if the class is loaded
304 |  |  |  |  | return undef unless $class->loaded( $name );
305 |  |  |  |  |
306 |  |  |  |  | # Does the GLOB exist and its CODE part exist
307 |  |  |  |  | defined &{"${name}::$function"};
308 |  |  |  |  | }
309 |  |  |  |  |
310 |  |  |  |  | =pod
311 |  |  |  |  |
312 |  |  |  |  | =head2 methods $class, @options
313 |  |  |  |  |
314 |  |  |  |  | For a given class name, the C<methods> static method will returns ALL
315 |  |  |  |  | the methods available to that class. This includes all methods available
316 |  |  |  |  | from every class up the class' C<@ISA> tree.
317 |  |  |  |  |
318 |  |  |  |  | Returns a reference to an array of the names of all the available methods
319 |  |  |  |  | on success, or C<undef> if the class name is invalid or the class is not
320 |  |  |  |  | loaded.
321 |  |  |  |  |
322 |  |  |  |  | A number of options are available to the C<methods> method that will alter
323 |  |  |  |  | the results returned. These should be listed after the class name, in any
324 |  |  |  |  | order.
325 |  |  |  |  |
326 |  |  |  |  | # Only get public methods
327 |  |  |  |  | my $method = Class::Inspector->methods( 'My::Class', 'public' );
328 |  |  |  |  |
329 |  |  |  |  | =over 4
330 |  |  |  |  |
331 |  |  |  |  | =item public
332 |  |  |  |  |
333 |  |  |  |  | The C<public> option will return only 'public' methods, as defined by the Perl
334 |  |  |  |  | convention of prepending an underscore to any 'private' methods. The C<public>
335 |  |  |  |  | option will effectively remove any methods that start with an underscore.
336 |  |  |  |  |
337 |  |  |  |  | =item private
338 |  |  |  |  |
339 |  |  |  |  | The C<private> options will return only 'private' methods, as defined by the
340 |  |  |  |  | Perl convention of prepending an underscore to an private methods. The
341 |  |  |  |  | C<private> option will effectively remove an method that do not start with an
342 |  |  |  |  | underscore.
343 |  |  |  |  |
344 |  |  |  |  | B<Note: The C<public> and C<private> options are mutually exclusive>
345 |  |  |  |  |
346 |  |  |  |  | =item full
347 |  |  |  |  |
348 |  |  |  |  | C<methods> normally returns just the method name. Supplying the C<full> option
349 |  |  |  |  | will cause the methods to be returned as the full names. That is, instead of
350 |  |  |  |  | returning C<[ 'method1', 'method2', 'method3' ]>, you would instead get
351 |  |  |  |  | C<[ 'Class::method1', 'AnotherClass::method2', 'Class::method3' ]>.
352 |  |  |  |  |
353 |  |  |  |  | =item expanded
354 |  |  |  |  |
355 |  |  |  |  | The C<expanded> option will cause a lot more information about method to be
356 |  |  |  |  | returned. Instead of just the method name, you will instead get an array
357 |  |  |  |  | reference containing the method name as a single combined name, ala C<full>,
358 |  |  |  |  | the seperate class and method, and a CODE ref to the actual function ( if
359 |  |  |  |  | available ). Please note that the function reference is not guarenteed to
360 |  |  |  |  | be available. C<Class::Inspector> is intended at some later time, work
361 |  |  |  |  | with modules that have some some of common run-time loader in place ( e.g
362 |  |  |  |  | C<Autoloader> or C<Class::Autouse> for example.
363 |  |  |  |  |
364 |  |  |  |  | The response from C<methods( 'Class', 'expanded' )> would look something like
365 |  |  |  |  | the following.
366 |  |  |  |  |
367 |  |  |  |  | [
368 |  |  |  |  | [ 'Class::method1', 'Class', 'method1', \&Class::method1 ],
369 |  |  |  |  | [ 'Another::method2', 'Another', 'method2', \&Another::method2 ],
370 |  |  |  |  | [ 'Foo::bar', 'Foo', 'bar', \&Foo::bar ],
371 |  |  |  |  | ]
372 |  |  |  |  |
373 |  |  |  |  | =back
374 |  |  |  |  |
375 |  |  |  |  | =cut
376 |  |  |  |  |
377 |  |  |  |  | sub methods {
378 |  |  |  |  | my $class = shift;
379 |  |  |  |  | my $name = $class->_class( shift ) or return undef;
380 |  |  |  |  | my @arguments = map { lc $_ } @_;
381 |  |  |  |  |
382 |  |  |  |  | # Process the arguments to determine the options
383 |  |  |  |  | my %options = ();
384 |  |  |  |  | foreach ( @arguments ) {
385 |  |  |  |  | if ( $_ eq 'public' ) {
386 |  |  |  |  | # Only get public methods
387 |  |  |  |  | return undef if $options{private};
388 |  |  |  |  | $options{public} = 1;
389 |  |  |  |  |
390 |  |  |  |  | } elsif ( $_ eq 'private' ) {
391 |  |  |  |  | # Only get private methods
392 |  |  |  |  | return undef if $options{public};
393 |  |  |  |  | $options{private} = 1;
394 |  |  |  |  |
395 |  |  |  |  | } elsif ( $_ eq 'full' ) {
396 |  |  |  |  | # Return the full method name
397 |  |  |  |  | return undef if $options{expanded};
398 |  |  |  |  | $options{full} = 1;
399 |  |  |  |  |
400 |  |  |  |  | } elsif ( $_ eq 'expanded' ) {
401 |  |  |  |  | # Returns class, method and function ref
402 |  |  |  |  | return undef if $options{full};
403 |  |  |  |  | $options{expanded} = 1;
404 |  |  |  |  |
405 |  |  |  |  | } else {
406 |  |  |  |  | # Unknown or unsupported options
407 |  |  |  |  | return undef;
408 |  |  |  |  | }
409 |  |  |  |  | }
410 |  |  |  |  |
411 |  |  |  |  | # Only works if the class is loaded
412 |  |  |  |  | return undef unless $class->loaded( $name );
413 |  |  |  |  |
414 |  |  |  |  | # Get the super path ( not including UNIVERSAL )
415 |  |  |  |  | # Rather than using Class::ISA, we'll use an inlined version
416 |  |  |  |  | # that implements the same basic algorithm.
417 |  |  |  |  | my @path = ();
418 |  |  |  |  | my @queue = ( $name );
419 |  |  |  |  | my %seen = ( $name => 1 );
420 |  |  |  |  | while ( my $cl = shift @queue ) {
421 |  |  |  |  | push @path, $cl;
422 |  |  |  |  | unshift @queue, grep { ! $seen{$_}++ }
423 |  |  |  |  | map { s/^::/main::/; s/\'/::/g; $_ }
424 |  |  |  |  | ( @{"${cl}::ISA"} );
425 |  |  |  |  | }
426 |  |  |  |  |
427 |  |  |  |  | # Find and merge the function names across the entire super path.
428 |  |  |  |  | # Sort alphabetically and return.
429 |  |  |  |  | my %methods = ();
430 |  |  |  |  | foreach my $namespace ( @path ) {
431 |  |  |  |  | my @functions = grep { ! $methods{$_} }
432 |  |  |  |  | grep { /$RE_IDENTIFIER/o }
433 |  |  |  |  | grep { defined &{"${namespace}::$_"} }
434 |  |  |  |  | keys %{"${namespace}::"};
435 |  |  |  |  | foreach ( @functions ) {
436 |  |  |  |  | $methods{$_} = $namespace;
437 |  |  |  |  | }
438 |  |  |  |  | }
439 |  |  |  |  |
440 |  |  |  |  | # Filter to public or private methods if needed
441 |  |  |  |  | my @methodlist = sort keys %methods;
442 |  |  |  |  | @methodlist = grep { ! /^\_/ } @methodlist if $options{public};
443 |  |  |  |  | @methodlist = grep { /^\_/ } @methodlist if $options{private};
444 |  |  |  |  |
445 |  |  |  |  | # Return in the correct format
446 |  |  |  |  | @methodlist = map { "$methods{$_}::$_" } @methodlist if $options{full};
447 |  |  |  |  | @methodlist = map {
448 |  |  |  |  | [ "$methods{$_}::$_", $methods{$_}, $_, \&{"$methods{$_}::$_"} ]
449 |  |  |  |  | } @methodlist if $options{expanded};
450 |  |  |  |  |
451 |  |  |  |  | \@methodlist;
452 |  |  |  |  | }
453 |  |  |  |  |
- - |  |  |  |  |
458 |  |  |  |  | #####################################################################
459 |  |  |  |  | # Search Methods
460 |  |  |  |  |
461 |  |  |  |  | =pod
462 |  |  |  |  |
463 |  |  |  |  | =head2 subclasses $class
464 |  |  |  |  |
465 |  |  |  |  | The C<subclasses> static method will search then entire namespace (and thus
466 |  |  |  |  | B<all> currently loaded classes) to find all classes that are subclasses
467 |  |  |  |  | of the class provided as a the parameter.
468 |  |  |  |  |
469 |  |  |  |  | The actual test will be done by calling C<isa> on the class as a static
470 |  |  |  |  | method. (i.e. C<My::Class-E<gt>isa($class)>.
471 |  |  |  |  |
472 |  |  |  |  | Returns a reference to a list of the loaded classes that match the class
473 |  |  |  |  | provided, or false is none match, or C<undef> if the class name provided
474 |  |  |  |  | is invalid.
475 |  |  |  |  |
476 |  |  |  |  | =cut
477 |  |  |  |  |
478 |  |  |  |  | sub subclasses {
479 |  |  |  |  | my $class = shift;
480 |  |  |  |  | my $name = $class->_class( shift ) or return undef;
481 |  |  |  |  |
482 |  |  |  |  | # Prepare the search queue
483 |  |  |  |  | my @found = ();
484 |  |  |  |  | my @queue = grep { $_ ne 'main' } $class->_subnames('');
485 |  |  |  |  | while ( @queue ) {
486 |  |  |  |  | my $c = shift(@queue); # c for class
487 |  |  |  |  | if ( $class->_loaded($c) ) {
488 |  |  |  |  | # At least one person has managed to misengineer
489 |  |  |  |  | # a situation in which ->isa could die, even if the
490 |  |  |  |  | # class is real. Trap these cases and just skip
491 |  |  |  |  | # over that (bizarre) class. That would at limit
492 |  |  |  |  | # problems with finding subclasses to only the
493 |  |  |  |  | # modules that have broken ->isa implementation.
494 |  |  |  |  | local $@;
495 |  |  |  |  | eval {
496 |  |  |  |  | if ( $c->isa($name) ) {
497 |  |  |  |  | # Add to the found list, but don't add the class itself
498 |  |  |  |  | push @found, $c unless $c eq $name;
499 |  |  |  |  | }
500 |  |  |  |  | };
501 |  |  |  |  | }
502 |  |  |  |  |
503 |  |  |  |  | # Add any child namespaces to the head of the queue.
504 |  |  |  |  | # This keeps the queue length shorted, and allows us
505 |  |  |  |  | # not to have to do another sort at the end.
506 |  |  |  |  | unshift @queue, map { "${c}::$_" } $class->_subnames($c);
507 |  |  |  |  | }
508 |  |  |  |  |
509 |  |  |  |  | @found ? \@found : '';
510 |  |  |  |  | }
511 |  |  |  |  |
512 |  |  |  |  | sub _subnames {
513 |  |  |  |  | my ($class, $name) = @_;
514 |  |  |  |  | return sort
515 |  |  |  |  | grep {
516 |  |  |  |  | substr($_, -2, 2, '') eq '::'
517 |  |  |  |  | and
518 |  |  |  |  | /$RE_IDENTIFIER/o
519 |  |  |  |  | }
520 |  |  |  |  | keys %{"${name}::"};
521 |  |  |  |  | }
522 |  |  |  |  |
- - |  |  |  |  |
527 |  |  |  |  | #####################################################################
528 |  |  |  |  | # Children Related Methods
529 |  |  |  |  |
530 |  |  |  |  | # These can go undocumented for now, until I decide if its best to
531 |  |  |  |  | # just search the children in namespace only, or if I should do it via
532 |  |  |  |  | # the file system.
533 |  |  |  |  |
534 |  |  |  |  | # Find all the loaded classes below us
535 |  |  |  |  | sub children {
536 |  |  |  |  | my $class = shift;
537 |  |  |  |  | my $name = $class->_class(shift) or return ();
538 |  |  |  |  |
539 |  |  |  |  | # Find all the Foo:: elements in our symbol table
540 | 3 | 79µs | 2 | 37µs | # spent 23µs (9+14) within Class::Inspector::BEGIN@540 which was called: # once (9µs+14µs) by File::ShareDir::BEGIN@114 at line 540 no strict 'refs'; # spent 23µs making 1 call to Class::Inspector::BEGIN@540 # spent 14µs making 1 call to strict::unimport
541 |  |  |  |  | map { "${name}::$_" } sort grep { s/::$// } keys %{"${name}::"};
542 |  |  |  |  | }
543 |  |  |  |  |
544 |  |  |  |  | # As above, but recursively
545 |  |  |  |  | sub recursive_children {
546 |  |  |  |  | my $class = shift;
547 |  |  |  |  | my $name = $class->_class(shift) or return ();
548 |  |  |  |  | my @children = ( $name );
549 |  |  |  |  |
550 |  |  |  |  | # Do the search using a nicer, more memory efficient
551 |  |  |  |  | # variant of actual recursion.
552 |  |  |  |  | my $i = 0;
553 | 3 | 240µs | 2 | 23µs | # spent 15µs (7+8) within Class::Inspector::BEGIN@553 which was called: # once (7µs+8µs) by File::ShareDir::BEGIN@114 at line 553 no strict 'refs'; # spent 15µs making 1 call to Class::Inspector::BEGIN@553 # spent 8µs making 1 call to strict::unimport
554 |  |  |  |  | while ( my $namespace = $children[$i++] ) {
555 |  |  |  |  | push @children, map { "${namespace}::$_" }
556 |  |  |  |  | grep { ! /^::/ } # Ignore things like ::ISA::CACHE::
557 |  |  |  |  | grep { s/::$// }
558 |  |  |  |  | keys %{"${namespace}::"};
559 |  |  |  |  | }
560 |  |  |  |  |
561 |  |  |  |  | sort @children;
562 |  |  |  |  | }
563 |  |  |  |  |
- - |  |  |  |  |
568 |  |  |  |  | #####################################################################
569 |  |  |  |  | # Private Methods
570 |  |  |  |  |
571 |  |  |  |  | # Checks and expands ( if needed ) a class name
572 |  |  |  |  | # spent 65µs (46+19) within Class::Inspector::_class which was called 8 times, avg 8µs/call: # 4 times (30µs+11µs) by Class::Inspector::loaded at line 116, avg 10µs/call # 2 times (8µs+4µs) by Class::Inspector::filename at line 163, avg 6µs/call # 2 times (8µs+4µs) by Class::Inspector::_inc_filename at line 588, avg 6µs/call sub _class {
573 | 40 | 73µs |  |  | my $class = shift;
574 |  |  |  |  | my $name = shift or return '';
575 |  |  |  |  |
576 |  |  |  |  | # Handle main shorthand
577 |  |  |  |  | return 'main' if $name eq '::';
578 |  |  | 8 | 2µs | $name =~ s/\A::/main::/; # spent 2µs making 8 calls to Class::Inspector::CORE:subst, avg 288ns/call
579 |  |  |  |  |
580 |  |  |  |  | # Check the class name is valid
581 |  |  | 9 | 17µs | $name =~ /$RE_CLASS/o ? $name : ''; # spent 15µs making 8 calls to Class::Inspector::CORE:match, avg 2µs/call # spent 1µs making 1 call to Class::Inspector::CORE:regcomp
582 |  |  |  |  | }
583 |  |  |  |  |
584 |  |  |  |  | # Create a INC-specific filename, which always uses '/'
585 |  |  |  |  | # regardless of platform.
586 |  |  |  |  | # spent 25µs (13+12) within Class::Inspector::_inc_filename which was called 2 times, avg 12µs/call: # 2 times (13µs+12µs) by Class::Inspector::loaded_filename at line 218, avg 12µs/call sub _inc_filename {
587 | 6 | 13µs |  |  | my $class = shift;
588 |  |  | 2 | 12µs | my $name = $class->_class(shift) or return undef; # spent 12µs making 2 calls to Class::Inspector::_class, avg 6µs/call
589 |  |  |  |  | join( '/', split /(?:\'|::)/, $name ) . '.pm';
590 |  |  |  |  | }
591 |  |  |  |  |
592 |  |  |  |  | # Convert INC-specific file name to local file name
593 |  |  |  |  | sub _inc_to_local {
594 |  |  |  |  | # Shortcut in the Unix case
595 |  |  |  |  | return $_[1] if $UNIX;
596 |  |  |  |  |
597 |  |  |  |  | # On other places, we have to deal with an unusual path that might look
598 |  |  |  |  | # like C:/foo/bar.pm which doesn't fit ANY normal pattern.
599 |  |  |  |  | # Putting it through splitpath/dir and back again seems to normalise
600 |  |  |  |  | # it to a reasonable amount.
601 |  |  |  |  | my $class = shift;
602 |  |  |  |  | my $inc_name = shift or return undef;
603 |  |  |  |  | my ($vol, $dir, $file) = File::Spec->splitpath( $inc_name );
604 |  |  |  |  | $dir = File::Spec->catdir( File::Spec->splitdir( $dir || "" ) );
605 |  |  |  |  | File::Spec->catpath( $vol, $dir, $file || "" );
606 |  |  |  |  | }
607 |  |  |  |  |
608 | 1 | 2µs |  |  | 1;
609 |  |  |  |  |
610 |  |  |  |  | =pod
611 |  |  |  |  |
612 |  |  |  |  | =head1 SUPPORT
613 |  |  |  |  |
614 |  |  |  |  | Bugs should be reported via the CPAN bug tracker
615 |  |  |  |  |
616 |  |  |  |  | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Inspector>
617 |  |  |  |  |
618 |  |  |  |  | For other issues, or commercial enhancement or support, contact the author.
619 |  |  |  |  |
620 |  |  |  |  | =head1 AUTHOR
621 |  |  |  |  |
622 |  |  |  |  | Adam Kennedy E<lt>adamk@cpan.orgE<gt>
623 |  |  |  |  |
624 |  |  |  |  | =head1 SEE ALSO
625 |  |  |  |  |
626 |  |  |  |  | L<http://ali.as/>, L<Class::Handle>
627 |  |  |  |  |
628 |  |  |  |  | =head1 COPYRIGHT
629 |  |  |  |  |
630 |  |  |  |  | Copyright 2002 - 2012 Adam Kennedy.
631 |  |  |  |  |
632 |  |  |  |  | This program is free software; you can redistribute
633 |  |  |  |  | it and/or modify it under the same terms as Perl itself.
634 |  |  |  |  |
635 |  |  |  |  | The full text of the license can be found in the
636 |  |  |  |  | LICENSE file included with this module.
637 |  |  |  |  |
638 |  |  |  |  | =cut
|  |  |  |  | # spent 15µs within Class::Inspector::CORE:match which was called 8 times, avg 2µs/call: # 8 times (15µs+0s) by Class::Inspector::_class at line 581, avg 2µs/call sub Class::Inspector::CORE:match; # opcode
|  |  |  |  | # spent 6µs within Class::Inspector::CORE:qr which was called 2 times, avg 3µs/call: # once (6µs+0s) by Class::Inspector::BEGIN@51 at line 63 # once (1µs+0s) by Class::Inspector::BEGIN@51 at line 64 sub Class::Inspector::CORE:qr; # opcode
|  |  |  |  | # spent 1µs within Class::Inspector::CORE:regcomp which was called: # once (1µs+0s) by Class::Inspector::_class at line 581 sub Class::Inspector::CORE:regcomp; # opcode
|  |  |  |  | # spent 2µs within Class::Inspector::CORE:subst which was called 8 times, avg 288ns/call: # 8 times (2µs+0s) by Class::Inspector::_class at line 578, avg 288ns/call sub Class::Inspector::CORE:subst; # opcode