info > ATTRIBUTES(7)

🏷️ NAME

attributes - get/set subroutine or variable attributes

🚀 Quick Reference

🔧 Use Case💻 Command📝 Description
Retrieve attributesmy @attrs = attributes::get(\&foo);Get list of attributes on a subroutine
Declare a methodsub foo : method {}Suppress ambiguous‑call warnings
Declare an lvalue subsub foo : lvalue {}Subroutine that can be assigned to; must return modifiable value
Constant anonymous submy $s = sub : const { 'value' };Evaluated immediately and turned into a constant
Get reference typemy $type = attributes::reftype(\$var);Returns 'SCALAR', 'ARRAY', etc.
Custom attribute validationsub MODIFY_CODE_ATTRIBUTES { … }Handle user‑defined attributes in a package

📜 SYNOPSIS

    sub foo : method ;
    my ($x,@y,%z) : Bent = 1;
    my $s = sub : method { ... };

    use attributes ();    # optional, to get subroutine declarations
    my @attrlist = attributes::get(\&foo);

    use attributes 'get'; # import the attributes::get subroutine
    my @attrlist = get \&foo;

📖 DESCRIPTION

Subroutine declarations and definitions may optionally have attribute lists associated with them. (Variable my declarations also may, but see the warning below.) Perl handles these declarations by passing information about the call site and the thing being declared along with the attribute list to this module. The first example above is equivalent to:

    use attributes __PACKAGE__, \&foo, 'method';

The second example does something equivalent to:

    use attributes ();
    my ($x,@y,%z);
    attributes::->import(__PACKAGE__, \$x, 'Bent');
    attributes::->import(__PACKAGE__, \@y, 'Bent');
    attributes::->import(__PACKAGE__, \%z, 'Bent');
    ($x,@y,%z) = 1;

Yes, that's a lot of expansion.

⚠️ WARNING: attribute declarations for variables are still evolving. The semantics and interfaces of such declarations could change in future versions. They are present for purposes of experimentation with what the semantics ought to be. Do not rely on the current implementation of this feature.

There are only a few attributes currently handled by Perl itself (or directly by this module). However, package‑specific attributes are allowed by an extension mechanism. (See “Package‑specific Attribute Handling” below.)

The setting of subroutine attributes happens at compile time. Variable attributes in our declarations are also applied at compile time. However, my variables get their attributes applied at run‑time. This means that you have to reach the run‑time component of the my before those attributes will get applied. For example:

    my $x : Bent = 42 if 0;

will neither assign 42 to $x nor apply the Bent attribute.

An attempt to set an unrecognized attribute is a fatal error. (The error is trappable, but it still stops compilation within that eval.) Setting an attribute whose name is all lowercase and not a built‑in (such as foo) will produce a warning with -w or use warnings 'reserved'.

❓ What import does

The description says

    sub foo : method;

is equivalent to

    use attributes __PACKAGE__, \&foo, 'method';

This calls the import function of attributes at compile time with these parameters: the module name, the caller’s package, the code reference and 'method'.

    attributes->import( __PACKAGE__, \&foo, 'method' );

So what does import actually do?

First, import gets the type of the third parameter ('CODE' in this case). attributes.pm checks if there is a subroutine called MODIFY_<reftype>_ATTRIBUTES in the caller’s namespace (here: 'main'). In this example a subroutine MODIFY_CODE_ATTRIBUTES is required. Then this method is called to check for “bad attributes”. The call would look like

    MODIFY_CODE_ATTRIBUTES( 'main', \&foo, 'method' );

MODIFY_<reftype>_ATTRIBUTES must return a list of all “bad attributes”. If any are present, import croaks.

(See “Package‑specific Attribute Handling” below.)

🧱 Built-in Attributes

The following are the built‑in attributes for subroutines:

The following are the built‑in attributes for variables:

📦 Available Subroutines

The following subroutines are available for general use once this module has been loaded:

These routines are not exported by default.

🔧 Package‑specific Attribute Handling

⚠️ WARNING: the mechanisms described here are still experimental. Do not rely on the current implementation. In particular, there is no provision for applying package attributes to ‘cloned’ copies of subroutines used as closures. Package‑specific attribute handling may change incompatibly in a future release.

When an attribute list is present in a declaration, a check is made to see whether an attribute ‘modify’ handler is present in the appropriate package (or its @ISA inheritance tree). Similarly, when attributes::get is called on a valid reference, a check is made for an appropriate attribute ‘fetch’ handler. See “EXAMPLES” to see how the “appropriate package” determination works.

The handler names are based on the underlying type of the variable being declared or of the reference passed. This deliberately ignores any possibility of being blessed into some package. Thus, a subroutine declaration uses “CODE” as its type, and even a blessed hash reference uses “HASH” as its type.

The class methods invoked for modifying and fetching are:

The call to MODIFY_type_ATTRIBUTES is made during the processing of the declaration. In particular, a subroutine reference will probably be for an undefined subroutine, even if this declaration is actually part of the definition.

Calling attributes::get() from within the scope of a null package declaration package ; for an unblessed variable reference will not provide any starting package name for the ‘fetch’ method lookup, and thus will not result in a method call for package‑defined attributes. A named subroutine knows to which symbol table entry it belongs and will use the corresponding package. An anonymous subroutine knows the package name into which it was compiled (unless compiled with a null package declaration), and so it will use that package name.

🔡 Syntax of Attribute Lists

