# man > Sub::Name(3pm)

---
type: CommandReference
command: Sub::Name
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference

- `subname($name, $coderef)` — Assign a new name to a referenced sub, returning the sub.

## Name

Sub::Name - (Re)name a sub

## Synopsis

perl
use Sub::Name;

subname $name, $subref;

$subref = subname foo => sub { ... };
## Functions

### `subname(NAME, CODEREF)`

Assigns a new name to the referenced sub. If package specification is omitted in the name, the current package is used. The return value is the sub.

The name is only used for informative routines (`caller`, `Carp`, etc.). You cannot invoke the sub by the given name without glob-assignment.

For anonymous closures (subs that reference lexicals declared outside the sub itself) you can name each instance of the closure differently, which is useful for debugging.

## Examples

perl
use Sub::Name;

# Rename an existing sub
subname 'my_new_name', \&original_sub;

# Name an anonymous sub
my $sub = subname 'my_closure' => sub { ... };
## See Also

- [Sub::Identify](https://metacpan.org/pod/Sub::Identify) — for getting information about subs
- [Sub::Util](https://metacpan.org/pod/Sub::Util) — `set_subname` is another implementation of `subname`