sub_util_set_prototype#

Set or clear the prototype on a subroutine reference.

Synopsis#

use Sub::Util qw(set_prototype);
my $code = set_prototype '$$', \&adder;
my $code = set_prototype undef, \&adder;   # clear the prototype

What you get back#

The same code reference, with its prototype replaced by $proto (a string like '$$', '&@', '\@') or cleared when $proto is undef.

Examples#

set_prototype '$$', \&my_add;     # force two scalar args
set_prototype '&@',  \&wrapper;    # block followed by a list
set_prototype undef, \&free_form;  # no prototype

Edge cases#

  • Croaks with set_prototype: not a reference for scalars.

  • Croaks with set_prototype: not a subroutine reference for non-CODE refs.

  • Croaks with set_prototype: not enough arguments on missing args.

Differences from upstream#

Fully compatible with upstream.

See also#

  • set_subname — rename a code reference.

  • prototype — perl built-in that reads a prototype.