Debian Codegraph @

upstream/mageia-cauldron/man3pm/Sub::Util.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (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 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Sub::Util 3pm"
61 .TH Sub::Util 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Sub::Util \- A selection of utility subroutines for subs and CODE references
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 1
71 \& use Sub::Util qw( prototype set_prototype subname set_subname );
72 .Ve
73 .SH DESCRIPTION
74 .IX Header "DESCRIPTION"
75 \&\f(CW\*(C`Sub::Util\*(C'\fR contains a selection of utility subroutines that are useful for
76 operating on subs and CODE references.
77 .PP
78 The rationale for inclusion in this module is that the function performs some
79 work for which an XS implementation is essential because it cannot be
80 implemented in Pure Perl, and which is sufficiently\-widely used across CPAN
81 that its popularity warrants inclusion in a core module, which this is.
82 .SH FUNCTIONS
83 .IX Header "FUNCTIONS"
84 .SS prototype
85 .IX Subsection "prototype"
86 .Vb 1
87 \& my $proto = prototype( $code )
88 .Ve
89 .PP
90 \&\fISince version 1.40.\fR
91 .PP
92 Returns the prototype of the given \f(CW$code\fR reference, if it has one, as a
93 string. This is the same as the \f(CW\*(C`CORE::prototype\*(C'\fR operator; it is included
94 here simply for symmetry and completeness with the other functions.
95 .SS set_prototype
96 .IX Subsection "set_prototype"
97 .Vb 1
98 \& my $code = set_prototype $prototype, $code;
99 .Ve
100 .PP
101 \&\fISince version 1.40.\fR
102 .PP
103 Sets the prototype of the function given by the \f(CW$code\fR reference, or deletes
104 it if \f(CW$prototype\fR is \f(CW\*(C`undef\*(C'\fR. Returns the \f(CW$code\fR reference itself.
105 .PP
106 \&\fICaution\fR: This function takes arguments in a different order to the previous
107 copy of the code from \f(CW\*(C`Scalar::Util\*(C'\fR. This is to match the order of
108 \&\f(CW\*(C`set_subname\*(C'\fR, and other potential additions in this file. This order has
109 been chosen as it allows a neat and simple chaining of other
110 \&\f(CW\*(C`Sub::Util::set_*\*(C'\fR functions as might become available, such as:
111 .PP
112 .Vb 5
113 \& my $code =
114 \& set_subname name_here =>
115 \& set_prototype \*(Aq&@\*(Aq =>
116 \& set_attribute \*(Aq:lvalue\*(Aq =>
117 \& sub { ...... };
118 .Ve
119 .SS subname
120 .IX Subsection "subname"
121 .Vb 1
122 \& my $name = subname( $code )
123 .Ve
124 .PP
125 \&\fISince version 1.40.\fR
126 .PP
127 Returns the name of the given \f(CW$code\fR reference, if it has one. Normal named
128 subs will give a fully\-qualified name consisting of the package and the
129 localname separated by \f(CW\*(C`::\*(C'\fR. Anonymous code references will give \f(CW\*(C`_\|_ANON_\|_\*(C'\fR
130 as the localname. If the package the code was compiled in has been deleted
131 (e.g. using \f(CW\*(C`delete_package\*(C'\fR from Symbol), \f(CW\*(C`_\|_ANON_\|_\*(C'\fR will be returned as
132 the package name. If a name has been set using "set_subname", this name will be
133 returned instead.
134 .PP
135 This function was inspired by \f(CW\*(C`sub_fullname\*(C'\fR from Sub::Identify. The
136 remaining functions that \f(CW\*(C`Sub::Identify\*(C'\fR implements can easily be emulated
137 using regexp operations, such as
138 .PP
139 .Vb 3
140 \& sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
141 \& sub sub_name { return (get_code_info $_[0])[0] }
142 \& sub stash_name { return (get_code_info $_[0])[1] }
143 .Ve
144 .PP
145 \&\fIUsers of Sub::Name beware\fR: This function is \fBnot\fR the same as
146 \&\f(CW\*(C`Sub::Name::subname\*(C'\fR; it returns the existing name of the sub rather than
147 changing it. To set or change a name, see instead "set_subname".
148 .SS set_subname
149 .IX Subsection "set_subname"
150 .Vb 1
151 \& my $code = set_subname $name, $code;
152 .Ve
153 .PP
154 \&\fISince version 1.40.\fR
155 .PP
156 Sets the name of the function given by the \f(CW$code\fR reference. Returns the
157 \&\f(CW$code\fR reference itself. If the \f(CW$name\fR is unqualified, the package of the
158 caller is used to qualify it.
159 .PP
160 This is useful for applying names to anonymous CODE references so that stack
161 traces and similar situations, to give a useful name rather than having the
162 default of \f(CW\*(C`_\|_ANON_\|_\*(C'\fR. Note that this name is only used for this situation;
163 the \f(CW\*(C`set_subname\*(C'\fR will not install it into the symbol table; you will have to
164 do that yourself if required.
165 .PP
166 However, since the name is not used by perl except as the return value of
167 \&\f(CW\*(C`caller\*(C'\fR, for stack traces or similar, there is no actual requirement that
168 the name be syntactically valid as a perl function name. This could be used to
169 attach extra information that could be useful in debugging stack traces.
170 .PP
171 This function was copied from \f(CW\*(C`Sub::Name::subname\*(C'\fR and renamed to the naming
172 convention of this module.
173 .SH AUTHOR
174 .IX Header "AUTHOR"
175 The general structure of this module was written by Paul Evans
176 <[email protected]>.
177 .PP
178 The XS implementation of "set_subname" was copied from Sub::Name by
179 Matthijs van Duin <[email protected]>