guess_stash#

Work out which package a reference “belongs to”, for the purpose of looking up FETCH_${type}_ATTRIBUTES / MODIFY_${type}_ATTRIBUTES.

Synopsis#

my $pkg = attributes::_guess_stash(\&Some::Module::foo);   # 'Some::Module'

What you get back#

A package name string, or undef if no package can be determined. The rules, in order:

  • A blessed referent returns its blessed class.

  • A coderef returns the stash of the sub’s glob, or the compilation stash if the glob is missing.

  • A typeglob reference returns the glob’s effective stash.

  • Plain scalar / array / hash references that aren’t blessed return undef.

Examples#

package Foo;
sub bar { }
my $p = attributes::_guess_stash(\&bar);          # 'Foo'
my $h = bless {}, 'My::Class';
my $p = attributes::_guess_stash($h);             # 'My::Class'
my $p = attributes::_guess_stash(\42);            # undef

Edge cases#

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

  • An anonymous sub with no compilation stash returns undef.

Differences from upstream#

Fully compatible with upstream.

See also#

  • get — the primary consumer of this lookup.

  • reftype — picks the ${type} half of the handler name that get builds after calling _guess_stash.