| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "Sub::Util 3perl" |
| 58 |
.TH Sub::Util 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
Sub::Util \- A selection of utility subroutines for subs and CODE references |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use Sub::Util qw( prototype set_prototype subname set_subname ); |
| 69 |
.Ve |
| 70 |
.SH DESCRIPTION |
| 71 |
.IX Header "DESCRIPTION" |
| 72 |
\&\f(CW\*(C`Sub::Util\*(C'\fR contains a selection of utility subroutines that are useful for |
| 73 |
operating on subs and CODE references. |
| 74 |
.PP |
| 75 |
The rationale for inclusion in this module is that the function performs some |
| 76 |
work for which an XS implementation is essential because it cannot be |
| 77 |
implemented in Pure Perl, and which is sufficiently-widely used across CPAN |
| 78 |
that its popularity warrants inclusion in a core module, which this is. |
| 79 |
.SH FUNCTIONS |
| 80 |
.IX Header "FUNCTIONS" |
| 81 |
.SS prototype |
| 82 |
.IX Subsection "prototype" |
| 83 |
.Vb 1 |
| 84 |
\& my $proto = prototype( $code ) |
| 85 |
.Ve |
| 86 |
.PP |
| 87 |
\&\fISince version 1.40.\fR |
| 88 |
.PP |
| 89 |
Returns the prototype of the given \f(CW$code\fR reference, if it has one, as a |
| 90 |
string. This is the same as the \f(CW\*(C`CORE::prototype\*(C'\fR operator; it is included |
| 91 |
here simply for symmetry and completeness with the other functions. |
| 92 |
.SS set_prototype |
| 93 |
.IX Subsection "set_prototype" |
| 94 |
.Vb 1 |
| 95 |
\& my $code = set_prototype $prototype, $code; |
| 96 |
.Ve |
| 97 |
.PP |
| 98 |
\&\fISince version 1.40.\fR |
| 99 |
.PP |
| 100 |
Sets the prototype of the function given by the \f(CW$code\fR reference, or deletes |
| 101 |
it if \f(CW$prototype\fR is \f(CW\*(C`undef\*(C'\fR. Returns the \f(CW$code\fR reference itself. |
| 102 |
.PP |
| 103 |
\&\fICaution\fR: This function takes arguments in a different order to the previous |
| 104 |
copy of the code from \f(CW\*(C`Scalar::Util\*(C'\fR. This is to match the order of |
| 105 |
\&\f(CW\*(C`set_subname\*(C'\fR, and other potential additions in this file. This order has |
| 106 |
been chosen as it allows a neat and simple chaining of other |
| 107 |
\&\f(CW\*(C`Sub::Util::set_*\*(C'\fR functions as might become available, such as: |
| 108 |
.PP |
| 109 |
.Vb 5 |
| 110 |
\& my $code = |
| 111 |
\& set_subname name_here => |
| 112 |
\& set_prototype \*(Aq&@\*(Aq => |
| 113 |
\& set_attribute \*(Aq:lvalue\*(Aq => |
| 114 |
\& sub { ...... }; |
| 115 |
.Ve |
| 116 |
.SS subname |
| 117 |
.IX Subsection "subname" |
| 118 |
.Vb 1 |
| 119 |
\& my $name = subname( $code ) |
| 120 |
.Ve |
| 121 |
.PP |
| 122 |
\&\fISince version 1.40.\fR |
| 123 |
.PP |
| 124 |
Returns the name of the given \f(CW$code\fR reference, if it has one. Normal named |
| 125 |
subs will give a fully-qualified name consisting of the package and the |
| 126 |
localname separated by \f(CW\*(C`::\*(C'\fR. Anonymous code references will give \f(CW\*(C`_\|_ANON_\|_\*(C'\fR |
| 127 |
as the localname. If the package the code was compiled in has been deleted |
| 128 |
(e.g. using \f(CW\*(C`delete_package\*(C'\fR from Symbol), \f(CW\*(C`_\|_ANON_\|_\*(C'\fR will be returned as |
| 129 |
the package name. If a name has been set using "set_subname", this name will be |
| 130 |
returned instead. |
| 131 |
.PP |
| 132 |
This function was inspired by \f(CW\*(C`sub_fullname\*(C'\fR from Sub::Identify. The |
| 133 |
remaining functions that \f(CW\*(C`Sub::Identify\*(C'\fR implements can easily be emulated |
| 134 |
using regexp operations, such as |
| 135 |
.PP |
| 136 |
.Vb 3 |
| 137 |
\& sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ } |
| 138 |
\& sub sub_name { return (get_code_info $_[0])[0] } |
| 139 |
\& sub stash_name { return (get_code_info $_[0])[1] } |
| 140 |
.Ve |
| 141 |
.PP |
| 142 |
\&\fIUsers of Sub::Name beware\fR: This function is \fBnot\fR the same as |
| 143 |
\&\f(CW\*(C`Sub::Name::subname\*(C'\fR; it returns the existing name of the sub rather than |
| 144 |
changing it. To set or change a name, see instead "set_subname". |
| 145 |
.SS set_subname |
| 146 |
.IX Subsection "set_subname" |
| 147 |
.Vb 1 |
| 148 |
\& my $code = set_subname $name, $code; |
| 149 |
.Ve |
| 150 |
.PP |
| 151 |
\&\fISince version 1.40.\fR |
| 152 |
.PP |
| 153 |
Sets the name of the function given by the \f(CW$code\fR reference. Returns the |
| 154 |
\&\f(CW$code\fR reference itself. If the \f(CW$name\fR is unqualified, the package of the |
| 155 |
caller is used to qualify it. |
| 156 |
.PP |
| 157 |
This is useful for applying names to anonymous CODE references so that stack |
| 158 |
traces and similar situations, to give a useful name rather than having the |
| 159 |
default of \f(CW\*(C`_\|_ANON_\|_\*(C'\fR. Note that this name is only used for this situation; |
| 160 |
the \f(CW\*(C`set_subname\*(C'\fR will not install it into the symbol table; you will have to |
| 161 |
do that yourself if required. |
| 162 |
.PP |
| 163 |
However, since the name is not used by perl except as the return value of |
| 164 |
\&\f(CW\*(C`caller\*(C'\fR, for stack traces or similar, there is no actual requirement that |
| 165 |
the name be syntactically valid as a perl function name. This could be used to |
| 166 |
attach extra information that could be useful in debugging stack traces. |
| 167 |
.PP |
| 168 |
This function was copied from \f(CW\*(C`Sub::Name::subname\*(C'\fR and renamed to the naming |
| 169 |
convention of this module. |
| 170 |
.SH AUTHOR |
| 171 |
.IX Header "AUTHOR" |
| 172 |
The general structure of this module was written by Paul Evans |
| 173 |
<[email protected]>. |
| 174 |
.PP |
| 175 |
The XS implementation of "set_subname" was copied from Sub::Name by |
| 176 |
Matthijs van Duin <[email protected]> |