Class::Inspector(3)

πŸ’… Class::Inspector(3) β€” or, how to snoop on a class without the wizardry

okay so this is the manual page for Class::Inspector, living in section 3 of the perl man pages (the User Contributed Perl Documentation corner of the internet). Buckle up bestie, we're getting to know classes πŸ’–

✨ NAME ✨

Class::Inspector β€” Get information about a class and its structure. That's it, that's the whole vibe check 🫢

πŸŽ€ VERSION πŸŽ€

version 1.36 β€” yes, she's on 1.36, no cap ✨

🌸 SYNOPSIS 🌸

Here's the whole mood board, literally exactly as written:

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' );

like, look at her go πŸ˜­πŸ‘‘

πŸ¦‹ DESCRIPTION πŸ¦‹

So Class::Inspector lets you get information about a loaded class. And yeah, technically most or all of this info can be found in other ways β€” but those ways aren't always very friendly, and they usually involve a relatively high level of Perl wizardry, or straight up strange and unusual looking code (iykyk πŸ’€). Class::Inspector attempts to provide an easier, more friendly interface to this information. She's giving accessibility queen, periodt πŸ’…


⭐ METHODS ⭐

πŸ’œ installed

my $bool = Class::Inspector->installed($class);

The "installed" static method tries to determine if a class is installed on the machine, or at least available to Perl. She does this by wrapping around "resolved_filename".

Returns true if installed/available, false if the class is not installed, or "undef" if the class name is invalid. Three whole outcomes, she's versatile 🌈

πŸ’œ loaded

my $bool = Class::Inspector->loaded($class);

The "loaded" static method tries to determine if a class is loaded by looking for symbol table entries πŸ‘€

The method she 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. icons only ✨

Returns true if the class is loaded, false if not, or "undef" if the class name is invalid.

πŸ’œ filename

my $filename = Class::Inspector->filename($class);

For a given class, returns the base filename for the class. Heads up though: this will NOT be a fully resolved filename, just the part of the filename BELOW the @INC entry. She's giving partial information, on purpose, no cap.

print Class->filename( 'Foo::Bar' );
> Foo/Bar.pm

This filename will be returned with the right separator for the local platform, and should work on all platforms 🌍🫢

Returns the filename on success or "undef" if the class name is invalid.

πŸ’œ resolved_filename

my $filename = Class::Inspector->resolved_filename($class);
my $filename = Class::Inspector->resolved_filename($class, @try_first);

For a given class, the "resolved_filename" static method returns the fully resolved filename for a class. That is, the file that the class would be loaded from. The full thing, the whole package 🎁

Now β€” and this is important β€” this is not necessarily the file that the class WAS loaded from, because the value returned is determined each time it runs, and the @INC include path may change. She's dynamic like that.

To get the actual file for a loaded class, see the "loaded_filename" method (right below, bestie, keep reading πŸ‘‡).

Returns the filename for the class, or "undef" if the class name is invalid.

πŸ’œ loaded_filename

my $filename = Class::Inspector->loaded_filename($class);

For a given loaded class, the "loaded_filename" static method determines (via the %INC hash) the name of the file that it was originally loaded from. Receipts from the %INC hash, literally 🧾

Returns a resolved file path, or false if the class did not have it's own file.

πŸ’œ functions

my $arrayref = Class::Inspector->functions($class);

For a loaded class, the "functions" 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. Big distinction, don't get it twisted πŸ’€

Returns a reference to an array of the function names on success, or "undef" if the class name is invalid or the class is not loaded.

πŸ’œ function_refs

my $arrayref = Class::Inspector->function_refs($class);

For a loaded class, the "function_refs" static method returns references to all the functions in the classes immediate namespace. Now with actual refs, she upgraded πŸ”₯

Again: note that this is not the METHODS of the class, just the functions.

Returns a reference to an array of "CODE" refs of the functions on success, or "undef" if the class is not loaded.

πŸ’œ function_exists

my $bool = Class::Inspector->function_exists($class, $functon);

(yes, $functon, that's exactly how it's written in the source, we do not edit the queen's typo πŸ™ˆ)

Given a class and function name the "function_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 "can" method for any class or object. Different tool, different job 🧰

Returns true if the function exists, false if not, or "undef" if the class or function name are invalid, or the class is not loaded.

πŸ’œ methods

my $arrayref = Class::Inspector->methods($class, @options);

For a given class name, the "methods" static method will returns ALL the methods available to that class. And when we say all, we mean all β€” this includes all methods available from every class up the class' @ISA tree. She went digging through the whole family tree πŸŒ³πŸ‘‘

Returns a reference to an array of the names of all the available methods on success, or "undef" if the class name is invalid or the class is not loaded.

A number of options are available to the "methods" method that will alter the results returned. These should be listed after the class name, in any order β€” she's flexible like that.

# Only get public methods
my $method = Class::Inspector->methods( 'My::Class', 'public' );

πŸ“ public

The "public" option will return only 'public' methods, as defined by the Perl convention of prepending an underscore to any 'private' methods. The "public" option will effectively remove any methods that start with an underscore. Bye bye, _underscore_babes πŸ‘‹

πŸ“ private

The "private" options will return only 'private' methods, as defined by the Perl convention of prepending an underscore to an private methods. The "private" option will effectively remove an method that do not start with an underscore. Only the underscored ones survive πŸ’…

Note: The "public" and "private" options are mutually exclusive. Pick a lane bestie, you literally cannot have both 😭

πŸ“ full

"methods" normally returns just the method name. Supplying the "full" option will cause the methods to be returned as the full names. That is, instead of returning "[ 'method1', 'method2', 'method3' ]", you would instead get "[ 'Class::method1', 'AnotherClass::method2', 'Class::method3' ]". Now with surnames ✨

πŸ“ expanded

The "expanded" 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, a la "full", the separate class and method, and a CODE ref to the actual function ( if available ). Please note that the function reference is not guaranteed to be available. "Class::Inspector" is intended at some later time, to work with modules that have some kind of common run-time loader in place ( e.g "Autoloader" or "Class::Autouse" for example. (that trailing unclosed paren? also canon, we don't touch it 🫢)

The response from "methods( '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         ],
]

Look at all that structure, she's organized, she's got range πŸ”₯

πŸ’œ subclasses

my $arrayref = Class::Inspector->subclasses($class);

The "subclasses" 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. Full-on family reunion search πŸ”Ž

The actual test will be done by calling "isa" on the class as a static method. (i.e. "My::Class->isa($class)".

Returns a reference to a list of the loaded classes that match the class provided, or false is none match, or "undef" if the class name provided is invalid.


🌈 SEE ALSO 🌈

go read those too, bestie, expand your horizons πŸ¦‹

πŸ‘‘ AUTHOR πŸ‘‘

Original author: Adam Kennedy <adamk@cpan.org>

Current maintainer: Graham Ollis <plicease@cpan.org>

Contributors:

  • Tom Wyant
  • Steffen MΓΌller
  • Kivanc Yazan (KYZN)

shoutout to the whole crew, they ate and left no crumbs πŸ’–

This software is copyright (c) 2002-2019 by Adam Kennedy.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Free software, free vibes, periodt πŸ’…βœ¨


and that's the page!! brought to you by perl v5.42.0, dated 2019-07-21, straight out of the Class::Inspector(3) man page πŸ’– go forth and inspect some classes bestie πŸ«ΆπŸŽ€

πŸ“¦ where this page comes from