1Sub::Util(3)          User Contributed Perl Documentation         Sub::Util(3)
2
3
4

NAME

6       Sub::Util - A selection of utility subroutines for subs and CODE
7       references
8

SYNOPSIS

10           use Sub::Util qw( prototype set_prototype subname set_subname );
11

DESCRIPTION

13       "Sub::Util" contains a selection of utility subroutines that are useful
14       for operating on subs and CODE references.
15
16       The rationale for inclusion in this module is that the function
17       performs some work for which an XS implementation is essential because
18       it cannot be implemented in Pure Perl, and which is sufficiently-widely
19       used across CPAN that its popularity warrants inclusion in a core
20       module, which this is.
21

FUNCTIONS

23   prototype
24           my $proto = prototype( $code )
25
26       Since version 1.40.
27
28       Returns the prototype of the given $code reference, if it has one, as a
29       string. This is the same as the "CORE::prototype" operator; it is
30       included here simply for symmetry and completeness with the other
31       functions.
32
33   set_prototype
34           my $code = set_prototype $prototype, $code;
35
36       Since version 1.40.
37
38       Sets the prototype of the function given by the $code reference, or
39       deletes it if $prototype is "undef". Returns the $code reference
40       itself.
41
42       Caution: This function takes arguments in a different order to the
43       previous copy of the code from "Scalar::Util". This is to match the
44       order of "set_subname", and other potential additions in this file.
45       This order has been chosen as it allows a neat and simple chaining of
46       other "Sub::Util::set_*" functions as might become available, such as:
47
48        my $code =
49           set_subname   name_here =>
50           set_prototype '&@'      =>
51           set_attribute ':lvalue' =>
52              sub { ...... };
53
54   subname
55           my $name = subname( $code )
56
57       Since version 1.40.
58
59       Returns the name of the given $code reference, if it has one. Normal
60       named subs will give a fully-qualified name consisting of the package
61       and the localname separated by "::". Anonymous code references will
62       give "__ANON__" as the localname. If the package the code was compiled
63       in has been deleted (e.g. using "delete_package" from Symbol),
64       "__ANON__" will be returned as the package name. If a name has been set
65       using "set_subname", this name will be returned instead.
66
67       This function was inspired by "sub_fullname" from Sub::Identify. The
68       remaining functions that "Sub::Identify" implements can easily be
69       emulated using regexp operations, such as
70
71        sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
72        sub sub_name      { return (get_code_info $_[0])[0] }
73        sub stash_name    { return (get_code_info $_[0])[1] }
74
75       Users of Sub::Name beware: This function is not the same as
76       "Sub::Name::subname"; it returns the existing name of the sub rather
77       than changing it. To set or change a name, see instead "set_subname".
78
79   set_subname
80           my $code = set_subname $name, $code;
81
82       Since version 1.40.
83
84       Sets the name of the function given by the $code reference. Returns the
85       $code reference itself. If the $name is unqualified, the package of the
86       caller is used to qualify it.
87
88       This is useful for applying names to anonymous CODE references so that
89       stack traces and similar situations, to give a useful name rather than
90       having the default of "__ANON__". Note that this name is only used for
91       this situation; the "set_subname" will not install it into the symbol
92       table; you will have to do that yourself if required.
93
94       However, since the name is not used by perl except as the return value
95       of "caller", for stack traces or similar, there is no actual
96       requirement that the name be syntactically valid as a perl function
97       name. This could be used to attach extra information that could be
98       useful in debugging stack traces.
99
100       This function was copied from "Sub::Name::subname" and renamed to the
101       naming convention of this module.
102

AUTHOR

104       The general structure of this module was written by Paul Evans
105       <leonerd@leonerd.org.uk>.
106
107       The XS implementation of "set_subname" was copied from Sub::Name by
108       Matthijs van Duin <xmath@cpan.org>
109
110
111
112perl v5.34.0                      2021-10-08                      Sub::Util(3)
Impressum