Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sub::Util

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

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

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)

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);

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"