As far as I know, there are basically three ways to define and use subroutine attributes in the Perl environment:
The built-in attributes are integrated into the compilation process and can affect what counts as legal syntax (e.g. lvalue). In addition to the two built-in attributes listed above, there are two version specific subroutine attributes that you might see in older code:
- locked which was used in the threaded version of Perl 5.005 and deprecated thereafter.
- assertion which was added in 5.9.0 and removed in 5.10.0 because it made code disappear when maybe it shouldn't - see the assertion pragma for a discussion and further links.
As there are very few built-in attributes, Perl and its extensions provide two mechanisms for custom attribute definitions. Common reasons to use custom attributes are
- generation of a wrapper function that inserts code before and after the subroutine call. The code assigned to the subroutine name is replaced by the code making before+call+after code. For example, you might want to log the input parameters and return values of a call.
- generation supplemental functions and data based on the actual attributes.
- compilation messages and metadata statistics for use by frameworks (e.g. Catalyst and
MooseClass::Std).
Custom attributes defined using FETCH_CODE_ATTRIBUTES and MODIFY_CODE_ATTRIBUTES can be used safely under mod_perl but only handle use cases 2 and 3 above. They can't be used to generate wrappers that replace subroutine definitions (use case 1) because MODIFY_CODE_ATTRIBUTES is called before the function
body has been defined and inserted into the symbol table.
Attribute::Handlers can be used for all three purposes, but it relies on the CHECK and INIT phases for loading up a module and these don't get triggered under mod_perl or in other environments that rely on eval to load code. The issues related to the two different methods for defining custom attributes has been discussed at length in the following threads:
For examples using customized attributes, see the following links:
Best, beth |