Sub::Util(3perl)
π Sub::Util
okay bestie, so this whole page is Sub::Util(3perl) β Perl Programmers Reference Guide vibes β and it's literally just a lil grab-bag of helper subroutines for messing with subs and CODE refs. iykyk. π
πΈ NAME
Sub::Util β a whole lil selection of utility subroutines for subs and CODE references. yes that's the whole name section, she's simple but she serves. β¨
π SYNOPSIS
use Sub::Util qw( prototype set_prototype subname set_subname );
that's it. one line, four functions, we love a low-effort queen. π
π¦ DESCRIPTION
Sub::Util contains a selection of utility subroutines that are useful for operating on subs and CODE references. no cap, that's the whole thing.
the rationale for why these bitches even got put in this module: each function does some work where an XS implementation is essential, because you literally cannot do it in Pure Perl. and also they're used sooooo widely across CPAN that the popularity alone warrants inclusion in a core module β which, btw, this is. main character entry to core, honestly. π
β¨ FUNCTIONS β¨
π prototype
my $proto = prototype( $code )
since version 1.40. β
returns the prototype of the given $code reference, if it even has one, as a string. it's literally the exact same thing as the CORE::prototype operator β she's here purely for symmetry and completeness with her sisters. no extra lore, no secrets, just vibes-matching. π«Ά
π set_prototype
my $code = set_prototype $prototype, $code;
since version 1.40. β
sets the prototype of the function given by the $code reference β or straight-up deletes it if $prototype is undef. returns the $code reference itself, so you can keep the party going.
β οΈ caution bestie: this function takes its arguments in a different order than the previous copy of the code fromScalar::Util. why the flip? because it matches the order ofset_subname, and other potential additions in this file. this order was chosen because it allows a neat and simple chaining of otherSub::Util::set_*functions as they might become available, like:
my $code =
set_subname name_here =>
set_prototype '&@' =>
set_attribute ':lvalue' =>
sub { ...... };
look at that chain. she's giving pipeline. she's giving glow-up. ππ₯
πΈ subname
my $name = subname( $code )
since version 1.40. β
returns the name of the given $code reference, if it has one. deets:
- normal named subs will give you a fully-qualified name β package plus localname, separated by
::. - anonymous code references will give you
__ANON__as the localname. - if the package the code was compiled in has been deleted (like, say, using
delete_packagefromSymbol), then__ANON__gets returned as the package name instead. π - if a name has been set using
set_subname, then that name is what you get back instead.
this function was inspired by sub_fullname from Sub::Identify. and the remaining functions that Sub::Identify implements can easily be emulated using regexp operations, like so:
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] }
π users of Sub::Name beware: this function is not the same asSub::Name::subname. she returns the existing name of the sub rather than changing it. if you wanna set or change a name, go seeset_subnameinstead. them's the rules.
π§ 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 $name is unqualified, the package of the caller is used to qualify it β auto-glow-up, no extra steps. β¨
this is useful for applying names to anonymous CODE references so that stack traces and similar situations give you a useful name, instead of that default __ANON__ sadness. but note: this name is only used for that situation. set_subname will not install it into the symbol table β you'll have to do that yourself if you require it. do it yourself energy. π«‘
however! since the name isn't used by perl except as the return value of caller, for stack traces or similar, there's no actual requirement that the name be syntactically valid as a perl function name. which means you could lowkey use this to attach extra information that might be useful when debugging stack traces. chaotic but iconic. π
this function was copied from Sub::Name::subname and renamed to the naming convention of this module. rebranded, refreshed, still slaying.
π 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>. π
and that's the whole page bestie β perl v5.44.0, dated 2026-10-17, filed forever under Sub::Util(3perl). go forth and name your anon subs, you gorgeous __ANON__-free legend. periodt. π
ππ
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms