1Class::Inspector(3) User Contributed Perl Documentation Class::Inspector(3)
2
3
4
6 Class::Inspector - Get information about a class and its structure
7
9 use Class::Inspector;
10
11 # Is a class installed and/or loaded
12 Class::Inspector->installed( 'Foo::Class' );
13 Class::Inspector->loaded( 'Foo::Class' );
14
15 # Filename related information
16 Class::Inspector->filename( 'Foo::Class' );
17 Class::Inspector->resolved_filename( 'Foo::Class' );
18
19 # Get subroutine related information
20 Class::Inspector->functions( 'Foo::Class' );
21 Class::Inspector->function_refs( 'Foo::Class' );
22 Class::Inspector->function_exists( 'Foo::Class', 'bar' );
23 Class::Inspector->methods( 'Foo::Class', 'full', 'public' );
24
25 # Find all loaded subclasses or something
26 Class::Inspector->subclasses( 'Foo::Class' );
27
29 Class::Inspector allows you to get information about a loaded class.
30 Most or all of this information can be found in other ways, but they
31 arn't always very friendly, and usually involve a relatively high level
32 of Perl wizardry, or strange and unusual looking code. Class::Inspector
33 attempts to provide an easier, more friendly interface to this informa‐
34 tion.
35
37 installed $class
38
39 The "installed" static method tries to determine if a class is
40 installed on the machine, or at least available to Perl. It does this
41 by wrapping around "resolved_filename".
42
43 Returns true if installed/available, false if the class is not
44 installed, or "undef" if the class name is invalid.
45
46 loaded $class
47
48 The "loaded" static method tries to determine if a class is loaded by
49 looking for symbol table entries.
50
51 This method it uses to determine this will work even if the class does
52 not have its own file, but is contained inside a single file with mul‐
53 tiple classes in it. Even in the case of some sort of run-time loading
54 class being used, these typically leave some trace in the symbol table,
55 so an Autoload or Class::Autouse-based class should correctly appear
56 loaded.
57
58 Returns true if the class is loaded, false if not, or "undef" if the
59 class name is invalid.
60
61 filename $class
62
63 For a given class, returns the base filename for the class. This will
64 NOT be a fully resolved filename, just the part of the filename BELOW
65 the @INC entry.
66
67 print Class->filename( 'Foo::Bar' );
68 > Foo/Bar.pm
69
70 This filename will be returned with the right seperator for the local
71 platform, and should work on all platforms.
72
73 Returns the filename on success or "undef" if the class name is
74 invalid.
75
76 resolved_filename $class, @try_first
77
78 For a given class, the "resolved_filename" static method returns the
79 fully resolved filename for a class. That is, the file that the class
80 would be loaded from.
81
82 This is not nescesarily the file that the class WAS loaded from, as the
83 value returned is determined each time it runs, and the @INC include
84 path may change.
85
86 To get the actual file for a loaded class, see the "loaded_filename"
87 method.
88
89 Returns the filename for the class, or "undef" if the class name is
90 invalid.
91
92 loaded_filename $class
93
94 For a given loaded class, the "loaded_filename" static method deter‐
95 mines (via the %INC hash) the name of the file that it was originally
96 loaded from.
97
98 Returns a resolved file path, or false if the class did not have it's
99 own file.
100
101 functions $class
102
103 For a loaded class, the "functions" static method returns a list of the
104 names of all the functions in the classes immediate namespace.
105
106 Note that this is not the METHODS of the class, just the functions.
107
108 Returns a reference to an array of the function names on success, or
109 "undef" if the class name is invalid or the class is not loaded.
110
111 function_refs $class
112
113 For a loaded class, the "function_refs" static method returns refer‐
114 ences to all the functions in the classes immediate namespace.
115
116 Note that this is not the METHODS of the class, just the functions.
117
118 Returns a reference to an array of "CODE" refs of the functions on suc‐
119 cess, or "undef" if the class is not loaded.
120
121 function_exists $class, $function
122
123 Given a class and function name the "function_exists" static method
124 will check to see if the function exists in the class.
125
126 Note that this is as a function, not as a method. To see if a method
127 exists for a class, use the "can" method for any class or object.
128
129 Returns true if the function exists, false if not, or "undef" if the
130 class or function name are invalid, or the class is not loaded.
131
132 methods $class, @options
133
134 For a given class name, the "methods" static method will returns ALL
135 the methods available to that class. This includes all methods avail‐
136 able from every class up the class' @ISA tree.
137
138 Returns a reference to an array of the names of all the available meth‐
139 ods on success, or "undef" if the class name is invalid or the class is
140 not loaded.
141
142 A number of options are available to the "methods" method that will
143 alter the results returned. These should be listed after the class
144 name, in any order.
145
146 # Only get public methods
147 my $method = Class::Inspector->methods( 'My::Class', 'public' );
148
149 public
150 The "public" option will return only 'public' methods, as defined
151 by the Perl convention of prepending an underscore to any 'private'
152 methods. The "public" option will effectively remove any methods
153 that start with an underscore.
154
155 private
156 The "private" options will return only 'private' methods, as
157 defined by the Perl convention of prepending an underscore to an
158 private methods. The "private" option will effectively remove an
159 method that do not start with an underscore.
160
161 Note: The "public" and "private" options are mutually exclusive
162
163 full
164 "methods" normally returns just the method name. Supplying the
165 "full" option will cause the methods to be returned as the full
166 names. That is, instead of returning "[ 'method1', 'method2',
167 'method3' ]", you would instead get "[ 'Class::method1', 'Another‐
168 Class::method2', 'Class::method3' ]".
169
170 expanded
171 The "expanded" option will cause a lot more information about
172 method to be returned. Instead of just the method name, you will
173 instead get an array reference containing the method name as a sin‐
174 gle combined name, ala "full", the seperate class and method, and a
175 CODE ref to the actual function ( if available ). Please note that
176 the function reference is not guarenteed to be available.
177 "Class::Inspector" is intended at some later time, work with mod‐
178 ules that have some some of common run-time loader in place ( e.g
179 "Autoloader" or "Class::Autouse" for example.
180
181 The response from "methods( 'Class', 'expanded' )" would look some‐
182 thing like the following.
183
184 [
185 [ 'Class::method1', 'Class', 'method1', \&Class::method1 ],
186 [ 'Another::method2', 'Another', 'method2', \&Another::method2 ],
187 [ 'Foo::bar', 'Foo', 'bar', \&Foo::bar ],
188 ]
189
190 subclasses $class
191
192 The "subclasses" static method will search then entire namespace (and
193 thus all currently loaded classes) to find all classes that are sub‐
194 classes of the class provided as a the parameter.
195
196 The actual test will be done by calling "isa" on the class as a static
197 method. (i.e. "My::Class->isa($class)".
198
199 Returns a reference to a list of the loaded classes that match the
200 class provided, or false is none match, or "undef" if the class name
201 provided is invalid.
202
204 - Adding Class::Inspector::Functions
205
207 Bugs should be reported via the CPAN bug tracker
208
209 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Inspector>
210
211 For other issues, or commercial enhancement or support, contact the
212 author.
213
215 Adam Kennedy <adamk@cpan.org>
216
218 <http://ali.as/>, Class::Handle
219
221 Copyright 2002 - 2008 Adam Kennedy.
222
223 This program is free software; you can redistribute it and/or modify it
224 under the same terms as Perl itself.
225
226 The full text of the license can be found in the LICENSE file included
227 with this module.
228
229
230
231perl v5.8.8 2008-03-01 Class::Inspector(3)