An attribute list is a sequence of attribute specifications, separated by whitespace or a colon (with optional whitespace). Each attribute specification is a simple name, optionally followed by a parenthesised parameter list. If a parameter list is present, it is scanned past as for the rules of the q() operator (see “Quote and Quote‑like Operators” in perlop). The parameter list is passed as it was found, however, and not as per q().

Some examples of syntactically valid attribute lists:

Some examples of syntactically invalid attribute lists (with annotation):

📤 EXPORTS

🛑 Default exports

None.

🔌 Available exports

The routines get and reftype are exportable.

🏷️ Export tags defined

The :ALL tag will get all of the above exports.

🔍 EXAMPLES

Samples of syntactically valid declarations, with annotation as to how they resolve internally into use attributes invocations by perl. These illustrate how the “appropriate package” is found for possible method lookups for package‑defined attributes.

  1. Code:
        package Canine;
        package Dog;
        my Canine $spot : Watchful ;
    Effect:
        use attributes ();
        attributes::->import(Canine => \$spot, "Watchful");
  2. Code:
        package Felis;
        my $cat : Nervous;
    Effect:
        use attributes ();
        attributes::->import(Felis => \$cat, "Nervous");
  3. Code:
        package X;
        sub foo : lvalue ;
    Effect:
        use attributes X => \&foo, "lvalue";
  4. Code:
        package X;
        sub Y::x : lvalue { 1 }
    Effect:
        use attributes Y => \&Y::x, "lvalue";
  5. Code:
        package X;
        sub foo { 1 }
    
        package Y;
        BEGIN { *bar = \&X::foo; }
    
        package Z;
        sub Y::bar : lvalue ;
    Effect:
        use attributes X => \&X::foo, "lvalue";

This last example is purely for completeness. You should not be trying to mess with the attributes of something in a package that's not your own.

💡 MORE EXAMPLES

  1.     sub MODIFY_CODE_ATTRIBUTES {
           my ($class,$code,@attrs) = @_;
    
           my $allowed = 'MyAttribute';
           my @bad = grep { $_ ne $allowed } @attrs;
    
           return @bad;
        }
    
        sub foo : MyAttribute {
           print "foo\n";
        }

    This example runs. At compile time MODIFY_CODE_ATTRIBUTES is called. We check if any attribute is disallowed and return a list of “bad attributes”. Since we return an empty list, everything is fine.

  2.     sub MODIFY_CODE_ATTRIBUTES {
           my ($class,$code,@attrs) = @_;
    
           my $allowed = 'MyAttribute';
           my @bad = grep{ $_ ne $allowed }@attrs;
    
           return @bad;
        }
    
        sub foo : MyAttribute Test {
           print "foo\n";
        }

    This example is aborted at compile time because we use the attribute Test which isn’t allowed. MODIFY_CODE_ATTRIBUTES returns a list containing 'Test'.

🔗 SEE ALSO

“Private Variables via my()” in perlsub, “Subroutine Attributes” in perlsub for details on basic declarations; “use” in perlfunc for details on the normal invocation mechanism.


🏷️ NAME

attributes - POSIX safety concepts

🚀 Quick Reference

🛡️ Use Case📋 Concept📝 Description
Check thread safetyMT‑Safe / MT‑Unsafe markersLook in function’s ATTRIBUTES section of man page
Initialise safelyinit featureCall function once in single‑thread context before starting threads
Avoid data racesrace featureUse mutexes around functions that modify shared state
Handle constant objectsconst featureUse read‑write lock; readers are safe, writers need write lock
Signal interferencesig featureBlock or ensure exclusive use of temporary signal handler
Terminal safetyterm featureUse a mutex to protect terminal attribute changes
Locale / environment readslocale, envSafe as long as modifying functions are not called concurrently

📖 DESCRIPTION

Note: the text of this man page is based on the material taken from the “POSIX Safety Concepts” section of the GNU C Library manual. Further details on the topics described here can be found in that manual.

Various function manual pages include a section ATTRIBUTES that describes the safety of calling the function in various contexts. This section annotates functions with the following safety markings:

Other keywords that appear in safety notes are defined in subsequent sections.

⚠️ Conditionally safe features

For some features that make functions unsafe to call in certain contexts, there are known ways to avoid the safety problem other than refraining from calling the function altogether. The keywords that follow refer to such features, and each of their definitions indicates how the whole program needs to be constrained in order to remove the safety problem indicated by the keyword. Only when all the reasons that make a function unsafe are observed and addressed, by applying the documented constraints, does the function become safe to call in a context.

ℹ️ Other safety remarks

Additional keywords may be attached to functions, indicating features that do not make a function unsafe to call, but that may need to be taken into account in certain classes of programs:

🔗 SEE ALSO

pthreads(7), signal-safety(7)

📚 COLOPHON

This page is part of release 5.10 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.

ATTRIBUTES(7)
🏷️ NAME 🚀 Quick Reference 📜 SYNOPSIS 📖 DESCRIPTION
❓ What import does 🧱 Built-in Attributes 📦 Available Subroutines 🔧 Package‑specific Attribute Handling 🔡 Syntax of Attribute Lists
📤 EXPORTS
🛑 Default exports 🔌 Available exports 🏷️ Export tags defined
🔍 EXAMPLES 💡 MORE EXAMPLES 🔗 SEE ALSO 🏷️ NAME 🚀 Quick Reference 📖 DESCRIPTION
⚠️ Conditionally safe features ℹ️ Other safety remarks
🔗 SEE ALSO 📚 COLOPHON

Generated by phpman v4.9.26-1-g511901d · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-05 03:51 @216.73.216.89
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^