<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright (c) 2014 Paul Evans &lt;leonerd@leonerd.org.uk&gt;. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.

package Sub::Util;

use strict;
use warnings;

require Exporter;

our @ISA = qw( Exporter );
our @EXPORT_OK = qw(
  prototype set_prototype
  subname set_subname
);

our $VERSION    = "1.63";
$VERSION =~ tr/_//d;

require List::Util; # as it has the XS
List::Util-&gt;VERSION( $VERSION ); # Ensure we got the right XS version (RT#100863)

=head1 NAME

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

=head1 SYNOPSIS

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

=head1 DESCRIPTION

C&lt;Sub::Util&gt; contains a selection of utility subroutines that are useful for
operating on subs and CODE references.

The rationale for inclusion in this module is that the function performs some
work for which an XS implementation is essential because it cannot be
implemented in Pure Perl, and which is sufficiently-widely used across CPAN
that its popularity warrants inclusion in a core module, which this is.

=cut

=head1 FUNCTIONS

=cut

=head2 prototype

    my $proto = prototype( $code )

I&lt;Since version 1.40.&gt;

Returns the prototype of the given C&lt;$code&gt; reference, if it has one, as a
string. This is the same as the C&lt;CORE::prototype&gt; operator; it is included
here simply for symmetry and completeness with the other functions.

=cut

sub prototype
{
  my ( $code ) = @_;
  return CORE::prototype( $code );
}

=head2 set_prototype

    my $code = set_prototype $prototype, $code;

I&lt;Since version 1.40.&gt;

Sets the prototype of the function given by the C&lt;$code&gt; reference, or deletes
it if C&lt;$prototype&gt; is C&lt;undef&gt;. Returns the C&lt;$code&gt; reference itself.

I&lt;Caution&gt;: This function takes arguments in a different order to the previous
copy of the code from C&lt;Scalar::Util&gt;. This is to match the order of
C&lt;set_subname&gt;, and other potential additions in this file. This order has
been chosen as it allows a neat and simple chaining of other
C&lt;Sub::Util::set_*&gt; functions as might become available, such as:

 my $code =
    set_subname   name_here =&gt;
    set_prototype '&amp;@'      =&gt;
    set_attribute ':lvalue' =&gt;
       sub { ...... };

=cut

=head2 subname

    my $name = subname( $code )

I&lt;Since version 1.40.&gt;

Returns the name of the given C&lt;$code&gt; reference, if it has one. Normal named
subs will give a fully-qualified name consisting of the package and the
localname separated by C&lt;::&gt;. Anonymous code references will give C&lt;__ANON__&gt;
as the localname. If the package the code was compiled in has been deleted
(e.g. using C&lt;delete_package&gt; from L&lt;Symbol&gt;), C&lt;__ANON__&gt; will be returned as
the package name. If a name has been set using L&lt;/set_subname&gt;, this name will be
returned instead.

This function was inspired by C&lt;sub_fullname&gt; from L&lt;Sub::Identify&gt;. The
remaining functions that C&lt;Sub::Identify&gt; implements can easily be emulated
using regexp operations, such as

 sub get_code_info { return (subname $_[0]) =~ m/^(.+)::(.*?)$/ }
 sub sub_name      { return (get_code_info $_[0])[0] }
 sub stash_name    { return (get_code_info $_[0])[1] }

I&lt;Users of Sub::Name beware&gt;: This function is B&lt;not&gt; the same as
C&lt;Sub::Name::subname&gt;; it returns the existing name of the sub rather than
changing it. To set or change a name, see instead L&lt;/set_subname&gt;.

=cut

=head2 set_subname

    my $code = set_subname $name, $code;

I&lt;Since version 1.40.&gt;

Sets the name of the function given by the C&lt;$code&gt; reference. Returns the
C&lt;$code&gt; reference itself. If the C&lt;$name&gt; is unqualified, the package of the
caller is used to qualify it.

This is useful for applying names to anonymous CODE references so that stack
traces and similar situations, to give a useful name rather than having the
default of C&lt;__ANON__&gt;. Note that this name is only used for this situation;
the C&lt;set_subname&gt; will not install it into the symbol table; you will have to
do that yourself if required.

However, since the name is not used by perl except as the return value of
C&lt;caller&gt;, for stack traces or similar, there is no actual requirement that
the name be syntactically valid as a perl function name. This could be used to
attach extra information that could be useful in debugging stack traces.

This function was copied from C&lt;Sub::Name::subname&gt; and renamed to the naming
convention of this module.

=cut

=head1 AUTHOR

The general structure of this module was written by Paul Evans
&lt;leonerd@leonerd.org.uk&gt;.

The XS implementation of L&lt;/set_subname&gt; was copied from L&lt;Sub::Name&gt; by
Matthijs van Duin &lt;xmath@cpan.org&gt;

=cut

1;
</pre></body></html>