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;
use attributes __PACKAGE__, \&foo, 'method';
The second example in the synopsis does something equivalent to this:
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, depending on how you look at it.) 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 will it apply the "Bent" attribute to the variable.
An attempt to set an unrecognized attribute is a fatal error. (The error is trappable, but it still stops the compilation within that "eval".) Setting an attribute with a name that's all lowercase letters that's not a built-in attribute (such as ``foo'') will result in a warning with -w or "use warnings 'reserved'".
Setting this attribute is only meaningful when the subroutine or method is to be called by multiple threads. When set on a method subroutine (i.e., one marked with the method attribute below), Perl ensures that any invocation of it implicitly locks its first argument before execution. When set on a non-method subroutine, Perl ensures that a lock is taken on the subroutine itself before execution. The semantics of the lock are exactly those of one explicitly taken with the "lock" operator immediately after the subroutine is entered.
For global variables there is "unique" attribute: see ``our'' in perlfunc.
Note that these routines are not exported by default.
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. Because these attributes are associated with subroutine or variable declarations, 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 these:
The call to this method is currently made during the processing of the declaration. In particular, this means that 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. Thus, this circumstance will not result in a method call for package-defined attributes. A named subroutine knows to which symbol table entry it belongs (or originally belonged), and it will use the corresponding package. An anonymous subroutine knows the package name into which it was compiled (unless it was also compiled with a null package declaration), and so it will use that package name.
Some examples of syntactically valid attribute lists:
switch(10,foo(7,3)) : expensive Ugly('\(") :Bad _5x5 locked method
Some examples of syntactically invalid attribute lists (with annotation):
switch(10,foo() # ()-string not balanced Ugly('(') # ()-string not balanced 5x5 # "5x5" not a valid identifier Y2::north # "Y2::north" not a simple identifier foo + bar # "+" neither a colon nor whitespace
package Canine; package Dog; my Canine $spot : Watchful ;
Effect:
use attributes (); attributes::->import(Canine => \$spot, "Watchful");
package Felis; my $cat : Nervous;
Effect:
use attributes (); attributes::->import(Felis => \$cat, "Nervous");
package X; sub foo : locked ;
Effect:
use attributes X => \&foo, "locked";
package X; sub Y::x : locked { 1 }
Effect:
use attributes Y => \&Y::x, "locked";
package X; sub foo { 1 } package Y; BEGIN { *bar = \&X::foo; } package Z; sub Y::bar : locked ;
Effect:
use attributes X => \&X::foo, "locked";
This last example is purely for purposes of completeness. You should not be trying to mess with the attributes of something in a package that's not your own.