Sub::Identify#

📦 std

Ask a code reference for its name, its package and where it was defined.

Synopsis#

use Sub::Identify qw(sub_name stash_name sub_fullname get_code_info
                     get_code_location is_sub_constant);

sub Foo::bar { 1 }
my $code = \&Foo::bar;
print sub_name($code), "\n";       # bar
print stash_name($code), "\n";     # Foo
print sub_fullname($code), "\n";   # Foo::bar
my ($file, $line) = get_code_location($code);
print is_sub_constant(sub () { 42 }) ? "constant\n" : "not\n";

Functions#

get_code_info($code) returns the package and the name of the glob the sub was defined under - ('main', '__ANON__') for an anonymous sub - or nothing for a sub still being compiled. sub_name, stash_name and sub_fullname are its parts. get_code_location returns the file and line of the sub’s first statement, or nothing for an XSUB or a sub with no body. is_sub_constant is true for a sub perl inlines as a constant (sub () { 42 }, use constant). A non-reference gives nothing (0 for is_sub_constant). Nothing is exported by default; :all exports everything.

This is a native implementation of Sub::Identify 0.14’s Identify.xs; its Sub/Identify.pm is used unchanged.

Differences from upstream#

None.

Functions#

Code introspection#

get_code_info#

The package and the name a sub was defined under.

get_code_location#

The file and line where a sub’s body begins.

is_sub_constant#

Whether a sub is a constant perl inlines.