Sub::Util#

Native implementation of Sub::Util

Provides subroutine utility functions for introspection and manipulation.

Synopsis#

use Sub::Util qw(subname set_subname);

my $name = subname(\&foo);    # "main::foo"

my $anon = sub { ... };
set_subname("MyPkg::handler", $anon);

Functions#

subname#

Return the fully qualified name of a subroutine reference (e.g. “main::foo”). Dies if the argument is not a code reference.

use Sub::Util 'subname';
my $name = subname(\&foo);  # "main::foo"

set_subname#

Set the name of a subroutine reference. Mutates the CV in-place and returns the same reference. Unqualified names are placed in the “main::” namespace.

use Sub::Util 'set_subname';
my $sub = set_subname("MyPkg::renamed", \&original);

prototype#

Return the prototype string of a subroutine, or undef if no prototype is set.

use Sub::Util 'prototype';
my $proto = prototype(\&mysub);  # e.g. "$$" or undef

set_prototype#

Set or remove the prototype of a subroutine reference. Pass undef to remove the prototype. Mutates the CV in-place and returns the same reference.

use Sub::Util 'set_prototype';
set_prototype('$$', \&mysub);
set_prototype(undef, \&mysub);  # remove prototype

Perl equivalent: Sub::Util (part of Scalar-List-Utils distribution)