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