get#

List every attribute set on a referent — both the built-ins Perl recognises and any package-defined ones returned by a FETCH_${type}_ATTRIBUTES handler.

Synopsis#

use attributes 'get';
my @attrs = get(\&foo);

What you get back#

A flat list. The built-in names come first (lvalue, method for subs), followed by whatever FETCH_${type}_ATTRIBUTES returned for the owning package. Returns an empty list when nothing is set.

Examples#

use attributes 'get';
sub s :lvalue { $x }
my @a = get(\&s);                    # ('lvalue')
## Package-defined attrs flow through FETCH_CODE_ATTRIBUTES:

package MyPkg;
my %tags;
sub MODIFY_CODE_ATTRIBUTES { my ($p, $c, @a) = @_; $tags{$c} = [@a]; () }
sub FETCH_CODE_ATTRIBUTES  { my ($p, $c) = @_; @{ $tags{$c} || [] } }
sub foo :Cached :Traced { }
my @a = attributes::get(\&foo);      # ('Cached', 'Traced')

Edge cases#

  • Called with the wrong number of args or a non-reference: croaks with Usage: attributes::get($reference).

  • The owning package is discovered by the same rules as _guess_stash; a referent with no discoverable stash yields built-ins only.

  • For a blessed referent, the blessed class is used as the stash, not the compilation package.

Differences from upstream#

  • Upstream falls back to scalar caller when _guess_stash returns undef; this implementation only consults the stash _guess_stash finds, so an anonymous sub compiled in a null-package context yields built-ins only instead of querying the caller’s package.

See also#

  • _fetch_attrs — the built-in-only half of this function.

  • _guess_stash — used to find the package whose FETCH_${type}_ATTRIBUTES is called.

  • reftype — picks the ${type} used in the handler name.