man Class::Inspector () - Get information about a class and its structure
NAME
Class::Inspector - Get information about a class and its structure
SYNOPSIS
use Class::Inspector;
# Is a class installed and/or loaded Class::Inspector->installed( 'Foo::Class' ); Class::Inspector->loaded( 'Foo::Class' );
# Filename related information Class::Inspector->filename( 'Foo::Class' ); Class::Inspector->resolved_filename( 'Foo::Class' );
# Get subroutine related information Class::Inspector->functions( 'Foo::Class' ); Class::Inspector->function_refs( 'Foo::Class' ); Class::Inspector->function_exists( 'Foo::Class', 'bar' ); Class::Inspector->methods( 'Foo::Class', 'full', 'public' );
# Find all loaded subclasses or something Class::Inspector->subclasses( 'Foo::Class' );
DESCRIPTION
Class::Inspector allows you to get information about a loaded class. Most or all of this information can be found in other ways, but they arn't always very friendly, and usually involve a relatively high level of Perl wizardry, or strange and unusual looking code. Class::Inspector attempts to provide an easier, more friendly interface to this information.
METHODS
The CWinstalled static method tries to determine if a class is installed on the machine, or at least available to Perl. It does this by wrapping around CWresolved_filename.
Returns true if installed/available, false if the class is not installed, or CWundef if the class name is invalid. The CWloaded static method tries to determine if a class is loaded by looking for symbol table entries.
This method it uses to determine this will work even if the class does not have its own file, but is contained inside a single file with multiple classes in it. Even in the case of some sort of run-time loading class being used, these typically leave some trace in the symbol table, so an Autoload or Class::Autouse-based class should correctly appear loaded.
Returns true if the class is loaded, false if not, or CWundef if the class name is invalid. For a given class, returns the base filename for the class. This will NOT be a fully resolved filename, just the part of the filename BELOW the CW@INC entry.
print Class->filename( 'Foo::Bar' ); > Foo/Bar.pm
This filename will be returned with the right seperator for the local platform, and should work on all platforms.
Returns the filename on success or CWundef if the class name is invalid. For a given class, the CWresolved_filename static method returns the fully resolved filename for a class. That is, the file that the class would be loaded from.
This is not nescesarily the file that the class WAS loaded from, as the value returned is determined each time it runs, and the CW@INC include path may change.
To get the actual file for a loaded class, see the CWloaded_filename method.
Returns the filename for the class, or CWundef if the class name is invalid. For a given loaded class, the CWloaded_filename static method determines (via the CW%INC hash) the name of the file that it was originally loaded from.
Returns a resolved file path, or false if the class did not have it's own file. For a loaded class, the CWfunctions static method returns a list of the names of all the functions in the classes immediate namespace.
Note that this is not the METHODS of the class, just the functions.
Returns a reference to an array of the function names on success, or CWundef if the class name is invalid or the class is not loaded. For a loaded class, the CWfunction_refs static method returns references to all the functions in the classes immediate namespace.
Note that this is not the METHODS of the class, just the functions.
Returns a reference to an array of CWCODE refs of the functions on success, or CWundef if the class is not loaded. Given a class and function name the CWfunction_exists static method will check to see if the function exists in the class.
Note that this is as a function, not as a method. To see if a method exists for a class, use the CWcan method for any class or object.
Returns true if the function exists, false if not, or CWundef if the class or function name are invalid, or the class is not loaded. For a given class name, the CWmethods static method will returns ALL the methods available to that class. This includes all methods available from every class up the class' CW@ISA tree.
Returns a reference to an array of the names of all the available methods on success, or CWundef if the class name is invalid or the class is not loaded.
A number of options are available to the CWmethods method that will alter the results returned. These should be listed after the class name, in any order.
# Only get public methods my $method = Class::Inspector->methods( 'My::Class', 'public' );
- public
- The CWpublic option will return only 'public' methods, as defined by the Perl convention of prepending an underscore to any 'private' methods. The CWpublic option will effectively remove any methods that start with an underscore.
- private
- The CWprivate options will return only 'private' methods, as defined by the Perl convention of prepending an underscore to an private methods. The CWprivate option will effectively remove an method that do not start with an underscore. Note: The CBpublic and CBprivate options are mutually exclusive
- full
- CWmethods normally returns just the method name. Supplying the CWfull option will cause the methods to be returned as the full names. That is, instead of returning CW[ 'method1', 'method2', 'method3' ], you would instead get CW[ 'Class::method1', 'AnotherClass::method2', 'Class::method3' ].
- expanded
-
The CWexpanded option will cause a lot more information about method to be
returned. Instead of just the method name, you will instead get an array
reference containing the method name as a single combined name, ala CWfull,
the seperate class and method, and a CODE ref to the actual function ( if
available ). Please note that the function reference is not guarenteed to
be available. CWClass::Inspector is intended at some later time, work
with modules that have some some of common run-time loader in place ( e.g
CWAutoloader or CWClass::Autouse for example.
The response from CWmethods( 'Class', 'expanded' ) would look something like
the following.
[ [ 'Class::method1', 'Class', 'method1', \&Class::method1 ], [ 'Another::method2', 'Another', 'method2', \&Another::method2 ], [ 'Foo::bar', 'Foo', 'bar', \&Foo::bar ], ]
The CWsubclasses static method will search then entire namespace (and thus all currently loaded classes) to find all classes that are subclasses of the class provided as a the parameter.
The actual test will be done by calling CWisa on the class as a static method. (i.e. CWMy::Class->isa($class).
Returns a reference to a list of the loaded classes that match the class provided, or false is none match, or CWundef if the class name provided is invalid.
TO DO
- Adding Class::Inspector::Functions
SUPPORT
Bugs should be reported via the CPAN bug tracker
<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Inspector>
For other issues, or commercial enhancement or support, contact the author.
AUTHOR
Adam Kennedy, <http://ali.as/>, cpan@ali.as
SEE ALSO
Class::Handle, which wraps this one
COPYRIGHT
Copyright (c) 2002 - 2004 Adam Kennedy. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.