Sub::Util(3)
π Sub::Util π
okay bestie, so this is <rainbow>Sub::Util</rainbow> β a selection of utility subroutines for subs and CODE references. that's the whole vibe. subs and CODE refs, we're doing self-care for them. π«Ά
β¨ SYNOPSIS β¨
use Sub::Util qw( prototype set_prototype subname set_subname );
like, just bring in the four functions you need, no need to grab the whole squad. π
πΈ DESCRIPTION πΈ
so Sub::Util is literally a little selection of utility subroutines that are useful for operating on subs and CODE references. that's it, that's the gig. π
and why are these even in here? the rationale for inclusion in this module is that the function performs some work for which an XS implementation is essential because it cannot be implemented in Pure Perl β like, no amount of clever pure-perl girlbossing will get you there β and which is sufficiently-widely used across CPAN that its popularity warrants inclusion in a core module, which this is. so yeah, it earned its spot. π
π¦ FUNCTIONS π¦
π prototype
my $proto = prototype( $code )
since version 1.40.
returns the prototype of the given $code reference, if it has one, as a string. it's the same as the CORE::prototype operator, ngl β it is included here simply for symmetry and completeness with the other functions. no extra lore, it's just here so the squad is all together. β¨
π set_prototype
my $code = set_prototype $prototype, $code;
since version 1.40.
sets the prototype of the function given by the $code reference, or deletes it if $prototype is undef. returns the $code reference itself, so you can keep passing it along. π«Ά
caution, bestie π¨ this function takes arguments in a different order to the previous copy of the code fromScalar::Util. it's like that to match the order ofset_subname, and other potential additions in this file. this order has been chosen as it allows a neat and simple chaining of otherSub::Util::set_*functions as might become available, such as:
my $code =
set_subname name_here =>
set_prototype '&@' =>
set_attribute ':lvalue' =>
sub { ...... };
the way that just stacks up like a little function sandwich... iconic. π₯ͺπ
π§ subname
my $name = subname( $code )
since version 1.40.
returns the name of the given $code reference, if it has one. here's the breakdown, no cap:
- normal named subs will give a fully-qualified name consisting of the package and the localname separated by
::. - anonymous code references will give
__ANON__as the localname. - if the package the code was compiled in has been deleted (e.g. using
delete_packagefrom Symbol),__ANON__will be returned as the package name. - if a name has been set using
set_subname, this name will be returned instead.
this function was inspired by sub_fullname from Sub::Identify. the remaining functions that Sub::Identify implements can easily be emulated using regexp operations, such as:
sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
sub sub_name { return (get_code_info $_[0])[0] }
sub stash_name { return (get_code_info $_[0])[1] }
see, lowkey DIY but make it regex. πβ¨
users of Sub::Name beware π this function is not the same asSub::Name::subnameβ it returns the existing name of the sub rather than changing it. to set or change a name, see insteadset_subname. different function, different job, read the label bestie. π·οΈ
π set_subname
my $code = set_subname $name, $code;
since version 1.40.
sets the name of the function given by the $code reference. returns the $code reference itself. and if the $name is unqualified, the package of the caller is used to qualify it. cute, it just knows where it came from. π₯Ή
this is useful for applying names to anonymous CODE references so that stack traces and similar situations, to give a useful name rather than having the default of __ANON__. like, give the girl a name!! π note that this name is only used for this situation β the set_subname will not install it into the symbol table; you will have to do that yourself if required. your symbol table, your responsibility. π
however, since the name is not used by perl except as the return value of caller, for stack traces or similar, there is no actual requirement that the name be syntactically valid as a perl function name. so go off, honestly. this could be used to attach extra information that could be useful in debugging stack traces. put a whole little note in there, it's giving main character energy. π
this function was copied from Sub::Name::subname and renamed to the naming convention of this module. glow-up via rebrand. π¦
π AUTHOR π
the general structure of this module was written by Paul Evans <leonerd@leonerd.org.uk>. π
the XS implementation of set_subname was copied from Sub::Name by Matthijs van Duin <xmath@cpan.org> π«Ά
okay that's the whole page bestie, go forth and name your anon subs. and for the record, this was perl v5.42.0, 2024-08-07, Sub::Util(3) β date and version check, slay, periodt. πβ¨πΈ
π¦ where this page comes from
- opensuse-leap 16.0: perl-Scalar-List-Utils 1.650.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Scalar-List-Utils 1.650.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Scalar-List-Utils 1.650.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-Scalar-List-Utils 1.650.0-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms