Copyright (C) 2000-2012 |
Manpages attributesSection: Perl Programmers Reference Guide (3perl)Updated: 2001-02-22 Index Return to Main Contents NAMEattributes - get/set subroutine or variable attributesSYNOPSISsub foo : method ; my ($x,@y,%z) : Bent ; 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; DESCRIPTIONSubroutine 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 some information about the call site and the thing being declared along with the attribute list to this module. In particular, the first example above is equivalent to the following:
use attributes __PACKAGE__, \&foo, 'method';The second example in the synopsis does something equivalent to this:
use attributes __PACKAGE__, \$x, 'Bent'; use attributes __PACKAGE__, \@y, 'Bent'; use attributes __PACKAGE__, \%z, 'Bent';Yes, that's three invocations. WARNING: attribute declarations for variables are an experimental feature. The semantics of such declarations could change or be removed 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 attributes happens at compile time. 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'". Built-in AttributesThe following are the built-in attributes for subroutines:
There are no built-in attributes for anything other than subroutines. Available SubroutinesThe following subroutines are available for general use once this module has been loaded:
Note that these routines are not exported by default. Package-specific Attribute HandlingWARNING: 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. (See ``Making References'' in perlref for information on 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. 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:
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. Syntax of Attribute ListsAn 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 such a parameter list is present, it is scanned past as for the rules for 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:
switch(10,foo(7,3)) : expensive Ugly('\(") :Bad _5x5 locked methodSome 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 EXPORTSDefault exportsNone.Available exportsThe routines "get" and "reftype" are exportable.Export tags definedThe ":ALL" tag will get all of the above exports.EXAMPLESHere are some samples of syntactically valid declarations, with annotation as to how they resolve internally into "use attributes" invocations by perl. These examples are primarily useful to see how the ``appropriate package'' is found for the possible method lookups for package-defined attributes.
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. SEE ALSO``Private Variables via my()'' in perlsub and ``Subroutine Attributes'' in perlsub for details on the basic declarations; attrs for the obsolescent form of subroutine attribute specification which this module replaces; ``use'' in perlfunc for details on the normal invocation mechanism.
Index
This document was created by man2html, using the manual pages. Time: 06:17:10 GMT, May 02, 2024 |