This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade ExtUtils::CBuilder from version 0.280217 to 0.280219
[perl5.git] / cpan / Scalar-List-Utils / lib / Sub / Util.pm
CommitLineData
d81c2d6a
CBW
1# Copyright (c) 2014 Paul Evans <leonerd@leonerd.org.uk>. All rights reserved.
2# This program is free software; you can redistribute it and/or
3# modify it under the same terms as Perl itself.
4
5package Sub::Util;
6
7use strict;
8use warnings;
9
10require Exporter;
11require List::Util; # as it has the XS
12
13our @ISA = qw( Exporter );
14our @EXPORT_OK = qw(
15 prototype set_prototype
16 subname set_subname
17);
18
19our $VERSION = "1.40";
20$VERSION = eval $VERSION;
21
22=head1 NAME
23
24Sub::Util - A selection of utility subroutines for subs and CODE references
25
26=head1 SYNOPSIS
27
28 use Sub::Util qw( set_prototype subname set_subname );
29
30=head1 DESCRIPTION
31
32C<Sub::Util> contains a selection of utility subroutines that are useful for
33operating on subs and CODE references.
34
35The rationale for inclusion in this module is that the function performs some
36work for which an XS implementation is essential because it cannot be
37implemented in Pure Perl, and which is sufficiently-widely used across CPAN
38that its popularity warrants inclusion in a core module, which this is.
39
40=cut
41
42=head1 FUNCTIONS
43
44=cut
45
46=head2 prototype
47
48 my $proto = prototype( $code )
49
50Returns the prototype of the given C<$code> reference, if it has one, as a
51string. This is the same as the C<CORE::prototype> operator; it is included
52here simply for symmetry and completeness with the other functions.
53
54=cut
55
56sub prototype
57{
58 my ( $code ) = @_;
59 return CORE::prototype( $code );
60}
61
62=head2 set_prototype
63
64 my $code = set_prototype $prototype, $code;
65
66I<Since version 1.40.>
67
68Sets the prototype of the function given by the C<$code> reference, or deletes
69it if C<$prototype> is C<undef>. Returns the C<$code> reference itself.
70
71I<Caution>: This function takes arguments in a different order to the previous
72copy of the code from C<Scalar::Util>. This is to match the order of
73C<set_subname>, and other potential additions in this file. This order has
74been chosen as it allows a neat and simple chaining of other
75C<Sub::Util::set_*> functions as might become available, such as:
76
77 my $code =
78 set_subname name_here =>
79 set_prototype '&@' =>
80 set_attribute ':lvalue' =>
81 sub { ...... };
82
83=cut
84
85=head2 subname
86
87 my $name = subname( $code )
88
89I<Since version 1.40.>
90
91Returns the name of the given C<$code> reference, if it has one. Normal named
92subs will give a fully-qualified name consisting of the package and the
93localname separated by C<::>. Anonymous code references will give C<__ANON__>
94as the localname. If a name has been set using L</set_subname>, this name will
95be returned instead.
96
97This function was inspired by C<sub_fullname> from L<Sub::Identify>. The
98remaining functions that C<Sub::Identify> implements can easily be emulated
99using regexp operations, such as
100
101 sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.+?)$/ }
102 sub sub_name { return (get_code_info $_[0])[0] }
103 sub stash_name { return (get_code_info $_[0])[1] }
104
105I<Users of Sub::Name beware>: This function is B<not> the same as
106C<Sub::Name::subname>; it returns the existing name of the sub rather than
107changing it. To set or change a name, see instead L</set_subname>.
108
109=cut
110
111=head2 set_subname
112
113 my $code = set_subname $name, $code;
114
115I<Since version 1.40.>
116
117Sets the name of the function given by the C<$code> reference. Returns the
118C<$code> reference itself. If the C<$name> is unqualified, the package of the
119caller is used to qualify it.
120
121This is useful for applying names to anonymous CODE references so that stack
122traces and similar situations, to give a useful name rather than having the
123default of C<__ANON__>. Note that this name is only used for this situation;
124the C<set_subname> will not install it into the symbol table; you will have to
125do that yourself if required.
126
127However, since the name is not used by perl except as the return value of
128C<caller>, for stack traces or similar, there is no actual requirement that
129the name be syntactically valid as a perl function name. This could be used to
130attach extra information that could be useful in debugging stack traces.
131
132This function was copied from C<Sub::Name::subname> and renamed to the naming
133convention of this module.
134
135=cut
136
137=head1 AUTHOR
138
139The general structure of this module was written by Paul Evans
140<leonerd@leonerd.org.uk>.
141
142The XS implementation of L</set_subname> was copied from L<Sub::Name> by
143Matthijs van Duin <xmath@cpan.org>
144
145=cut
146
1471;