Sub::Identify(3)

πŸ’– NAME

Sub::Identify β€” Retrieve names of code references. Yes bestie, this is the Sub::Identify(3) page, and it’s literally here to help you figure out what a code reference is actually named. ✨


🌸 SYNOPSIS

use Sub::Identify ':all';
my $subname = sub_name( $some_coderef );
my $packagename = stash_name( $some_coderef );
# or, to get all at once...
my $fully_qualified_name = sub_fullname( $some_coderef );
defined $subname
    and say "this coderef points to sub $subname in package $packagename";
my ($file, $line) = get_code_location( $some_coderef );
$file
    and say "this coderef is defined at line $line in file $file";
is_sub_constant( $some_coderef )
    and say "this coderef points to a constant subroutine";

πŸ¦‹ DESCRIPTION

Okay so Sub::Identify lets you retrieve the real name of code references. Slay. It gives you six functions, and every single one of them takes a code reference. πŸ‘‘

  • sub_name β€” returns the name of the code reference you passed in, or __ANON__ if it’s an anonymous code reference.
  • stash_name β€” returns its package.
  • sub_fullname β€” returns the concatenation of the two, aka name plus package. Iconic.
  • get_code_info β€” returns a list of two elements: the package and the subroutine name. Use this when you want both and you’re worried about speed.
  • get_code_location β€” returns a two-element list containing the file name and the line number where the subroutine has been defined.
  • is_sub_constant β€” returns a boolean value indicating whether the subroutine is a constant or not.
🫢 Caveat bestie: in case of subroutine aliasing, those functions always return the original name. No cap.

🧚 Pure-Perl version

By default, Sub::Identify tries to load an XS implementation of the get_code_info, get_code_location and (on perl versions 5.16.0 and later) is_sub_constant functions, for speed. Fast fast. If that fails, or if the environment variable PERL_SUB_IDENTIFY_PP is defined to a true value, it will fall back to a pure perl implementation, that uses perl’s introspection mechanism, provided by the B module. It’s giving backup plan, and honestly? Obsessed. πŸ¦‹


πŸ”₯ SEE ALSO

  • Sub::Util, part of the module distribution Scalar::List::Utils since version 1.40. Since this will be a core module starting with perl 5.22.0, it is encouraged to migrate to Sub::Util when possible. iykyk.
  • Sub::Name

πŸ“¦ SOURCE

A git repository for the sources is at <https://github.com/rgs/Sub-Identify>. Go look, bestie. ✨


πŸ‘‘ LICENSE

(c) Rafael Garcia-Suarez (rgs at consttype dot org) 2005, 2008, 2012, 2014, 2015

This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. Periodt. πŸ’…


Okay that’s the whole page, bestie β€” this one’s from perl v5.42.0, dated 2017-04-12, and it ate. Mwah! πŸ’‹πŸ’–

πŸ“¦ where this page comes from