xref: /openbsd-src/gnu/usr.bin/perl/cpan/Scalar-List-Utils/lib/Sub/Util.pm (revision e068048151d29f2562a32185e21a8ba885482260)
1b8851fccSafresh1# Copyright (c) 2014 Paul Evans <leonerd@leonerd.org.uk>. All rights reserved.
2b8851fccSafresh1# This program is free software; you can redistribute it and/or
3b8851fccSafresh1# modify it under the same terms as Perl itself.
4b8851fccSafresh1
5b8851fccSafresh1package Sub::Util;
6b8851fccSafresh1
7b8851fccSafresh1use strict;
8b8851fccSafresh1use warnings;
9b8851fccSafresh1
10b8851fccSafresh1require Exporter;
11b8851fccSafresh1
12b8851fccSafresh1our @ISA = qw( Exporter );
13b8851fccSafresh1our @EXPORT_OK = qw(
14b8851fccSafresh1  prototype set_prototype
15b8851fccSafresh1  subname set_subname
16b8851fccSafresh1);
17b8851fccSafresh1
18*e0680481Safresh1our $VERSION    = "1.63";
1956d68f1eSafresh1$VERSION =~ tr/_//d;
20b8851fccSafresh1
21b8851fccSafresh1require List::Util; # as it has the XS
22b8851fccSafresh1List::Util->VERSION( $VERSION ); # Ensure we got the right XS version (RT#100863)
23b8851fccSafresh1
24b8851fccSafresh1=head1 NAME
25b8851fccSafresh1
26b8851fccSafresh1Sub::Util - A selection of utility subroutines for subs and CODE references
27b8851fccSafresh1
28b8851fccSafresh1=head1 SYNOPSIS
29b8851fccSafresh1
30b8851fccSafresh1    use Sub::Util qw( prototype set_prototype subname set_subname );
31b8851fccSafresh1
32b8851fccSafresh1=head1 DESCRIPTION
33b8851fccSafresh1
34b8851fccSafresh1C<Sub::Util> contains a selection of utility subroutines that are useful for
35b8851fccSafresh1operating on subs and CODE references.
36b8851fccSafresh1
37b8851fccSafresh1The rationale for inclusion in this module is that the function performs some
38b8851fccSafresh1work for which an XS implementation is essential because it cannot be
39b8851fccSafresh1implemented in Pure Perl, and which is sufficiently-widely used across CPAN
40b8851fccSafresh1that its popularity warrants inclusion in a core module, which this is.
41b8851fccSafresh1
42b8851fccSafresh1=cut
43b8851fccSafresh1
44b8851fccSafresh1=head1 FUNCTIONS
45b8851fccSafresh1
46b8851fccSafresh1=cut
47b8851fccSafresh1
48b8851fccSafresh1=head2 prototype
49b8851fccSafresh1
50b8851fccSafresh1    my $proto = prototype( $code )
51b8851fccSafresh1
52b8851fccSafresh1I<Since version 1.40.>
53b8851fccSafresh1
54b8851fccSafresh1Returns the prototype of the given C<$code> reference, if it has one, as a
55b8851fccSafresh1string. This is the same as the C<CORE::prototype> operator; it is included
56b8851fccSafresh1here simply for symmetry and completeness with the other functions.
57b8851fccSafresh1
58b8851fccSafresh1=cut
59b8851fccSafresh1
60b8851fccSafresh1sub prototype
61b8851fccSafresh1{
62b8851fccSafresh1  my ( $code ) = @_;
63b8851fccSafresh1  return CORE::prototype( $code );
64b8851fccSafresh1}
65b8851fccSafresh1
66b8851fccSafresh1=head2 set_prototype
67b8851fccSafresh1
68b8851fccSafresh1    my $code = set_prototype $prototype, $code;
69b8851fccSafresh1
70b8851fccSafresh1I<Since version 1.40.>
71b8851fccSafresh1
72b8851fccSafresh1Sets the prototype of the function given by the C<$code> reference, or deletes
73b8851fccSafresh1it if C<$prototype> is C<undef>. Returns the C<$code> reference itself.
74b8851fccSafresh1
75b8851fccSafresh1I<Caution>: This function takes arguments in a different order to the previous
76b8851fccSafresh1copy of the code from C<Scalar::Util>. This is to match the order of
77b8851fccSafresh1C<set_subname>, and other potential additions in this file. This order has
78b8851fccSafresh1been chosen as it allows a neat and simple chaining of other
79b8851fccSafresh1C<Sub::Util::set_*> functions as might become available, such as:
80b8851fccSafresh1
81b8851fccSafresh1 my $code =
82b8851fccSafresh1    set_subname   name_here =>
83b8851fccSafresh1    set_prototype '&@'      =>
84b8851fccSafresh1    set_attribute ':lvalue' =>
85b8851fccSafresh1       sub { ...... };
86b8851fccSafresh1
87b8851fccSafresh1=cut
88b8851fccSafresh1
89b8851fccSafresh1=head2 subname
90b8851fccSafresh1
91b8851fccSafresh1    my $name = subname( $code )
92b8851fccSafresh1
93b8851fccSafresh1I<Since version 1.40.>
94b8851fccSafresh1
95b8851fccSafresh1Returns the name of the given C<$code> reference, if it has one. Normal named
96b8851fccSafresh1subs will give a fully-qualified name consisting of the package and the
97b8851fccSafresh1localname separated by C<::>. Anonymous code references will give C<__ANON__>
9856d68f1eSafresh1as the localname. If the package the code was compiled in has been deleted
9956d68f1eSafresh1(e.g. using C<delete_package> from L<Symbol>), C<__ANON__> will be returned as
10056d68f1eSafresh1the package name. If a name has been set using L</set_subname>, this name will be
10156d68f1eSafresh1returned instead.
102b8851fccSafresh1
103b8851fccSafresh1This function was inspired by C<sub_fullname> from L<Sub::Identify>. The
104b8851fccSafresh1remaining functions that C<Sub::Identify> implements can easily be emulated
105b8851fccSafresh1using regexp operations, such as
106b8851fccSafresh1
1075759b3d2Safresh1 sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
108b8851fccSafresh1 sub sub_name      { return (get_code_info $_[0])[0] }
109b8851fccSafresh1 sub stash_name    { return (get_code_info $_[0])[1] }
110b8851fccSafresh1
111b8851fccSafresh1I<Users of Sub::Name beware>: This function is B<not> the same as
112b8851fccSafresh1C<Sub::Name::subname>; it returns the existing name of the sub rather than
113b8851fccSafresh1changing it. To set or change a name, see instead L</set_subname>.
114b8851fccSafresh1
115b8851fccSafresh1=cut
116b8851fccSafresh1
117b8851fccSafresh1=head2 set_subname
118b8851fccSafresh1
119b8851fccSafresh1    my $code = set_subname $name, $code;
120b8851fccSafresh1
121b8851fccSafresh1I<Since version 1.40.>
122b8851fccSafresh1
123b8851fccSafresh1Sets the name of the function given by the C<$code> reference. Returns the
124b8851fccSafresh1C<$code> reference itself. If the C<$name> is unqualified, the package of the
125b8851fccSafresh1caller is used to qualify it.
126b8851fccSafresh1
127b8851fccSafresh1This is useful for applying names to anonymous CODE references so that stack
128b8851fccSafresh1traces and similar situations, to give a useful name rather than having the
129b8851fccSafresh1default of C<__ANON__>. Note that this name is only used for this situation;
130b8851fccSafresh1the C<set_subname> will not install it into the symbol table; you will have to
131b8851fccSafresh1do that yourself if required.
132b8851fccSafresh1
133b8851fccSafresh1However, since the name is not used by perl except as the return value of
134b8851fccSafresh1C<caller>, for stack traces or similar, there is no actual requirement that
135b8851fccSafresh1the name be syntactically valid as a perl function name. This could be used to
136b8851fccSafresh1attach extra information that could be useful in debugging stack traces.
137b8851fccSafresh1
138b8851fccSafresh1This function was copied from C<Sub::Name::subname> and renamed to the naming
139b8851fccSafresh1convention of this module.
140b8851fccSafresh1
141b8851fccSafresh1=cut
142b8851fccSafresh1
143b8851fccSafresh1=head1 AUTHOR
144b8851fccSafresh1
145b8851fccSafresh1The general structure of this module was written by Paul Evans
146b8851fccSafresh1<leonerd@leonerd.org.uk>.
147b8851fccSafresh1
148b8851fccSafresh1The XS implementation of L</set_subname> was copied from L<Sub::Name> by
149b8851fccSafresh1Matthijs van Duin <xmath@cpan.org>
150b8851fccSafresh1
151b8851fccSafresh1=cut
152b8851fccSafresh1
153b8851fccSafresh11;
154