1Sub::Util(3) User Contributed Perl Documentation Sub::Util(3)
2
3
4
6 Sub::Util - A selection of utility subroutines for subs and CODE
7 references
8
10 use Sub::Util qw( prototype set_prototype subname set_subname );
11
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
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 a name has been set using
63 "set_subname", this name will be returned instead.
64
65 This function was inspired by "sub_fullname" from Sub::Identify. The
66 remaining functions that "Sub::Identify" implements can easily be
67 emulated using regexp operations, such as
68
69 sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
70 sub sub_name { return (get_code_info $_[0])[0] }
71 sub stash_name { return (get_code_info $_[0])[1] }
72
73 Users of Sub::Name beware: This function is not the same as
74 "Sub::Name::subname"; it returns the existing name of the sub rather
75 than changing it. To set or change a name, see instead "set_subname".
76
77 set_subname
78 my $code = set_subname $name, $code;
79
80 Since version 1.40.
81
82 Sets the name of the function given by the $code reference. Returns the
83 $code reference itself. If the $name is unqualified, the package of the
84 caller is used to qualify it.
85
86 This is useful for applying names to anonymous CODE references so that
87 stack traces and similar situations, to give a useful name rather than
88 having the default of "__ANON__". Note that this name is only used for
89 this situation; the "set_subname" will not install it into the symbol
90 table; you will have to do that yourself if required.
91
92 However, since the name is not used by perl except as the return value
93 of "caller", for stack traces or similar, there is no actual
94 requirement that the name be syntactically valid as a perl function
95 name. This could be used to attach extra information that could be
96 useful in debugging stack traces.
97
98 This function was copied from "Sub::Name::subname" and renamed to the
99 naming convention of this module.
100
102 The general structure of this module was written by Paul Evans
103 <leonerd@leonerd.org.uk>.
104
105 The XS implementation of "set_subname" was copied from Sub::Name by
106 Matthijs van Duin <xmath@cpan.org>
107
108
109
110perl v5.28.1 2018-02-20 Sub::Util(3)